代码实现@Cacheable包括ssm spring3+redis和springboot+redis两种方式

16 篇文章 0 订阅
6 篇文章 1 订阅

方式一:Springboot+redis实现cacheable

https://blog.csdn.net/chachapaofan/article/details/88829265

方式二:

jar包

jedis-2.7.2.jar
org.springframework.context-3.1.3.RELEASE.jar
spring-data-redis-1.6.6.RELEASE.jar

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:jee="http://www.springframework.org/schema/jee"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:cache="http://www.springframework.org/schema/cache"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
   http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd ">

 <!-- 载入redis配置参数 -->
<!--     <context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/> -->
     
     <!-- redis config start -->
   <cache:annotation-driven cache-manager="cacheManager"/>
   <!-- 池化 -->
   <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">   
          <property name="maxIdle" value="5"></property> 
          <property name="maxTotal" value="10" />
          <property name="maxWaitMillis" value="2000" />
          <property name="testOnBorrow" value="true" />
    </bean> 
     <!-- redis连接工厂 -->
     <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy"> 
	      <property name="poolConfig" ref="jedisPoolConfig"></property> 
	      <property name="hostName" value="192.168.2.110"></property> 
	      <property name="port" value="6379"></property> 
	      <property name="password" value="jinku_redis"></property>
	      <property name="database" value="0"></property> 
	      <property name="timeout" value="3000"></property>
	      <constructor-arg index="0" ref="jedisPoolConfig" />   
	 </bean> 
	 
    <!-- key/value缓存序列化方式 -->
    <bean id="keySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
	<bean id="valueSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />

	<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
       	<property name="keySerializer" ref="keySerializer" />
		<property name="valueSerializer" ref="valueSerializer" />
		<property name="hashKeySerializer" ref="keySerializer" />
		<property name="hashValueSerializer" ref="valueSerializer" />
    </bean>
    <!-- cache管理,可配置多个 -->
	<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
		<property name="caches">
			<set>
				<bean class="org.springframework.data.redis.cache.RedisCache">
					<constructor-arg  name="redisOperations" ref="redisTemplate"/>
					<constructor-arg name="name" value="sysManage"/>
					<constructor-arg name="prefix" value="DPT:SysManage:"/>
					<constructor-arg name="expiration" value="60" />
				</bean>
			</set>
		</property>
	</bean>
	 
</beans>

配置文件中的xsd如果找不到按照下图方式修改 

 使用

//T(String).valueOf(#userID)  Integer转String  
@Cacheable(value="sysManage",key="'findDepartmentByUserId:'+T(String).valueOf(#userID)")
@Cacheable(value="sysManage",key="'findSysMenuByUserIdNew:'+T(String).valueOf(#userId)+'_'+T(String).valueOf(#parentId)")

//#root.target.getArrayToKey(#parentIds) 调用当前类中的getArrayToKey方法
@Cacheable(value="sysManage",key="'querySonGroupIdsAndUidsByParentIds:'+#root.target.getArrayToKey(#parentIds)")

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值