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

网站开发前途长沙企业建站

网站开发前途,长沙企业建站,中国建设工程安全协会网站,wordpress投稿者后台一、简介 1、缓存介绍 Spring 从 3.1 开始就引入了对 Cache 的支持。定义了 org.springframework.cache.Cache 和 org.springframework.cache.CacheManager 接口来统一不同的缓存技术。并支持使用 JCache#xff08;JSR-107#xff09;注解简化我们的开发。#xfeff; 其…一、简介 1、缓存介绍 Spring 从 3.1 开始就引入了对 Cache 的支持。定义了 org.springframework.cache.Cache 和 org.springframework.cache.CacheManager 接口来统一不同的缓存技术。并支持使用 JCacheJSR-107注解简化我们的开发。 其使用方法和原理都类似于 Spring 对事务管理的支持。Spring Cache 是作用在方法上的其核心思想是当我们在调用一个缓存方法时会把该方法参数和返回结果作为一个键值对存在缓存中。 2、Cache 和 CacheManager 接口说明 Cache 接口包含缓存的各种操作集合你操作缓存就是通过这个接口来操作的。Cache 接口下 Spring 提供了各种 xxxCache 的实现比如RedisCache、EhCache、ConcurrentMapCacheCacheManager 定义了创建、配置、获取、管理和控制多个唯一命名的 Cache。这些 Cache 存在于 CacheManager 的上下文中。 小结 每次调用需要缓存功能的方法时Spring 会检查指定参数的指定目标方法是否已经被调用过如果有就直接从缓存中获取方法调用后的结果如果没有就调用方法并缓存结果后返回给用户。下次调用直接从缓存中获取。 二、Cacheable 注解使用详细介绍 1、缓存使用步骤 Cacheable 这个注解用它就是为了使用缓存的。所以我们可以先说一下缓存的使用步骤 1、开启基于注解的缓存使用 EnableCaching 标识在 SpringBoot 的主启动类或配置类上。2、标注缓存注解即可① 第一步开启基于注解的缓存使用 EnableCaching 标注在 springboot 主启动类上 SpringBootApplication EnableCaching/**开启基于注解的缓存*/ Slf4j public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);log.info( 启动成功 );}}② 第二步标注缓存注解 Cacheable(value test,key #id)public String test(Integer id){System.out.println(经过这里);return 这是测试id;}注这里使用 Cacheable 注解就可以将运行结果缓存以后查询相同的数据直接从缓存中取不需要调用方法若数据过期需要调用方法查询结果返回并缓存 2、常用属性说明 下面介绍一下 Cacheable 这个注解常用的几个属性 cacheNames/value 用来指定缓存组件的名字key 缓存数据时使用的 key可以用它来指定。默认是使用方法参数的值。这个 key 你可以使用 spEL 表达式来编写keyGenerator key 的生成器。 key 和 keyGenerator 二选一使用cacheManager 可以用来指定缓存管理器。从哪个缓存管理器里面获取缓存。condition 可以用来指定符合条件的情况下才缓存unless 否定缓存。当 unless 指定的条件为 true 方法的返回值就不会被缓存。当然你也可以获取到结果进行判断。通过 #result 获取方法结果sync 是否使用异步模式。 cacheNames/value 用来指定缓存组件的名字将方法的返回结果放在哪个缓存中可以是数组的方式支持指定多个缓存。key 缓存数据时使用的 key。默认使用的是方法参数的值。可以使用 spEL 表达式去编写keyGenerator key 的生成器可以自己指定 key 的生成器通过这个生成器来生成 key 这样放入缓存中的 key 的生成规则就按照你自定义的 keyGenerator 来生成。不过需要注意的是 Cacheable 的属性key 和 keyGenerator 使用的时候一般二选一。condition 符合条件的情况下才缓存。方法返回的数据要不要缓存可以做一个动态判断。 unless 否定缓存。当 unless 指定的条件为 true 方法的返回值就不会被缓存。 sync 是否使用异步模式。默认是方法执行完以同步的方式将方法返回的结果存在缓存中。 前面说过缓存的 key 支持使用 spEL 表达式去编写下面总结一下使用 spEL 去编写 key 可以用的一些元数据 属性名称描述示例methodName当前方法名#root.methodNamemethod当前方法#root.method.nametarget当前被调用的对象#root.targettargetClass当前被调用的对象的class#root.targetClassargs当前方法参数组成的数组#root.args[0]caches当前被调用的方法使用的Cache#root.caches[0].name 3、CachePut和CacheEvict 注解使用 CachePut 将方法返回值存入到缓存中一般情况下是用在更新操作中并于Cacheable与CacheEvict配合使用CacheEvict 清除缓存值一般用在删除或更新操作中并于Cacheable与CachePut配合使用。并且在CacheEvict注解中多了两个参数 allEntries - 清除当前value下的所有缓存beforeInvocation - 在方法执行前清除缓存 示例代码示例如下 /**查询加载缓存*/Cacheable(value c, key 123)public String hello(String name) {System.out.println(name - name);return hello name;}/**更新缓存*/CachePut(value c, key 123)public String put() {return hello put;}/**删除缓存*/CacheEvict(value c, key 123)public String evict() {return hello put;}三、配置类结合Redis作为缓存 import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.cache.CacheManager; 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.RedisCacheConfiguration; 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 org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer;import java.time.Duration;/*** 缓存配置类* */ EnableCaching /**开启缓存注解*/ Configuration public class RedisConfig extends CachingConfigurerSupport {Beanpublic RedisTemplateString, Object redisTemplate(RedisConnectionFactory factory) {RedisTemplateString, Object template new RedisTemplate();RedisSerializerString redisSerializer new StringRedisSerializer();Jackson2JsonRedisSerializer jackson2JsonRedisSerializer new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);template.setConnectionFactory(factory);//key序列化方式template.setKeySerializer(redisSerializer);//value序列化template.setValueSerializer(jackson2JsonRedisSerializer);//value hashmap序列化template.setHashValueSerializer(jackson2JsonRedisSerializer);return template;}Beanpublic CacheManager cacheManager(RedisConnectionFactory factory) {RedisSerializerString redisSerializer new StringRedisSerializer();Jackson2JsonRedisSerializer jackson2JsonRedisSerializer new Jackson2JsonRedisSerializer(Object.class);/**解决查询缓存转换异常的问题*/ObjectMapper om new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);/**配置序列化解决乱码的问题*/RedisCacheConfiguration config RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(600))/**设置缓存默认过期时间600秒*/.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)).disableCachingNullValues();return RedisCacheManager.builder(factory).cacheDefaults(config).build();}} pom.xml需要Redis依赖: !-- redis --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactIdversion2.6.6/version/dependency纯属个人经验喜欢的可以点赞关注后续见
http://www.sadfv.cn/news/13205/

