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

一个外国人做的汉子 网站免费培训seo网站

一个外国人做的汉子 网站,免费培训seo网站,宁波信息港,怎么找人帮做网站文章目录 “缓存预热” 是什么?项目环境搭建创建数据访问层预热数据到 Redis 中创建缓存服务类测试缓存预热 “缓存预热” 是什么? 缓存预热是一种优化策略,在系统启动或者流量高峰来临之前,将一些经常访问的数据提前加载到缓存中…

文章目录

    • “缓存预热” 是什么?
    • 项目环境搭建
    • 创建数据访问层
    • 预热数据到 Redis 中
    • 创建缓存服务类
    • 测试缓存预热

“缓存预热” 是什么?

缓存预热是一种优化策略,在系统启动或者流量高峰来临之前,将一些经常访问的数据提前加载到缓存中。这样做的好处是,当用户实际请求这些数据时,能够直接从缓存中获取,避免了从数据库等慢速数据源中查询数据,从而提高系统的响应速度和吞吐量,减少数据库的压力。

缓存预热通常发生在以下情况下:

  1. 系统投入使用前:

    在系统正式投入使用之前,可以对一些初始化数据进行预热,以避免系统上线初期因为大量数据未被缓存而导致的性能问题。

  2. 数据访问热度周期性变化较高的情况下:

    对于有些数据,其访问热度可能会随着时间变化而变化,可以在预计到达高峰期之前预热这些数据,以确保在高峰期能够直接从缓存中获取,提高系统性能。

实际上,缓存预热是一种以时间换空间的策略,通过预先将需要频繁访问的数据加载到缓存中,来减少后续访问时因为缓存未命中而导致的性能损失。

例如,一个电商网站准备举办大型促销活动,预计将有大量用户访问某一特定类别的商品页面。为了避免在活动期间因为商品数据缓存未命中而导致系统性能下降,可以提前对这一类别的商品信息进行缓存预热。即在活动开始之前,系统可以将这类商品的信息提前加载到缓存中,以确保在活动期间可以直接从缓存中获取数据,提高系统的响应速度。

以下是一个基于 SpringBoot 与 Redis 的缓存预热案例。

项目环境搭建

  • 引入依赖:

    <dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId>
    </dependency>
    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId>
    </dependency>
    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-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 List<User> getAllUsers() {List<User> 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) {// 从数据库获取所有用户数据List<User> 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 RedisTemplate<String, User> redisTemplate;public void cacheUsers(List<User> 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,得到的响应结果:

在这里插入图片描述

以上结果说明,成功从缓存中获取到了预热到的数据。

http://www.dtcms.com/wzjs/212587.html

相关文章:

  • 做服装搭配的流行趋势网站无锡百姓网推广
  • 网站开发都是使用框架吗seo网站优化工具大全
  • 江西哪里有做电商网站的公司推广一手渠道
  • 如何做自适应网站app软件开发制作公司
  • 江西专业的网站建设制作app开发需要多少钱
  • 微商城下载优化视频
  • 网站开发是用html还是jsp搜索排名优化策划
  • 邓州网络推广有效的方法seo关键词排名网络公司
  • 免费搭建贴吧系统网站东莞seo快速排名
  • 大连做网站建设好搜seo软件
  • 网站建设实训周记河南seo排名
  • 网站建设暨检务公开自查报告郑州seo
  • 重庆市城乡住房和建设信息网seo的搜索排名影响因素有
  • 免费外贸网站建设企业推广公司
  • html5网站开发环境知道百度
  • 乐陵seo推广电商中seo是什么意思
  • 巨野网站定制个人网页设计
  • 新乡网站建设win10系统优化工具
  • 关于电视剧的网站设计网页云南seo公司
  • 江油建设局网站免费访问国外网站的app
  • 盐城做网站找哪家好推广项目的平台
  • 水泥制品做阿里巴巴还是网站好外贸营销策略都有哪些
  • 重庆做汉堡的餐饮公司网站长沙网站seo推广公司
  • 瑞安企业做网站临沂seo整站优化厂家
  • 怎么把视频做成网页链接如何优化关键词提升相关度
  • wordpress 缩略图不显示网站seo博客
  • 网站如何做线上推广谷歌在线浏览器入口
  • 重生做二次元网站免费制作自己的网站
  • 柳州做网站那家好网页推广平台
  • 需要网站建设的是哪一类人在线seo短视频