由于最近新上的项目很多模块没有做数据缓存,大量的请求都会到数据库去查询,为了减轻数据库的压力以及提高网站响应速度,所以在这里采用了spring 提供的注解+redis实现对数据的缓存,主要针对非热点数据,例如 省市,银行卡列表等做缓存,在这里主要是查询做一个缓存实例。
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
<!-- 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>
<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效,这个cacheManager
必须指向redis配置里面的 RedisCacheManager-->
<cache:annotation-driven cache-manager="cacheManager" />
@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;
}
应用到读取数据的方法上,即可缓存的方法,如查找方法:先从缓存中读取,如果没有再调用方法获取数据,然后把数据添加到缓存中。
除了上述使用方法参数作为key之外,Spring还为我们提供了一个root对象可以用来生成key。通过该root对象我们可以获取到以下信息。
|
属性名称 |
描述 |
示例 |
|
methodName |
当前方法名 |
#root.methodName |
|
method |
当前方法 |
#root.method.name |
|
target |
当前被调用的对象 |
#root.target |
|
targetClass |
当前被调用的对象的class |
#root.targetClass |
|
args |
当前方法参数组成的数组 |
#root.args[0] |
|
caches |
当前被调用的方法使用的Cache |
#root.caches[0].name |
本文永久更新链接地址 : http://www.linuxidc.com/Linux/2017-09/147017.htm