当前位置: 首页 > news >正文

东莞网站优化服务公司郑州居家办公全员核酸

东莞网站优化服务公司,郑州居家办公全员核酸,十大跨境电商公司排名,宁波网站建设怎么做一、简介 缓存介绍 缓存#xff0c;在我们的日常开发中用的非常多#xff0c;是我们应对各种性能问题支持高并发的一大利器。 Spring 从 3.1 开始就引入了缓存的支持。定义了如下两个接口来统一支持不同的缓存技术。 org.springframework.cache.Cacheorg.springframework.ca…一、简介 缓存介绍 缓存在我们的日常开发中用的非常多是我们应对各种性能问题支持高并发的一大利器。 Spring 从 3.1 开始就引入了缓存的支持。定义了如下两个接口来统一支持不同的缓存技术。 org.springframework.cache.Cacheorg.springframework.cache.CacheManager 我们熟知的缓存有堆缓存Ehcache3.x、Guava Cache、Caffeine等、堆外缓存Ehcache3.x、MapDB等、分布式缓存Redis、Memcached等等等。 常用的缓存注解EnableCaching、Cacheable、CachePut、CacheEvict、 Cache 和 CacheManager 接口说明 Cache 接口包含缓存的各种操作集合你操作缓存就是通过这个接口来操作的。Cache 接口下 Spring 提供了各种 xxxCache 的实现比如RedisCache、EhCache、ConcurrentMapCache等。CacheManager 定义了创建、配置、获取、管理和控制多个唯一命名的 Cache。这些 Cache 存在于 CacheManager 的上下文中。 二、缓存实战 1.开启缓存 在 SpringBoot 的启动类上添加注解EnableCaching。 2.Cacheable Cacheable 的作用 主要针对方法配置能够根据方法的请求参数对其结果进行缓存。 常用属性 cacheNames、value用来指定缓存组件名称。 Cacheable(cacheNames users, key#id) public User getUser(Integer id) {}key缓存数据的 key可以用它来指定。默认使用所有参数的值进行组合。key可以使用 spEL 表达式来编写。 Cacheable(cacheNames usersBySpEL, key#root.methodName [ #id ]) public User getUserBySpEL(Integer id) {}keyGeneratorkey 的生成器。key 和 keyGenerator 二选一使用。 Cacheable(cacheNames userByKeyGenerator, keyGenerator myKeyGenerator) public User getUserByKeyGenerator(Integer id) {}condition指定符合条件的情况下才缓存。 Cacheable(cacheNames userByCondition, condition #id 1) public User getUserByCondition(Integer id) {}unless指定不符合条件的情况下才缓存。可以获取到结果进行判断通过 #result 获取方法结果unless汉语意思除非指会缓存除了。。。之外。 Cacheable(cacheNames userByUnless, unless #id 1) public User getUserByUnless(Integer id) {}sync是否使用异步模式。 3.CachePut CachePut 的作用 主要针对配置能够根据方法的请求参数对其结果进行缓存。 区别于 Cacheable它每次都会触发真实方法的调用可以保证缓存的一致性。属性与 Cacheable 类同。 CachePut(cacheNames users , key #user.id) public User addUser(User user) {}4.CacheEvict CacheEvict 的作用 主要针对方法配置能够根据一定的条件对缓存进行清空。 常用属性 cacheNames、value用来指定缓存组件名称。key缓存数据的 key可以用它来指定。默认使用所有参数的值进行组合。key可以使用 spEL 表达式来编写。condition指定符合条件的情况下的缓存。allEntries是否清空所有缓存缺省为false。beforeInvocation是否在方法执行前就清空缺省为false缺省情况下如果方法执行抛异常则不会清空缓存。 CacheEvict(cacheNames users, key #id) public void delUserCache(Integer id) {}5.CacheConfig CacheConfig 的作用 主要针对类配置能够设置当前类中 Cacheable 的 value 属性默认值。当然如果 Cacheable 设置了 value还是以设置的值为准。 常用属性 cacheNames 指定缓存名称默认值。 6.Caching Caching 的作用 主要针对方法配置能够组合多个Cache注解。比如用户新增成功后我们可能需要添加 id - user、username - user、email - user 的缓存此时就需要 Caching 组合多个注解标签了。 常用属性 cacheable组合多个 Cacheable 注解put组合多个 CachePut 注解evict组合多个 CacheEvict 注解 CacheConfig(cacheNames users) public class CacheTestServiceImpl implements CacheTestService {/*** Cacheable 的 cacheNames 默认为 users*/Cacheable(key#id)public User getUser(Integer id) {...} }7.自定义缓存过期时间 7.1 设置全局默认缓存过期时间 // 提供默认的cacheManager应用于全局实现存活2天BeanPrimarypublic CacheManager defaultCacheManager(RedisTemplate?, ? redisTemplate) {RedisCacheWriter writer RedisCacheWriter.lockingRedisCacheWriter(redisTemplate.getConnectionFactory());RedisCacheConfiguration config RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(2));return new RedisCacheManager(writer, config);}7.2 定制部分缓存过期时间 定制缓存过期时间需要自定义RedisCacheManager来实现ttl设置。注意项目中如已配置了RedisCacheManager需要在原配置的bean上添加注解 Primary以免造成干扰 /**自定义RedisCacheManager用于在使用Cacheable时设置ttl*/Beanpublic RedisCacheManager selfCacheManager(RedisTemplateString, Object redisTemplate) {RedisCacheWriter redisCacheWriter RedisCacheWriter.nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory());RedisCacheConfiguration redisCacheConfiguration RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getValueSerializer()));return new SelfRedisCacheManager(redisCacheWriter, redisCacheConfiguration);}SelfRedisCacheManager.java public class SelfRedisCacheManager extends RedisCacheManager {public SelfRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) {super(cacheWriter, defaultCacheConfiguration);}Overrideprotected RedisCache createRedisCache(String name, RedisCacheConfiguration cacheConfig) {String[] cells StringUtils.delimitedListToStringArray(name, );name cells[0];if (cells.length 1) {long ttl Long.parseLong(cells[1]);// 根据传参设置缓存失效时间默认单位是秒cacheConfig cacheConfig.entryTtl(Duration.ofSeconds(ttl));}return super.createRedisCache(name, cacheConfig);} } 使用 // value、cacheNames是等效的,ttl600sunless是不缓存的结果为null时不缓存 Cacheable(value p_user600key #menu_#type_#userIdcacheManager selfCacheManager, unless #result null public User getUser(...){xxx }// 当前方法执行时对应的key失效,也可以用CachePut在当前方法执行时更新key CacheEvict(cacheNames p_userkey #p.menu_#p.type_#p.user public boolean setUser(User p){xxx } 三、spEL表达式 参考地址 1.Cacheable设置过期时间https://blog.csdn.net/weixin_41860719/article/details/125226096
http://www.huolong8.cn/news/431659/

