package com.atguigu.cache.config;

import com.atguigu.cache.bean.Employee;
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 org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import java.rmi.UnknownHostException;

@Configuration
public class MyRedisConfig {

    @Bean("mykeyGenerator")
    public RedisTemplate<Object, Employee> empRedisTemplate(
            RedisConnectionFactory redisConnectionFactory)
            throws UnknownHostException {
        RedisTemplate<Object, Employee> template = new RedisTemplate<Object, Employee>();
        template.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer<Employee> ser = new Jackson2JsonRedisSerializer<Employee>(Employee.class);
        template.setDefaultSerializer(ser);

        return template;
    }


    //CacheManagerCustomizers可以定制缓存一些规则
    @Bean
    public RedisCacheManager employeeCacheManager(RedisTemplate<Object, Employee> empRedisTemplate){
        RedisCacheManager cacheManager = new RedisCacheManager(empRedisTemplate);
        //key多了一个前缀
        //使用前缀,默认会将CacheName作为key的前缀
        cacheManager.setUsePrefix(true);
        return cacheManager;
    }

}
最近下载更多
手捧阳光  LV6 2022年12月2日
小白啰  LV1 2022年9月6日
crosa_Don  LV18 2022年6月2日
clcl55  LV1 2022年4月13日
wanlingaaa  LV1 2022年1月4日
11b23344  LV1 2021年3月12日
kinglong  LV16 2020年8月9日
3969138  LV15 2019年9月17日
打手犯规了  LV1 2019年9月5日
A_xiaobao  LV9 2019年8月17日
最近浏览更多
SILKYYY 5月25日
暂无贡献等级
qis119 5月9日
暂无贡献等级
微信网友_6263159342649344  LV7 2022年12月16日
总有人间一两风  LV8 2022年12月10日
手捧阳光  LV6 2022年12月2日
Zeorwyc  LV8 2022年4月22日
clcl55  LV1 2022年4月13日
crosa_Don  LV18 2022年3月31日
zhang200hlo  LV7 2022年2月14日
woaini12788  LV7 2022年1月18日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友