spring RedisCacheManager 使用

原创
2020/12/09 19:24
阅读数 1.4K

Configuration 配置

package com.smartsanitation.report.config.cache;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import redis.clients.jedis.JedisPool;

@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
	
	@Value("${spring.redis.host}")
	private String host;
	
	@Value("${spring.redis.port}")
	private Integer port;
	
	@Value("${spring.redis.timeout}")
	private Integer timeOut;
	
	@Value("${spring.redis.password}")
	private String password;

    /**
     * 缓存配置管理器
     *
     * @param factory
     * @return
     */
    @Bean
    public RedisCacheManager cacheManager(RedisTemplate<?, ?> redisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
        //开启使用缓存名称为key的前缀
        cacheManager.setUsePrefix(true);
        //设置默认的过期时间 单位是秒
        cacheManager.setDefaultExpiration(timeOut);

        // 设置具体某一个cache名称缓存的过期时间
//        Map<String, Long> expires = new HashMap<>();
//        expires.put("people", 1000);
//        redisCacheManager.setExpires(expires);
        return cacheManager;
    }

    /**
     * RedisTemplate配置
     *
     * @param factory
     * @return
     */
    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
        redisTemplate.setConnectionFactory(factory);
        return redisTemplate;
    }

    /**
	 * @description 自定义的缓存key的生成策略 若想使用这个key
	 *              只需要讲注解上keyGenerator的值设置为keyGenerator即可</br>
	 * @return 自定义策略生成的key
	 */
//    @Bean()
//    public KeyGenerator keyGenerator() {
//        return new KeyGenerator() {
//            @Override
//            public Object generate(Object target, Method method, Object... params) {
//                String keyStr = method.getName()+"["+ Arrays.asList(params).toString() +"]";
//                return DigestUtils.sha256Hex(keyStr.toString());
//            }
//        };
//    }



    
    @Bean
    @ConditionalOnMissingBean
    public JedisPool redisPoolFactory() {
        GenericObjectPoolConfig config = new GenericObjectPoolConfig();
        config.setMaxWaitMillis(2 * 1000L);
        JedisPool jedisPool = new JedisPool(config, host, port, timeOut, password);
        return jedisPool;
    }
}

Service 使用

   @Cacheable(value = "groupStatService",key = "#proxyQueryModel")
    public List<GroupStatMetricModel> proxyQuery(ProxyQueryModel proxyQueryModel) {
        Map<String,String> inputDate =  getInputDateMap(proxyQueryModel.getDate());
        Integer customerId = proxyQueryModel.getCustomerId();

在redis 存储key的格式

192.168.0.119:6379[2]> keys groupStatService*
1) "groupStatService:\xac\xed\x00\x05sr\x004com.smartsanitation.report.model.ext.ProxyQueryModel\xe4O\xe0\r\xab6s\xfa\x02\x00\x03L\x00\ncustomerIdt\x00\x13Ljava/lang/Integer;L\x00\x0ccustomerTypet\x00\x12Ljava/lang/String;L\x00\x04dateq\x00~\x00\x02xpsr\x00\x11java.lang.Integer\x12\xe2\xa0\xa4\xf7\x81\x878\x02\x00\x01I\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\x05\xb3t\x00\nefficiencyt\x00\a2020-11"

相关资料

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部