@Override
public List<TbContent> getContentListByCid(long cid) {
//先查询缓存
try {
String json = jedisClient.hget(CONTENT_KEY, cid+"");
//判断是否命中缓存
if(StringUtils.isNotBlank(json)){
//吧json转换成list
List<TbContent> list = JsonUtils.jsonToList(json, TbContent.class);
return list;
}
} catch (Exception e) {
e.printStackTrace();
}
// 根据分类id查询内容列表
TbContentExample example =new TbContentExample();
//设置查询条件
Criteria createCriteria = example.createCriteria();
createCriteria.andCategoryIdEqualTo(cid);
//执行查询
List<TbContent> list = contentMapper.selectByExample(example);
//像缓存中保存结果
try {
jedisClient.hset(CONTENT_KEY, cid+"", JsonUtils.objectToJson(list));
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
@Cacheable(value="getContentListByCid",key="#root.caches[0].name.concat(':').concat(#cid)")
@Override
public List<TbContent> getContentListByCid(long cid) {
// 根据分类id查询内容列表
TbContentExample example =new TbContentExample();
//设置查询条件
Criteria createCriteria = example.createCriteria();
createCriteria.andCategoryIdEqualTo(cid);
//执行查询
List<TbContent> list = contentMapper.selectByExample(example);
return list;
}
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
<cache:annotation-driven />
<!-- Jedis线程 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="minIdle" value="${redis.minIdle}" />
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="testOnBorrow" value="true" />
</bean>
<!-- 单机版配置 begin -->
<bean id="jedisShardInfo" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redis.host}" />
<constructor-arg index="1" value="${redis.port}" type="int" />
</bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="shardInfo" ref="jedisShardInfo" />
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>
<!-- 单机版配置 end -->
<!-- 缓存序列化方式 -->
<bean id="keySerializer"
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
<bean id="valueSerializer"
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
<!-- 缓存 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<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>
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg index="0" ref="redisTemplate" />
<property name="defaultExpiration" value="${redis.expiration}" />
</bean>
# Redis settings
redis.host=192.168.25.128
redis.port=6379
redis.pass=
redis.maxIdle=30
redis.minIdle=2
redis.maxActive=60
redis.maxWait=1000
redis.maxTotal=100
redis.testOnBorrow=true
redis.expiration=36000
@Cacheable("cache1")//Cache是发生在cache1上的
public User find(Integer id) {
returnnull;
}
@Cacheable({"cache1", "cache2"})//Cache是发生在cache1和cache2上的
public User find(Integer id) {
returnnull;
}
@Cacheable(value="users", key="#id")
public User find(Integer id) {
returnnull;
}
@Cacheable(value="users", key="#p0")
public User find(Integer id) {
returnnull;
}
@Cacheable(value="users", key="#user.id")
public User find(User user) {
returnnull;
}
@Cacheable(value="users", key="#p0.id")
public User find(User user) {
returnnull;
}
属性名称 | 描述 | 示例 |
methodName | 当前方法名 | #root.methodName |
method | 当前方法 | #root.method.name |
target | 当前被调用的对象 | #root.target |
targetClass | 当前被调用的对象的class | #root.targetClass |
args | 当前方法参数组成的数组 | #root.args[0] |
caches | 当前被调用的方法使用的Cache | #root.caches[0].name |
@Cacheable(value={"users"}, key="#user.id", condition="#user.id%2==0")
public User find(User user) {
System.out.println("find user by user " + user);
return user;
}
@CachePut("users")//每次都会执行方法,并将结果存入指定的缓存中
public User find(Integer id) {
returnnull;
}
@CacheEvict(value="users", allEntries=true)
public void delete(Integer id) {
System.out.println("delete user by id: " + id);
}
小石姐姐 发表于 2017-11-10 14:44
棒棒哒,纯干货
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |