Redis
Spring使用Redis;
加入redis依赖
<!-- 缓存 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.2.RELEASE</version> </dependency>
配置文件
<context:property-placeholder location="classpath*:properties/*.properties" />
<!-- 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>
实现类中注入依赖
@Autowired
private RedisTemplate redisTemplate;
redisTemplate.boundValueOps操作k-v
redisTemplate.boundSetOps操作Set
redisTemplate.boundListOps操作List
redisTemplate.boundHashOps操作Hash(Map)
步骤:
1.先从缓存中查询,如果为空则去数据库中查询,再存放在redis中
2.在增删改的操作中,删除缓存,清楚缓存。避免数据有变化时,只查询之前缓存的数据;