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

个人网站模板建站网站吸流量

个人网站模板建站,网站吸流量,襄阳网站建设企业,北京品牌网站定制公司目录 【7】Spring Boot 3 集成组件#xff1a;缓存组件 spring cache spring data redis什么是缓存抽象声明式注解JSR-107对应SpEL上下文数据 引入依赖cache 支持的缓存类型缓存类型配置NONESIMPLEREDIS自定义配置 CAFFEINE Hazelcast...总结 个人主页: 【⭐️个人主页】 需要… 目录 【7】Spring Boot 3 集成组件缓存组件 spring cache spring data redis什么是缓存抽象声明式注解JSR-107对应SpEL上下文数据 引入依赖cache 支持的缓存类型缓存类型配置NONESIMPLEREDIS自定义配置 CAFFEINE Hazelcast...总结 个人主页: 【⭐️个人主页】 需要您的【 点赞关注】支持 【7】Spring Boot 3 集成组件缓存组件 spring cache spring data redis 本文核心知识点 spring cache 抽象和注解 cache 支持的缓存类型 spring cache spring data redis 配置 redis序列化 本章使用的spring版本 spring boot version: 3.1.5 什么是缓存抽象 官网-缓存抽象 使用翻译软件阅读主要阅读的知识点 缓存抽象的作用spring缓存的声明式注解spring缓存的生成key策略和如何自定义keyGenerator缓存支持的后端类型 声明式注解 Spring 缓存注解说明Cacheable触发缓存填充。CacheEvict触发缓存退出。CachePut在不干扰方法执行的情况下更新缓存。Caching将多个缓存操作重新组合到一个方法上。CacheConfig在类级别共享一些常见的与缓存相关的设置。 JSR-107对应 Spring 缓存注解JSR-107备注CacheableCacheResult相当相似。 可以缓存特定的异常并强制 无论缓存的内容如何都执行该方法。CacheResultCachePutCachePut当 Spring 使用方法调用的结果更新缓存时JCache 要求将其作为注释的参数传递。 由于这种差异JCache 允许在 实际方法调用。CacheValueCacheEvictCacheRemove相当相似。 支持有条件逐出当 方法调用会导致异常。CacheRemoveCacheEvict(allEntriestrue)CacheRemoveAll看。CacheRemoveCacheConfigCacheDefaults允许您以类似的方式配置相同的概念。 SpEL上下文数据 名称位置描述示例methodNameroot对象当前被调用的方法名#root.methodnamemethodroot对象当前被调用的方法#root.method.nametargetroot对象当前被调用的目标对象实例#root.targettargetClassroot对象当前被调用的目标对象的类#root.targetClassargsroot对象当前被调用的方法的参数列表#root.args[0]cachesroot对象当前方法调用使用的缓存列表#root.caches[0].nameArgument Name执行上下文当前被调用的方法的参数如findArtisan(Artisan artisan),可以通过#artsian.id获得参数#artsian.idresult执行上下文方法执行后的返回值仅当方法执行后的判断有效如 unless cacheEvict的beforeInvocationfalse#result 引入依赖 implementation org.springframework.boot:spring-boot-starter-cache添加启用缓存注解EnableCaching SpringBootApplication(scanBasePackages {com.kongxiang}) EnableAutoConfiguration EnableCaching public class StudySpring3Application {public static void main(String[] args) {SpringApplication.run(StudySpring3Application.class, args);} }cache 支持的缓存类型 spring cache支持的缓存类型 查看类org.springframework.boot.autoconfigure.cache.CacheType public enum CacheType {/*** Generic caching using Cache beans from the context.*/GENERIC,/*** JCache (JSR-107) backed caching.*/JCACHE,/*** Hazelcast backed caching.*/HAZELCAST,/*** Couchbase backed caching.*/COUCHBASE,/*** Infinispan backed caching.*/INFINISPAN,/*** Redis backed caching.*/REDIS,/*** Cache2k backed caching.*/CACHE2K,/*** Caffeine backed caching.*/CAFFEINE,/*** Simple in-memory caching.*/SIMPLE,/*** No caching.*/NONE}缓存类型配置 NONE 无缓存 spring:cache:type: NONE SIMPLE 内存缓存 spring:cache:type: SIMPLE REDIS Redis 缓存 spring:cache:type: REDISdata:redis:host: 127.0.0.1username:port: 6379password:database: 1lettuce:pool:enabled: truemax-active: 8max-wait: 1000max-idle: 8connect-timeout: 5000 自定义配置 默认情况下会有两个模板类被注入Spring IoC供我们使用需要个性化配置来满足实际的开发。 一个是RedisTemplateObject, Object主要用于对象缓存其默认使用JDK序列化我们需要更改其序列化方式解决一些问题比如Java 8日期问题、JSON序列化问题。需要我们重写一下。 RedisTemplate自定义配置 Beanpublic RedisTemplateObject, Object redisTemplate(RedisConnectionFactory factory) {RedisTemplateObject, Object template new RedisTemplate();template.setConnectionFactory(factory);// 使用Jackson2JsonRedisSerialize 替换默认序列化Jackson2JsonRedisSerializerObject jackson2JsonRedisSerializer initJacksonSerializer();// 设置value的序列化规则和 key的序列化规则template.setValueSerializer(jackson2JsonRedisSerializer);template.setKeySerializer(new StringRedisSerializer());template.afterPropertiesSet();return template;}/*** 处理redis序列化问题* return Jackson2JsonRedisSerializer*/private Jackson2JsonRedisSerializerObject initJacksonSerializer() {ObjectMapper om new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);//以下替代旧版本 om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);om.activateDefaultTyping(om.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);//bugFix Jackson2反序列化数据处理LocalDateTime类型时出错om.disable(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS);// java8 时间支持om.registerModule(new JavaTimeModule());return new Jackson2JsonRedisSerializer(om,Object.class);}缓存管理器自定义配置 使用Spring Cache做缓存的时候有针对不同的key设置不同过期时间的场景。比如Jwt Token我想设置为一周过期而验证码我想设置为五分钟过期。这个怎么实现呢需要我们个性化配置RedisCacheManager。首先我通过枚举来定义这些缓存及其TTL时间 我们通过向Spring IoC分别注入RedisCacheConfiguration和RedisCacheManagerBuilderCustomizer来个性化配置 /*** Redis cache configuration.** param redisTemplate the redis template* return the redis cache configuration*/Beanpublic RedisCacheConfiguration redisCacheConfiguration(RedisTemplateObject, Object redisTemplate, CacheProperties cacheProperties) {// 参见 spring.cache.redisCacheProperties.Redis redisProperties cacheProperties.getRedis();RedisCacheConfiguration redisCacheConfiguration RedisCacheConfiguration.defaultCacheConfig()// 缓存的序列化问题.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getValueSerializer()));if (redisProperties.getTimeToLive() ! null) {// 全局 TTL 时间redisCacheConfiguration redisCacheConfiguration.entryTtl(redisProperties.getTimeToLive());}if (redisProperties.getKeyPrefix() ! null) {// key 前缀值redisCacheConfiguration redisCacheConfiguration.prefixCacheNameWith(redisProperties.getKeyPrefix());}if (!redisProperties.isCacheNullValues()) {// 默认缓存null值 可以防止缓存穿透redisCacheConfiguration redisCacheConfiguration.disableCachingNullValues();}if (!redisProperties.isUseKeyPrefix()) {// 不使用key前缀redisCacheConfiguration redisCacheConfiguration.disableKeyPrefix();}return redisCacheConfiguration;}/*** Redis cache manager 个性化配置缓存过期时间.** return the redis cache manager builder customizer*/Beanpublic RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer(RedisCacheConfiguration redisCacheConfiguration) {return builder - builder.cacheDefaults(redisCacheConfiguration);}CAFFEINE 引入依赖 implementation com.github.ben-manes.caffeine:caffeine:3.1.8修改配置 spring:cache:type: CAFFEINE直接启动即可 Hazelcast … 总结 spring cache 缓存抽象加上spring data包和spring boot autoconfig 配置包的能力可以快速接入一个具体的缓存实现。redis是我们公司基本使用的缓存策略。所以针对redis的一些自定义配置通过 java bean的方式实现。着重强调一下。
http://wiki.neutronadmin.com/news/265741/