相关文章:

  • 网站开发时间表网站制作方案大全
  • dw做的网站如何发布wordpress删除文章
  • 网站空间支持功能快速提升网站排名
  • 公司网站建设费怎么做账惠州免费建站模板
  • asp网站有哪些怎么查公司网站可信度
  • 如何写网站优化方案做网站备案是个人还是企业好
  • 好看英文网站专业的网站建设公司排名
  • 新网互联的网站wordpress增加分类目录
  • 装修网站建设案例苏州百度快照优化排名
  • 大圣网站建设做网站的费用计入什么费用
  • 江西 网站 建设 开发大连云购物app下载安装到手机
  • 西安优化网站自己建设网站要花多少钱
  • 有做lol直播网站家具东莞网站建设
  • 网页设计网站免登陆网站优化公司哪家效果好
  • 做网站累吗网站建设知识库
  • 凡科网可以免费做网站吗crm系统是什么意思
  • 百度一般多久收录网站网页打不开但是有网什么原因如何解决
  • 高性能网站建设 下载公司门户网站建设方案
  • 官方网站拼多多wordpress寄出邮箱地址
  • 聊城高端网站制作南宁自助建站模板下载
  • nas可以做网站服务器吗大商创源码
  • 网站推广计划方案网站建设与管理需要什么软件有哪些方面
  • 南昌网站建设怎么样网站有订单了有声音提醒怎么做
  • 深圳优秀网站建设定制ip域名查询地址
  • 墨客网站建设xcyxqc根据颜色找网站
  • 校园网站建设年度总结营销网站制作多少钱
  • 做网站的人怎么联系微信网站设计制作
  • 泉州哪里做网站网站网站建设专业
  • 网站开发公司怎么查网站建设公司做前端
  • 外贸电商网站制作有域名可以自己做网站吗