相关文章:

  • 网站建设公司简介广告发布费属于什么服务
  • 东莞阳光网站官网张家港建网站
  • 温州市微网站制作电话wordpress怎样加入代码
  • 服务关系型网站怎么做给企业做免费的推广
  • 企业网站关键词应如何优化北碚免费建站哪家做得好
  • 网站建设与管理实训报告总结做项目接任务的网站
  • 舒兰网站建设快速进入网站
  • 网站案例演示婚礼策划
  • 织梦网站手机版怎么做罗湖做网站公司排名
  • 网站打不开404app免费制作平台下载
  • 360浏览网页入口重庆百度快速优化
  • 冠县网站建设多少钱做视频用的网站
  • 平凉市市建设局网站企业163邮箱登录入口
  • 个人怎么做ipv6的网站在线设计签名免费艺术签名
  • 做外贸网站需要请外贸文员吗.net 获取网站域名
  • 常州模板建站哪家好asp.net网站开发全过程
  • 关于icp备案信息中注销网站的通知广告公司网站制作
  • 广州市住房和建设水务局网站网站关键词快速排名
  • 网站建设案例典型企业案例遵义在线招聘网
  • 福鼎手机网站建设在哪个国家做垂直网站好
  • 做统计图的网站如何用python开发网页
  • 楚雄市住房和城乡建设局网站企业建站系统 哪个好
  • 门户网站开发投标文件.docrss 网站插件
  • 网页制作app下载四川成都网站优化
  • 医院网站建设的要求广州关键词seo
  • 湖南省住建厅官方网站建设干校摄影网站定位
  • 网站建设推广专家服务集团网站建设的要求
  • 一个专门做字画的网站企业管理公司取名字大全
  • 需要手机端网站建设的企业网站开发进度缓慢
  • 揭阳关键词网站排名wordpress备案信息链接