本文实例讲述了弹簧启动注解方式使用复述,缓存操作。分享给大家供大家参考,具体如下:
在pom中引入依赖库,如下
& lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-starter-data-redis & lt;/dependency> & lt; dependency> & lt; groupId> redis.clients & lt; artifactId> jedis & lt;/dependency> >之前
@Cacheable @Cacheable(“产品”) @Cacheable (value=https://www.yisu.com/zixun/{“产品”,“秩序”},关键=" # root.targetClass +“-”+ # id”) @Cacheable(值=" https://www.yisu.com/zixun/product "键=" # root.targetClass +“-”+ # id”) >之前自定义cacheManager
@Cacheable(值=" https://www.yisu.com/zixun/product "键=" #根。targetClass +“-”+ # id cacheManager“cacheManager=) @CachePut >之前应用到写数据的方法上,如新增/修改方法
@CachePut(值=" https://www.yisu.com/zixun/product "键=" # root.targetClass +“-”+ # product.id”) @CacheEvict >之前即应用到移除数据的方法上,如删除方法
@CacheEvict(值=" https://www.yisu.com/zixun/product "键=" # root.targetClass +“-”+ # id”) >之前提供的?上下文数据
春缓存提供了一些供我们使用的?上下文数据,下表直接摘自春官方文档:
名字 位置 描述 示例 methodName 根对象 当前被调用的方法名 # root.methodName 方法 根对象 当前被调用的方法 # root.method.name 目标 根对象 当前被调用的目标对象 # root.target targetClass 根对象 当前被调用的目标对象类 # root.targetClass arg游戏 根对象 当前被调用的方法的参数列表 # root.args [0] 缓存 根对象 当前方法调用使用的缓存列表(如@Cacheable (value=https://www.yisu.com/zixun/{“cache1”、“cache2”})),则有两个缓存 # root.caches [0] . name 参数名称 执行上下文 当前被调用的方法的参数,如findById (id),我们可以通过#身份证拿到参数 # user.id 结果 执行上下文 方法执行后的返回值(仅当方法执行之后的判断有效,如“除非”、“缓存驱逐”的beforeInvocation=false) #结果
@ configuration @EnableCaching 公开课RedisConfig延伸CachingConfigurerSupport {/* * *自定义复述,键值生成策略 */@ bean @Override 公共KeyGenerator KeyGenerator () { 返回(目标、方法、参数)→{ StringBuilder某人=new StringBuilder (); sb.append (target.getClass () . getname ()); sb.append (method.getName ()); (对象obj: params) { sb.append (obj.toString ()); } 返回sb.toString (); }; } @ bean 公共RedisTemplate<字符串,String>redisTemplate (RedisConnectionFactory工厂){ objectmap om=new objectmap (); om.setVisibility (PropertyAccessor。所有,JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(对象 >之前更多关于java相关内容感兴趣的读者可查看本站专题:《春天框架入门与进阶教程》、《java数据结构与算法教程》、《java操作DOM节点技巧总结》,《java文件与目录操作技巧汇总》和《java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
弹簧启动注解方式使用复述,缓存操作示例