Redis注解形式集成及注解详解(@Cacheable、
@CacheEvict、@Cach。。。
1.⾸先加⼊maven依赖
<!--redis start-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!--redis end-->
2.然后在application.properties中加⼊redis配置
# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
# Redis服务器地址
# Redis服务器连接密码(默认为空)
# Redis服务器连接端⼝
# 连接池最⼤连接数(使⽤负值表⽰没有限制)
# 连接池最⼤阻塞等待时间(使⽤负值表⽰没有限制)
# 连接池中的最⼤空闲连接
# 连接池中的最⼩空闲连接
# 连接超时时间(毫秒)
3.实现CacheConfig类
dis;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.figuration.WebConfig;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import t.annotation.Bean;
import t.annotation.Configuration;
import org.dis.cache.RedisCacheManager;
import org.tion.RedisConnectionFactory;
import org.RedisTemplate;
el表达式执行结果为import org.StringRedisTemplate;
import org.dis.serializer.Jackson2JsonRedisSerializer;
import java.time.Duration;
import java.util.Arrays;
/**
* @author cs
* redis配置类
*/
@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport {
@SuppressWarnings("rawtypes")
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
// 多个缓存的名称,⽬前只定义了⼀个
rcm.setCacheNames(Arrays.asList(WebConfig.REDIS_NAME));
//设置缓存过期时间(秒)
rcm.setDefaultExpiration(12*60*60);
return rcm;
}
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new
Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
jackson2JsonRedisSerializer.setObjectMapper(om);
RedisTemplate<String, Object> template = new RedisTemplate();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashKeySerializer(new JdkSerializationRedisSerializer());
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
4.使⽤
接下来就是如何使⽤注解啦,这⼀步反⽽是最简单的.其实只⽤到了两个注解,@Cacheable和@CacheEvict.第⼀个注解代表从缓存中查询指定的key,如果有,从缓存中取,不再执⾏⽅法.如果没有则执
⾏⽅法,并且将⽅法的返回值和指定的key关联起来,放⼊到缓存中.⽽@CacheEvict则是从缓存中清除指定的key对应的数
据.@CachePut是修改缓存中指定key 的数据
使⽤的代码如下:
@Cacheable(value="thisredis", key="'users_'+#id")
public User findUser(Integer id) {
User user = new User();
user.setUsername("hlhdidi");
user.setPassword("123");
user.setUid(id.longValue());
System.out.println("log4j2坏啦?");
logger.info("输⼊user,⽤户名:{},密码:{}",Username(),Password());
return user;
}
@CacheEvict(value="thisredis", key="'users_'+#id",condition="#id!=1")
public void delUser(Integer id) {
/
/ 删除user
System.out.println("user删除");
}
@CachePut(value="common",key="#UserName()")
public void insertSelective(User user) {
//修改
userBo.insertSelective(user);
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论