方式一: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.jarapplicationContext-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)")