REDIS学习(3.2)spring boot 使用redis作为缓存

一,指定主键的生成规则

在3.1的基础上修改RedisConfig

@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
	@Bean
	public KeyGenerator smpkeyGenerator() {
		return new KeyGenerator() {
			@Override
			public Object generate(Object target, Method method, Object... params) {
				StringBuilder sb = new StringBuilder();
				sb.append(target.getClass().getSimpleName());
				sb.append(method.getName());
				for (Object o : params) {
					sb.append("_").append(o.toString());
				}
				return sb.toString();
			}
		};
	} }

相关的方法要加上keyGenerator="smpkeyGenerator"

	@Cacheable(value = "usercache", keyGenerator="smpkeyGenerator")
	public User getUser(String no,String name){
		LogCore.BASE.debug("invoke persistent:{},{}", no, name);
		return new User(no, name);
	}

如果不用keyGenerator的话可以直接使用SpEL表达式来描述key的生成规则

例如

	@CacheEvict(value = "usercache", key="#no+'_'+#name")
	public boolean clearUser(String no,String name){
		LogCore.BASE.debug("invoke clear:{},{}", no, name);
		return true;
	}

http://localhost:8084/redis/test?name=lee&no=001

在redis存的键值为

usercache~keys

\xAC\xED\x00\x05t\x00\x1BRedisServicegetUser_001_lee

二,指定序列化方式

user去掉序列化标记接口serializable

public class User{
	public String no;
	public String name;
	public User(){}
	public User(String no, String name) {
		this.no = no;
		this.name = name;
	}
}
修改RedisConfig,增加CacheManager和RedisTemplate的bean注入


@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
	@Bean
	public CacheManager cacheManager(RedisTemplate redisTemplate) {
		RedisCacheManager manager = new RedisCacheManager(redisTemplate);
		manager.setDefaultExpiration(20);//设置默认过期时间
		return manager;
	}

	@Bean
	public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
		StringRedisTemplate template = new StringRedisTemplate(factory);
		Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
		ObjectMapper om = new ObjectMapper();
		om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
		om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
		jackson2JsonRedisSerializer.setObjectMapper(om);
		template.setValueSerializer(jackson2JsonRedisSerializer);
		template.afterPropertiesSet();
		return template;
	}
	
	@Bean
	public KeyGenerator smpkeyGenerator() {
		return new KeyGenerator() {
			@Override
			public Object generate(Object target, Method method, Object... params) {
				StringBuilder sb = new StringBuilder();
				//sb.append(target.getClass().getSimpleName());
				//sb.append(method.getName());
				for (Object o : params) {
					sb.append("_").append(o.toString());
				}
				return sb.toString();
			}
		};
	}
} 
  

三,用SPEL表达式控制添加缓存的条件

如下:只有name不为null, 且返回结果不为null的时候才加入缓存

	@Cacheable(value = "usercache", keyGenerator = "smpkeyGenerator",condition="#name!=null", unless = "#result==null")
	public User getUser(String no, String name) {
		LogCore.BASE.debug("invoke getUser:{},{}", no, name);
		if (Util.anyNonEmpty(no, name)) {
			return new User(no, name);
		}
		return null;
	}




你可能感兴趣的:(spring-boot,redis)