Spring Boot集成redis,key自定义生成

redis key生成策略代码:

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCachePrefix;
import org.springframework.data.redis.core.RedisTemplate;

import com.alibaba.fastjson.JSON;

/**
 * redis配置工具类
 * @Configuration表示当前类属于配置类
 * @EnableCaching表示支持缓存
 * @author ouyangjun
 */
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

	/**
	 * redis key生成策略
	 * target: 类
	 * method: 方法
	 * params: 参数
	 * @return KeyGenerator
	 * 
	 * 注意: 该方法只是声明了key的生成策略,还未被使用,需在@Cacheable注解中指定keyGenerator
	 *      如: @Cacheable(value = "key", keyGenerator = "cacheKeyGenerator")
	 */
    @Bean
    public KeyGenerator cacheKeyGenerator() {
        return (target, method, params) -> {
            StringBuilder sb = new StringBuilder();
            sb.append(target.getClass().getName());
            sb.append(method.getName());
            for (Object obj : params) {
            	// 由于参数可能不同, hashCode肯定不一样, 缓存的key也需要不一样
                sb.append(JSON.toJSONString(obj).hashCode());
            }
            return sb.toString();
        };
    }

    /**
     * redis全局默认配置
     * @param redisTemplate
     * @return
     */
    @Bean
    public CacheManager cacheManager(RedisTemplate<String, Object> redisTemplate) {
        RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
        redisCacheManager.setUsePrefix(true);
        // key缓存的前缀,以conf开头
        RedisCachePrefix cachePrefix = new RedisPrefix("conf");
        redisCacheManager.setCachePrefix(cachePrefix);
        // key缓存的过期时间, 600秒
        redisCacheManager.setDefaultExpiration(600L);
        return redisCacheManager;
    }

}

注解说明:

@Cacheable支持如下几个参数:

value:缓存位置的一段名称,不能为空

key:缓存的key,默认为空,表示使用方法的参数类型及参数值作为key,支持SpEL

keyGenerator:指定key的生成策略

condition:触发条件,满足条件就加入缓存,默认为空,表示全部都加入缓存,支持SpEL

@CacheEvict支持如下几个参数:

value:缓存位置的一段名称,不能为空

key:缓存的key,默认为空,表示使用方法的参数类型及参数值作为key,支持SpEL

condition:触发条件,满足条件就加入缓存,默认为空,表示全部都加入缓存,支持SpEL

allEntries:true表示清除value中的全部缓存,默认为false

测试代码

package hk.com.cre.process.basic.service.impl;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;

public class RdisCacheTest {

	/**
	 * 缓存test
	 * 缓存生成规则: conf:redis:类名方法名参数hashcode
	 * @param param1
	 * @param param2
	 * @return
	 * 
	 * 注意: @Cacheable注解生成的类型在redis中默认都是string
	 *      在每次请求的时候,都是先根据key到redis查询是否存在,如不存在则执行方法中的代码
	 */
	@Cacheable(value = "redis", keyGenerator = "cacheKeyGenerator")
	public String getRedisString(String param1, String param2) {
		return param1+":"+param2;
	}
	
	/**
	 * 清除缓存
	 * @return
	 */
	@CacheEvict(value = "redis", allEntries = true)
    public String cleanCache() {
        return "success";
    }
	
}

本章完结,待续!

本文说明:该文章属于原创,如需转载,请标明文章转载来源

猜你喜欢

转载自blog.csdn.net/p812438109/article/details/81631263