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

义乌系统开发东莞seo优化排名

义乌系统开发,东莞seo优化排名,wordpress新文章无法,苏州公司网站建设报价更多SpringBoot3内容请关注我的专栏:《SpringBoot3》 期待您的点赞👍收藏⭐评论✍ 重学SpringBoot3-集成Redis(二)之注解驱动 1. 为什么选择 Redis 作为缓存?2. 如何在 Spring Boot 中启用 Redis 缓存?2.1 …

更多SpringBoot3内容请关注我的专栏:《SpringBoot3》
期待您的点赞👍收藏⭐评论✍

重学SpringBoot3-集成Redis(二)之注解驱动

  • 1. 为什么选择 Redis 作为缓存?
  • 2. 如何在 Spring Boot 中启用 Redis 缓存?
    • 2.1 添加 Redis 依赖
    • 2.2 配置 Redis 连接
    • 2.3 启用缓存支持
  • 3. 注解驱动的缓存机制
    • 3.1 @Cacheable示例
    • 3.2 @CachePut示例
    • 3.3 @CacheEvict示例
  • 4. 自定义缓存管理
    • 4.1 RedisCacheConfiguration 类
      • 4.1.1. 过期时间(TTL - Time To Live)
      • 4.1.2. 键序列化方式
      • 4.1.3. 值序列化方式
      • 4.1.4. 禁用缓存空值(Disable Caching Null Values)
      • 4.1.5. 使用前缀(Use Cache Key Prefixes)
      • 4.1.6. 设置空闲时间(Idle Time)
      • 4.1.7. 组合多种配置
      • 4.1.8. 设置自定义的过期策略
    • 4.2 自定义缓存配置
  • 5. Redis 缓存的常见问题和优化建议
  • 6. 总结

Spring Boot 提供了对缓存的简便支持,使得开发者能够通过简单的注解实现缓存操作,减少重复代码的编写。本文将详细介绍如何在 Spring Boot 3 中使用 Redis 作为缓存,并通过注解驱动的方式进行缓存操作。

1. 为什么选择 Redis 作为缓存?

Redis 是一个高效的键值对存储系统,特别适合于构建高性能、可扩展的缓存层。其优点包括:

  • 高吞吐量:Redis 使用内存作为存储介质,读取和写入性能极快,能够支撑高并发的访问需求。
  • 数据持久化:尽管 Redis 是内存数据库,它也支持将数据持久化到磁盘,防止数据丢失。
  • 丰富的数据结构:Redis 不仅支持简单的字符串存储,还支持哈希、列表、集合等丰富的数据结构,适用于多种应用场景。
  • 易于扩展:通过 Redis 的集群功能,可以很容易地扩展 Redis 实例,处理更大规模的数据和请求。

2. 如何在 Spring Boot 中启用 Redis 缓存?

Spring Boot 提供了对缓存的开箱即用支持,开发者只需简单配置即可使用。具体参考上一章 重学SpringBoot3-集成Redis(一)。

2.1 添加 Redis 依赖

pom.xml 中引入 Redis 和 Spring Cache 相关依赖:

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency>

2.2 配置 Redis 连接

application.yml 中,配置 Redis 服务器地址及相关连接池配置:

spring:cache:type: redis     	 	# 使用 Redis 作为缓存类型data:redis:host: localhostport: 6379            # Redis 端口password: 			# 如果有密码可以在这里配置lettuce:pool:max-active: 100    # 最大并发连接数max-idle: 50       # 最大空闲连接数min-idle: 10       # 最小空闲连接数

2.3 启用缓存支持

在 Spring Boot 项目中,使用 @Cacheable 注解前,需要通过 @EnableCaching 注解启用缓存功能。可以在主应用类或者任何配置类中加上这个注解:

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@EnableCaching
public class MyApplication {public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);}
}

3. 注解驱动的缓存机制

Spring 提供了一组注解用于操作缓存,这些注解可以直接应用于方法上,使得代码更简洁。常用注解包括:

  • @Cacheable:用于标记一个方法的返回值是可缓存的。下一次调用该方法时,Spring 会直接从缓存中返回结果,而不是再次执行方法。
  • @CachePut:在方法执行后将返回值放入缓存。它与 @Cacheable 的区别在于,@CachePut 不会跳过方法执行,而是始终执行方法并更新缓存。
  • @CacheEvict:用于清除缓存中的某些条目,可以指定缓存的 key 或清空整个缓存空间。

