网站建设管理教程,网站点击量设计,梅林关网站建设,网站建设开标书文章目录 “缓存预热” 是什么#xff1f;项目环境搭建创建数据访问层预热数据到 Redis 中创建缓存服务类测试缓存预热 “缓存预热” 是什么#xff1f;
缓存预热是一种优化策略#xff0c;在系统启动或者流量高峰来临之前#xff0c;将一些经常访问的数据提前加载到缓存中… 文章目录 “缓存预热” 是什么项目环境搭建创建数据访问层预热数据到 Redis 中创建缓存服务类测试缓存预热 “缓存预热” 是什么
缓存预热是一种优化策略在系统启动或者流量高峰来临之前将一些经常访问的数据提前加载到缓存中。这样做的好处是当用户实际请求这些数据时能够直接从缓存中获取避免了从数据库等慢速数据源中查询数据从而提高系统的响应速度和吞吐量减少数据库的压力。
缓存预热通常发生在以下情况下 系统投入使用前 在系统正式投入使用之前可以对一些初始化数据进行预热以避免系统上线初期因为大量数据未被缓存而导致的性能问题。 数据访问热度周期性变化较高的情况下 对于有些数据其访问热度可能会随着时间变化而变化可以在预计到达高峰期之前预热这些数据以确保在高峰期能够直接从缓存中获取提高系统性能。
实际上缓存预热是一种以时间换空间的策略通过预先将需要频繁访问的数据加载到缓存中来减少后续访问时因为缓存未命中而导致的性能损失。
例如一个电商网站准备举办大型促销活动预计将有大量用户访问某一特定类别的商品页面。为了避免在活动期间因为商品数据缓存未命中而导致系统性能下降可以提前对这一类别的商品信息进行缓存预热。即在活动开始之前系统可以将这类商品的信息提前加载到缓存中以确保在活动期间可以直接从缓存中获取数据提高系统的响应速度。
以下是一个基于 SpringBoot 与 Redis 的缓存预热案例。
项目环境搭建 引入依赖 dependencygroupIdorg.apache.commons/groupIdartifactIdcommons-pool2/artifactId
/dependency
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId
/dependency
dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId
/dependency
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId
/dependency定义启动类 package test;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication
public class SpringBootApp {public static void main(String[] args) {SpringApplication.run(SpringBootApp.class, args);}}定义配置 spring:redis:# Redis主机IPhost: localhost# Redis主机端口port: 6379# Redis主机密码password:# 使用Redis的8号库database: 8# 使用lettuce客户端lettuce:# 连接池配置pool:# 最大连接数max-active: 8# 最大空闲连接max-idle: 8# 最小空闲连接min-idle: 0# 连接等待时间max-wait: 100创建数据访问层
模拟从数据库中获取用户数据。
package test;import org.springframework.stereotype.Repository;import java.util.ArrayList;
import java.util.List;Repository
public class UserDao {/*** 模拟从数据库中获取所有用户** return 所有用户*/public ListUser getAllUsers() {ListUser users new ArrayList();users.add(new User(1L, Alice));users.add(new User(2L, Bob));users.add(new User(3L, Charlie));return users;}}预热数据到 Redis 中
实现 CommandLineRunner 接口在 Spring Boot 应用启动时执行缓存预热操作。
package test;import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;import java.util.List;Component
Slf4j
public class CachePreheater implements CommandLineRunner {Autowiredprivate UserDao userDao;Autowiredprivate UserCacheService userCacheService;Overridepublic void run(String... args) {// 从数据库获取所有用户数据ListUser users userDao.getAllUsers();// 将用户数据缓存到 Redis 中userCacheService.cacheUsers(users);log.info(Cache preheating completed.);}}启动 SpringBoot 应用控制台输出如下结果 以上结果说明数据已经成功被预热到缓存Redis中。
创建缓存服务类
负责将用户数据缓存到 Redis 中并从 Redis 中获取用户数据。
package test;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.List;Service
public class UserCacheService {Resourceprivate RedisTemplateString, User redisTemplate;public void cacheUsers(ListUser users) {for (User user : users) {redisTemplate.opsForValue().set(user: user.getId(), user);}}public User getUserFromCache(Long userId) {return redisTemplate.opsForValue().get(user: userId);}}测试缓存预热
创建一个控制器来测试缓存是否预热成功。
package test;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;RestController
public class UserController {Autowiredprivate UserCacheService userCacheService;GetMapping(/users/{userId})public User getUser(PathVariable Long userId) {return userCacheService.getUserFromCache(userId);}}在浏览器中访问 http://localhost:8080/users/1得到的响应结果 以上结果说明成功从缓存中获取到了预热到的数据。