【原】spring redis注解使用

时间:2021-04-06 20:33:21
  •  pom.xml  
 <!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>

 

  • spring-redis.xml配置
<!-- jedis 配置 -->

<!-- redis连接池的配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="100" />
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="minIdle" value="${redis.minIdle}"/>
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
<property name="testOnReturn" value="${redis.testOnReturn}"/>
</bean>

<!-- redis的连接池pool,不是必选项:timeout/password -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
<property name="usePool" value="true"></property>
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<property name="password" value="${redis.password}" />
<property name="timeout" value="100000" />
<property name="database" value="0"></property>
<constructor-arg index="0" ref="jedisPoolConfig" />
</bean>

<!-- 配置redis模板,需要注入到 RedisCacheManager by dada --> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
</bean>

<!-- 配置缓存 by dada -->
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg ref="redisTemplate" />
<!--<property name="defaultExpiration" value="300"></property>-->
<property name="usePrefix" value="true"></property>
<property name="expires">
<util:map>
<!-- 指定key的时间为1500秒 -->
<entry key="get_bank_province_list" value="1500"></entry>
<entry key="get_areas_bypid_list" value="1500"></entry>
</util:map>
</property>
</bean>

 

  • 由于使用注解方式实现redis缓存,需要在spring初始化的时候配置缓存注解
<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效,这个cacheManager
必须指向redis配置里面的 RedisCacheManager
-->
<cache:annotation-driven cache-manager="cacheManager" />

 

  • 在需要缓存的service实现类方法加上对应的缓存注解,需要注意的是如果查询没有传入参数则需要默认指定一个key,否则会出现无法找到key的错误。#root.methodName的意思是默认使用 getBankProvince作为key的后缀,则拼接后的key为 get_bank_province_list:getBankProvince
@Cacheable(value = "get_bank_province_list",key = "#root.methodName")
public List<SAreasEntity> getBankProvince() {
System.out.println(
"get_bank_province_listget_bank_province_listget_bank_province_listget_bank_province_listget_bank_province_listget_bank_province_list");
List
<SAreasEntity> list = new ArrayList<SAreasEntity>();
List
<SAreasItem> listItem = sAreasMapper.getBankProvince();
for(int i=0;i<listItem.size();i++){
SAreasEntity sareasEntity
=new SAreasEntity();
SAreasItem sareasItem
=listItem.get(i);
sareasEntity.setId(sareasItem.getId());
sareasEntity.setName(sareasItem.getName());
sareasEntity.setPid(sareasItem.getPid());
sareasEntity.setCity_name(sareasItem.getCity_name());
list.add(sareasEntity);
}
return list;
}