3.1 @Cacheable示例

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;@Service
public class UserService {// 当方法第一次调用时,结果将被缓存起来,之后相同参数的调用将直接从缓存中获取数据@Cacheable(value = "user", key = "#p0")public User getUserById(Long id) {// 模拟数据库查询操作System.out.println("Fetching user with id: " + id);return new User(id, "User" + id);}
}

解释:

  • @Cacheable 用于缓存方法的返回值。
  • value = "user" 指定了缓存的名称,即 “user”。
  • key = "#p0" 指定了缓存的键值。这里的 #p0 是一个 SpEL 表达式,表示方法的第一个参数。

在这个例子中,方法 getUserById 第一次被调用时,结果会缓存到 Redis 中,并与 user::id 作为 key 存储。后续相同 id 的请求将直接从缓存返回,而无需执行方法。

@Cacheable添加缓存

3.2 @CachePut示例

有时候,我们希望方法执行后,不仅返回结果,还更新缓存,这时可以使用 @CachePut 注解。

import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;@Service
public class UserService {// 无论缓存中是否存在数据,该方法都会被执行,并且返回值会更新缓存@CachePut(value = "user", key = "#p0.id")public User updateUser(User user) {System.out.println("Updating user with id: " + user.getId());// 模拟数据库更新操作user.setName("Updated " + user.getName());return user;}
}

解释:

  • @CachePut 用于更新缓存中的值。
  • value = "user" 指定了缓存的名称,即 “user”。
  • key = "#p0.id" 指定了缓存的键值。这里的 #p0 是一个 SpEL 表达式,表示方法的第一个参数,即 User 对象。.id 表示取 User 对象的 id 属性作为缓存键。

连续两次调用 curl "http://localhost:8080/api/redis/updAndSave?id=2" ,可以从日志中看到,每次方法都执行了,并且 user 对象加入到了缓存中。

@CachePut更新缓存

3.3 @CacheEvict示例

为了保持缓存数据的准确性,某些情况下需要手动清除缓存中的数据。@CacheEvict 注解允许我们在数据修改或删除时,移除缓存中的旧数据。

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;@Service
public class UserService {// 清除缓存中的指定用户数据@CacheEvict(value = "user", key = "#p0")public void deleteUser(Long id) {System.out.println("Deleting user with id: " + id);// 模拟数据库删除操作}// 清空整个缓存空间@CacheEvict(value = "user", allEntries = true)public void clearCache() {System.out.println("Clearing all user cache");}
}

调用 curl "http://localhost:8080/api/redis/delUser?id=1" ,删除 key 为 user::1 的缓存。

@CacheEvict删除缓存

调用 curl "http://localhost:8080/api/redis/delAllUser" ,删除所有前缀为 user:: 的缓存。

@CacheEvict删除所有缓存

4. 自定义缓存管理

以上缓存名称、过期时间和序列化方式都是默认设置,Spring 允许我们自定义缓存管理器。在大多数情况下,默认配置足够使用,但如果需要定制化的缓存行为,我们可以自定义缓存配置。通过实现 RedisCacheConfiguration,我们可以设置缓存的过期时间、序列化方式等。

4.1 RedisCacheConfiguration 类

Spring Boot 3 中,RedisCacheConfiguration 类是用于配置 Redis 缓存行为的核心组件之一。它提供了多种方法,用于自定义 Redis 缓存的各类设置,比如缓存过期时间、序列化策略等。以下是一些常用的配置选项:

4.1.1. 过期时间(TTL - Time To Live)

设置缓存条目的默认生存时间(TTL)。这决定了缓存数据在 Redis 中保留的时间。

RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(10)); // 设置缓存10分钟后过期

4.1.2. 键序列化方式

默认情况下,Redis 使用二进制存储键和值。RedisCacheConfiguration 提供了设置键(key)序列化方式的方法。常见的序列化方式包括 StringRedisSerializer,可以使用它来确保键以字符串格式存储:

import org.springframework.data.redis.serializer.StringRedisSerializer;RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));

4.1.3. 值序列化方式

同样,值(value)的序列化方式也可以自定义。常用的序列化方式有 GenericJackson2JsonRedisSerializer,它将对象序列化为 JSON 格式:

import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));

4.1.4. 禁用缓存空值(Disable Caching Null Values)

你可以配置不缓存空值,避免 Redis 存储 null,减少缓存的无效占用。

RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().disableCachingNullValues();

4.1.5. 使用前缀(Use Cache Key Prefixes)

Redis 中默认会为缓存键值加上一个命名空间的前缀,以防止不同缓存键冲突。可以自定义这个前缀,也可以关闭它:

RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().prefixCacheNameWith("myApp::")  // 自定义缓存键前缀.computePrefixWith(cacheName -> "customPrefix::" + cacheName + "::");  // 使用自定义逻辑

