分布式项目中,使用redis数据库--减轻数据库压力
一、需要的配置文件
1.applicationContext-redis.xml (这个配置文件放在公共包common下面的spring包下面)
2.redis-config.properties (这个配置文件放在公共包common下面的properties包下面)
二、具体文件内容
1.applicationContext-redis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- redis 相关配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="JedisConnectionFactory" />
</bean>
</beans>
2.redis-config.properties
# Redis settings # server IP redis.host=192.168.200.128 # server port redis.port=6379 # server pass redis.pass= # use dbIndex redis.database=0 # 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例 redis.maxIdle=300 # 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间(毫秒),则直接抛出JedisConnectionException; redis.maxWait=3000 # 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的 redis.testOnBorrow=true
三、service层的实现思路
1.查找数据
1.1要先从redis数据库中查找
1.2判断查找的的集合是否为空
1.3为空的话再到mysql数据库中查找
1.4将查找到的数据添加到redis数据库中
2.修改数据
2.1先将要被修改的数据从redis数据空中删除
2.2然后执行修改方法,修改mysql'数据库中的数据
3.添加数据
3.1先执行添加方法将数据添加到mysql数据库中
3.2然后将新添加的这条数据添加到redis数据库中
4.删除数据
4.1先执行删除方法删除mysql数据库中的数据
4.2然后将redis数据库中的数据清空

浙公网安备 33010602011771号