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

如何查看自己制作的网站奶茶推广软文200字

如何查看自己制作的网站,奶茶推广软文200字,网站弹窗是怎么做的,网站主页模板目录 【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
    • 什么是缓存抽象
      • 声明式注解
      • JSR-107对应
      • SpEL上下文数据
    • 引入依赖
    • cache 支持的缓存类型
    • 缓存类型配置
      • NONE
      • SIMPLE
      • REDIS
        • 自定义配置
      • 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

什么是缓存抽象

  • 官网-缓存抽象
    使用翻译软件阅读
  • 主要阅读的知识点
  1. 缓存抽象的作用
  2. spring缓存的声明式注解
  3. spring缓存的生成key策略和如何自定义keyGenerator
  4. 缓存支持的后端类型

声明式注解

Spring 缓存注解说明
@Cacheable触发缓存填充。
@CacheEvict触发缓存退出。
@CachePut在不干扰方法执行的情况下更新缓存。
@Caching将多个缓存操作重新组合到一个方法上。
@CacheConfig在类级别共享一些常见的与缓存相关的设置。

JSR-107对应

Spring 缓存注解JSR-107备注
@Cacheable@CacheResult相当相似。 可以缓存特定的异常并强制 无论缓存的内容如何,都执行该方法。@CacheResult
@CachePut@CachePut当 Spring 使用方法调用的结果更新缓存时,JCache 要求将其作为注释的参数传递。 由于这种差异,JCache 允许在 实际方法调用。@CacheValue
@CacheEvict@CacheRemove相当相似。 支持有条件逐出,当 方法调用会导致异常。@CacheRemove
@CacheEvict(allEntries=true)@CacheRemoveAll看。@CacheRemove
@CacheConfig@CacheDefaults允许您以类似的方式配置相同的概念。

SpEL上下文数据

名称位置描述示例
methodNameroot对象当前被调用的方法名#root.methodname
methodroot对象当前被调用的方法#root.method.name
targetroot对象当前被调用的目标对象实例#root.target
targetClassroot对象当前被调用的目标对象的类#root.targetClass
argsroot对象当前被调用的方法的参数列表#root.args[0]
cachesroot对象当前方法调用使用的缓存列表#root.caches[0].name
Argument Name执行上下文当前被调用的方法的参数,如findArtisan(Artisan artisan),可以通过#artsian.id获得参数#artsian.id
result执行上下文方法执行后的返回值(仅当方法执行后的判断有效,如 unless cacheEvict的beforeInvocation=false)#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.1'username:port: 6379password:database: 1lettuce:pool:enabled: truemax-active: 8max-wait: 1000max-idle: 8connect-timeout: 5000
自定义配置

默认情况下会有两个模板类被注入Spring IoC供我们使用,需要个性化配置来满足实际的开发。

一个是RedisTemplate<Object, Object>,主要用于对象缓存,其默认使用JDK序列化,我们需要更改其序列化方式解决一些问题,比如Java 8日期问题JSON序列化问题。需要我们重写一下。

  1. RedisTemplate自定义配置
 @Beanpublic RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<Object, Object> template = new RedisTemplate<>();template.setConnectionFactory(factory);// 使用Jackson2JsonRedisSerialize 替换默认序列化Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = initJacksonSerializer();// 设置value的序列化规则和 key的序列化规则template.setValueSerializer(jackson2JsonRedisSerializer);template.setKeySerializer(new StringRedisSerializer());template.afterPropertiesSet();return template;}/*** 处理redis序列化问题* @return Jackson2JsonRedisSerializer*/private Jackson2JsonRedisSerializer<Object> 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);}
  1. 缓存管理器自定义配置
    使用Spring Cache做缓存的时候,有针对不同的key设置不同过期时间的场景。比如Jwt Token我想设置为一周过期,而验证码我想设置为五分钟过期。这个怎么实现呢?需要我们个性化配置RedisCacheManager。首先我通过枚举来定义这些缓存及其TTL时间

我们通过向Spring IoC分别注入RedisCacheConfigurationRedisCacheManagerBuilderCustomizer来个性化配置

/*** Redis cache configuration.** @param redisTemplate the redis template* @return the redis cache configuration*/@Beanpublic RedisCacheConfiguration redisCacheConfiguration(RedisTemplate<Object, 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://www.khdw.cn/news/40927.html

相关文章:

  • 苹果网站用flash做全网营销与seo
  • wordpress删除用户下所有文章seo网站地图
  • 网站魔板大全百度提交
  • 网页设计网站开发需要什么推广接单平台
  • 公司做网站找谁公司做网站找谁it培训机构排行榜
  • 胶州网站搭建公司宁波网站推广联系方式
  • 做网站好的公司关键词推广技巧
  • 中国做网站公司东莞seo建站哪家好
  • 如何更改网站关键词无锡营销型网站建设
  • 广西贵港建设集团有限公司网站seo内容优化是什么意思
  • 网站总体规划设计说明广州seo外包多少钱
  • 最吉利旺财的公司名字苏州首页排名关键词优化
  • 便宜购 网站建设私密浏览器免费版
  • 网站原则广州seo优化
  • 网站管理问题优化大师下载旧版本安装
  • 二维码生成器小程序免费版百度 seo 工具
  • 如何设计网站栏目重庆网站快速排名提升
  • 主要网站维护软件东莞疫情最新数据
  • 网站怎么做 流程图品牌全案策划
  • 做网站用什么编程语言好广东省广州市白云区
  • 网站建设策划执行省好多会员app
  • 有没有做.net面试题的网站seo内容优化是什么意思
  • 平台推广网站常见的系统优化软件
  • 重庆妇科医院推荐陕西seo
  • 微网站制作焦作整站优化
  • 济南正宗网站建设报价网站设计需要什么
  • 西安淘宝网站建设公司排名十堰seo
  • 外国做视频在线观看网站竞价排名营销
  • 网站功能列表小红书seo是什么意思
  • 莞城网站制作手机优化大师官网