测试缓存:
原理:
CacheManager===Cache 缓存组件来实际给缓存中存储数据
1,引入redis的starter,容器中保存的是RedisCacheManager
2,RedisCacheManager 帮我们创建RedisCache 来作为缓存组件;
RedisCache通过操作redis来缓存数据
3,默认保存数据 K -V 都是通过序列化来保存的;

关于能存储redis。第二次查不能反序列化出来的问题。
原因:存的是dept的缓存数据,而CacheManager默认使用RedisTemplate<Object, Employee>来操作redis
解决方法:
自定义CacheManager:
1> 引入了redis的starter,cacheManager变为RedisCacheManager
2> 默认创建的RedisCacheManager 操作redis的时候使用的是 RedisTemlate<Object,Object>
3> RedisTemlate<Object,Object> 是默认使用jdk序列化机制
4> 自定义CacheManager

@Configuration
public class MyRedisConfig { //员工缓存
@Bean
public RedisTemplate<Object, Employee> empRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Employee> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer<Employee> ser = new Jackson2JsonRedisSerializer<Employee>(Employee.class);
template.setDefaultSerializer(ser);
return template;
} //员工缓存
//CacheManagerCustomizers 可以定制缓存的一些规则
@Bean
@Primary //默认缓存管理器 必须得有默认
public RedisCacheManager employeeCacheManager(RedisTemplate<Object, Employee> empRedisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(empRedisTemplate);
//key多了一个前缀 //使用前缀,默认把cacheName作为前缀
cacheManager.setUsePrefix(true);
return cacheManager;
} //部门缓存
@Bean
public RedisTemplate<Object, Department> deptRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Department> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer<Department> ser = new Jackson2JsonRedisSerializer<Department>(Department.class);
template.setDefaultSerializer(ser);
return template;
} //部门缓存
@Bean
public RedisCacheManager deptCacheManager(RedisTemplate<Object, Department> deptRedisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(deptRedisTemplate);
cacheManager.setUsePrefix(true);
return cacheManager;
}
}
  1. @Primary 当自定义两个CacheManager的时候。必须指定一个为默认的
  2. 针对不同的service可以直接标注CacheManager,同时如果有默认指定,可以省略不写
    @CacheConfig(cacheNames = "dept",cacheManager = "deptCacheManager")
    @Service
    public class DeptService {
    @CacheConfig(cacheNames = "emp"/*,cacheManager = "employeeCacheManager"*/)
    @Service
    public class EmployeeService {
  3. 以上都是注解形式的缓存,编码形式的缓存写法:
       //方法内,编码方式做缓存
    @Qualifier("deptCacheManager")
    @Autowired
    RedisCacheManager deptCacheManager; //在方法内做缓存写法
    public Department getDeptById(Integer id){
    System.out.println("查询部门id为"+id+"...........");
    Department dept = departmentMapper.getDeptById(id);
    Cache deptCache=deptCacheManager.getCache("dept");//相当于 @Cacheable(cacheNames = "dept")
    deptCache.put("dept:1",dept);
    return dept;
    }

Redis缓存之自定义CacheManager的更多相关文章

  1. SpringBoot缓存管理(三) 自定义Redis缓存序列化机制

    前言 在上一篇文章中,我们完成了SpringBoot整合Redis进行数据缓存管理的工作,但缓存管理的实体类数据使用的是JDK序列化方式(如下图所示),不便于使用可视化管理工具进行查看和管理. 接下来 ...

  2. 1.2_springboot2.x中redis缓存&原理介绍

    1.整合redis作为缓存 说明这里springboot版本2.19 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. 它支持多种类型的数据结构 ...

  3. Spring Boot自定义Redis缓存配置,保存value格式JSON字符串

    Spring Boot自定义Redis缓存,保存格式JSON字符串 部分内容转自 https://blog.csdn.net/caojidasabi/article/details/83059642 ...

  4. ssm+redis 如何更简洁的利用自定义注解+AOP实现redis缓存

    基于 ssm + maven + redis 使用自定义注解 利用aop基于AspectJ方式 实现redis缓存 如何能更简洁的利用aop实现redis缓存,话不多说,上demo 需求: 数据查询时 ...

  5. 【Azure Redis 缓存】由Azure Redis是否可以自定义密码而引申出Azure PaaS的Redis服务是否可以和自建的Redis进行主从配置呢?

    问题描述 在自建的Redis服务中,可以通过 config set requirepass <Password> 设置Redis的登录密码,然后使用auth 命令输入密码.操作命令如下: ...

  6. AbpVnext使用分布式IDistributedCache Redis缓存(自定义扩展方法)

    AbpVnext使用分布式IDistributedCache缓存from Redis(带自定义扩展方法) 我的依赖包的主要版本以及Redis依赖如下 1:添加依赖 <PackageReferen ...

  7. Redis 缓存 + Spring 的集成示例(转)

    <整合 spring 4(包括mvc.context.orm) + mybatis 3 示例>一文简要介绍了最新版本的 Spring MVC.IOC.MyBatis ORM 三者的整合以及 ...

  8. ABP入门系列(13)——Redis缓存用起来

    ABP入门系列目录--学习Abp框架之实操演练 源码路径:Github-LearningMpaAbp 1. 引言 创建任务时我们需要指定分配给谁,Demo中我们使用一个下拉列表用来显示当前系统的所有用 ...

  9. springboot redis 缓存对象

    只要加入spring-boot-starter-data-redis , springboot 会自动识别并使用redis作为缓存容器,使用方式如下 gradle加入依赖 compile(" ...

随机推荐

  1. js优化 前端小白适用

    注意啦,前端初学者适合看的js优化,当你看我的优化认为太low,那么恭喜,你已经脱离初学者了. 首先这边我觉得分享的还是以js为主,前端性能优化,我认为最重要的还是js,因为js是一门解释型的语言,相 ...

  2. 关于Linux目录结构的理解

    dUI与刚接触Linux的学习者来说,那么多的根下目录足够让我们头疼不已,如下图: 那么对于初学者来说,我们首要了解的是哪些目录呢?  就是这个标黄绿色的tmp目录,此目录是一个存放临时文件夹的目录( ...

  3. archlinux中安装Oracle12c的过程中遇到的问题

    INFO: : cannot find INFO: /usr/lib64/libpthread_nonshared.aINFO: INFO: genclntsh: Failed to link lib ...

  4. sql 生成随机字符串

    生成三位随机字母+12位数字 ),), @c int; select @CardCode=abs(CHECKSUM(NEWID())) -LEN(@CardCode); ,@c)) set @Card ...

  5. 初识JDBC

    1,概念: JDBC(Java DateBase Connective) ,即利用Java语言操作数据库语言 2,示例:Mysql中的JDBC 1,新建一个Dynamic Web Projectx项目 ...

  6. noVNC支持手机自带键盘输入

    代码修改说明 novnc的web链接类似为:http://192.168.1.177:6080/vnc_auto.html?token=105356fa-bbe3-43e4-a0ce-7703dc42 ...

  7. C#-----线程安全的ConcurrentQueue<T>队列

     ConcurrentQueue<T>队列是一个高效的线程安全的队列,是.Net Framework 4.0,System.Collections.Concurrent命名空间下的一个数据 ...

  8. CLASS 类 __getattr__

    class Chain(object): def __init__(self, path=''): self._path = path def __getattr__(self, path): ret ...

  9. CCF CSP 201809-2 买菜

    题目链接:http://118.190.20.162/view.page?gpid=T78 问题描述 小H和小W来到了一条街上,两人分开买菜,他们买菜的过程可以描述为,去店里买一些菜然后去旁边的一个广 ...

  10. mysql 批量导入

    load data LOCAL infile 'D:/user.txt' into table userssFIELDS TERMINATED BY ',' LINES TERMINATED BY ' ...