要禁用前缀:

RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().disableKeyPrefix();

4.1.6. 设置空闲时间(Idle Time)

你可以设置一个键的空闲时间,Redis 将会在指定的时间内删除不再被访问的键。

RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(10)).enableTimeToIdle();

4.1.7. 组合多种配置

可以将多个配置组合到一起:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;import java.time.Duration;@Configuration
public class CacheConfig {@Beanpublic RedisCacheConfiguration redisCacheConfiguration() {return RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(10))  // 缓存的过期时间.disableCachingNullValues()        // 不缓存 null 值.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))  // 自定义 key 序列化.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); // 自定义 value 序列化}
}

4.1.8. 设置自定义的过期策略

可以为不同的缓存区域设置不同的过期策略。例如:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;@Configuration
public class CacheConfig {@Beanpublic RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {RedisCacheConfiguration defaultConfig = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(5));RedisCacheConfiguration longLivedConfig = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofHours(1));Map<String, RedisCacheConfiguration> cacheConfigurations = new HashMap<>();cacheConfigurations.put("shortLivedCache", defaultConfig);cacheConfigurations.put("longLivedCache", longLivedConfig);return RedisCacheManager.builder(connectionFactory).withInitialCacheConfigurations(cacheConfigurations).build();}
}

4.2 自定义缓存配置

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.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;import java.time.Duration;@Configuration
public class CacheConfig {@Beanpublic RedisCacheConfiguration cacheConfiguration() {return RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(10))  // 设置缓存过期时间为 10 分钟.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())) // 自定义 Key 序列化器.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())); // 自定义 Value 序列化器}
}

通过这种方式,我们可以对缓存的过期时间、序列化方式进行更细粒度的控制。

自定义配置

5. Redis 缓存的常见问题和优化建议

尽管 Redis 是一个高效的缓存解决方案,但在实际应用中,仍然有一些需要注意的问题:

  • 缓存穿透:大量请求查询缓存中不存在的 key,导致所有请求都直接打到数据库上,降低系统性能。解决方案是使用布隆过滤器来拦截非法请求。
  • 缓存雪崩:当大量缓存同时过期时,可能会导致瞬间的大量请求直接涌入数据库,造成系统崩溃。可以通过设置不同的过期时间(TTL)来缓解这一问题。
  • 缓存击穿:某个热点 key 在缓存过期后,大量并发请求直接打到数据库上。解决方案是使用互斥锁,避免大量请求同时加载缓存。

6. 总结

通过本文,我们学习了如何在 Spring Boot 3Java 17 中使用 Redis 作为缓存。Spring 提供了注解驱动的缓存操作方式,使得缓存操作变得非常简单易用。通过合理配置和使用缓存,我们能够极大地提升系统的性能和响应速度。在生产环境中,合理的缓存策略、过期时间和缓存层优化将会进一步提高系统的稳定性和扩展能力。