相关文章:

  • .net 微信网站开发网站域名备案流程
  • 在线教育网站有什么程序做青岛网站推广方案
  • 邳州做网站pzwodewordpress漫画主题
  • 网站防恶意注册招生网站建设方案
  • 江门企业做网站做网站超链接用什么软件
  • 个人网站备案都需要什么网站标签名词
  • 维护一个网站的费用外包软件公司在哪里去接项目
  • 沈阳做网站有名公司有哪些企业网站seo数据
  • 婚恋网站女代我做彩票shopnc本地生活o2o网站系统
  • 雷山网站建设aso推广公司
  • 公司三站合一的网站免费wordpress导购主题
  • 遵义做百度网站一年多少钱济南做网站建设公司
  • 南昌自助建站模板wordpress多个page
  • 本科专业 网站开发网站开发后台需要做什么
  • 网站开发接口文档模板ui设计风格有哪几种
  • 开发网站需要什么技术2022企业服务网
  • 岳阳网站开发公司东莞公司注册地址查询
  • 如何制作网站视频教程网站怎么更新内容
  • 上海做网站的公司官网wordpress 获取分类名称
  • 宝塔配置wordpress主题网站优化比较好用的软件
  • 什么建站程序最利于seo做女装的看哪个网站好
  • 商城网站建设code521python基础教程 入门教程
  • 做网站程序看什么书wordpress接入支付宝
  • 纯html网站模板江苏有什么网站找工程建设人员
  • 个人网站可以备案了吗学计算机网站建设
  • 浪琴手表网站建设图北京网站优化推广分析
  • 免费做简单网站企业培训考试
  • 网站建设h5渭南建设用地规划查询网站
  • 兰州建设局网站公告网站跟app的区别是什么
  • 深圳公众号开发公司百度网站优化软件