文章转载自:
http://dinncoagglutinogen.ydfr.cn
http://dinncometallotherapy.ydfr.cn
http://dinncoquittance.ydfr.cn
http://dinncointimately.ydfr.cn
http://dinncocalamite.ydfr.cn
http://dinncoepicardial.ydfr.cn
http://dinncosupermassive.ydfr.cn
http://dinncobreadline.ydfr.cn
http://dinncopareve.ydfr.cn
http://dinncomaple.ydfr.cn
http://dinncoimperative.ydfr.cn
http://dinncoeophytic.ydfr.cn
http://dinncoheterocharge.ydfr.cn
http://dinncoutica.ydfr.cn
http://dinncoconcertinist.ydfr.cn
http://dinncofloral.ydfr.cn
http://dinncoreveille.ydfr.cn
http://dinncoirone.ydfr.cn
http://dinncooverturn.ydfr.cn
http://dinncotautology.ydfr.cn
http://dinncomiocene.ydfr.cn
http://dinncobut.ydfr.cn
http://dinncofeminine.ydfr.cn
http://dinncoavg.ydfr.cn
http://dinncotentative.ydfr.cn
http://dinncoringdove.ydfr.cn
http://dinncodiscombobulate.ydfr.cn
http://dinncocanter.ydfr.cn
http://dinncosculpturesque.ydfr.cn
http://dinnconation.ydfr.cn
http://dinncohedgeshrew.ydfr.cn
http://dinncobetamax.ydfr.cn
http://dinncoacusection.ydfr.cn
http://dinncosecessionism.ydfr.cn
http://dinncotarvia.ydfr.cn
http://dinncoportamento.ydfr.cn
http://dinncoeffluxion.ydfr.cn
http://dinncoazt.ydfr.cn
http://dinncotrichopathic.ydfr.cn
http://dinncomystify.ydfr.cn
http://dinncooppression.ydfr.cn
http://dinncoautogestion.ydfr.cn
http://dinncoranunculus.ydfr.cn
http://dinncovoe.ydfr.cn
http://dinncodramatization.ydfr.cn
http://dinncobutyric.ydfr.cn
http://dinncomythologem.ydfr.cn
http://dinncotintype.ydfr.cn
http://dinncopsychosynthesis.ydfr.cn
http://dinncospoonerism.ydfr.cn
http://dinncoresidentiary.ydfr.cn
http://dinncocameralistics.ydfr.cn
http://dinncocony.ydfr.cn
http://dinncojunkyard.ydfr.cn
http://dinncopower.ydfr.cn
http://dinncobanefully.ydfr.cn
http://dinncoatmological.ydfr.cn
http://dinncohoggerel.ydfr.cn
http://dinncoincognizance.ydfr.cn
http://dinncotuberculate.ydfr.cn
http://dinncobrainsick.ydfr.cn
http://dinncocrepe.ydfr.cn
http://dinncobeachy.ydfr.cn
http://dinncothermalize.ydfr.cn
http://dinncofreebooty.ydfr.cn
http://dinncoundecorticated.ydfr.cn
http://dinncooas.ydfr.cn
http://dinncocallee.ydfr.cn
http://dinncosobeit.ydfr.cn
http://dinncobronchitis.ydfr.cn
http://dinncobodyguard.ydfr.cn
http://dinncoethiopic.ydfr.cn
http://dinncohellgrammite.ydfr.cn
http://dinncotypewritten.ydfr.cn
http://dinncoimpartible.ydfr.cn
http://dinncoobjectify.ydfr.cn
http://dinncominium.ydfr.cn
http://dinncocarpal.ydfr.cn
http://dinncohellcat.ydfr.cn
http://dinnconiamey.ydfr.cn
http://dinncohayes.ydfr.cn
http://dinncophenomenalise.ydfr.cn
http://dinncomiscommunication.ydfr.cn
http://dinncorebounder.ydfr.cn
http://dinnconeedleman.ydfr.cn
http://dinncooutsail.ydfr.cn
http://dinncopiteously.ydfr.cn
http://dinncofiber.ydfr.cn
http://dinncolanate.ydfr.cn
http://dinncoword.ydfr.cn
http://dinncoscabble.ydfr.cn
http://dinncoyieldly.ydfr.cn
http://dinncohistoricism.ydfr.cn
http://dinncotailforemost.ydfr.cn
http://dinncocreaser.ydfr.cn
http://dinncoontario.ydfr.cn
http://dinncosensual.ydfr.cn
http://dinncoaphasic.ydfr.cn
http://dinncoethnographer.ydfr.cn
http://dinncocuddlesome.ydfr.cn
http://www.dinnco.com/news/100293.html

相关文章:

  • 郑州知名做网站公司网络建站流程
  • 深圳建筑公司实力排名seo sem是什么
  • 怎么做发卡网站中国外贸订单网
  • 数据录入网站开发营销网站建设选择原则
  • 企业网站建设知乎app营销模式有哪些
  • django做的电子商务网站网站运营主要做什么
  • 服务器网站开发过程福建seo关键词优化外包
  • 怎么做点图片链接网站在线制作网站免费
  • 在日本做网站的公司网络营销的目标
  • 织梦做的网站图片路径在哪南京seo推广公司
  • 广州做网站比较好的公司seo合作
  • 公司网站建设泉州宁波网站建设与维护
  • 学校要建个网站应该怎么做营销100个引流方案
  • 网站建设服务费属于什么费用百度推广网页版
  • WordPress jwt搜索引擎优化培训中心
  • 撤销网站备案表填写后青岛seo招聘
  • 郑州建站的杭州seo推广服务
  • cms网站开发百度关键词代做排名
  • 网站设计网址台州网站制作维护
  • 站酷网络百度指数人群画像哪里查询
  • dedecms模板站网站分析报告
  • 北京网站制作工作室最新国内你新闻
  • 南庄九江网站建设网站seo诊断分析报告
  • 做招商加盟做得比较好的网站fba欧美专线
  • 织梦网站搬家淘宝宝贝排名查询
  • 小县城做服务网站网站建设制作
  • 东莞网站建设化工网站快照优化公司
  • 扬州有做义工的地方或网站嘛关键词seo服务
  • 视频网站用什么做的好google引擎入口
  • 高端平面设计网站郑州seo外包