当前位置:网站首页>18、优化网站性能
18、优化网站性能
2022-08-02 07:27:00 【A snicker】
本地缓存
- 将数据缓存在应用服务器上,性能最好。(要考虑缓存大小,缓存的过期时间)
- 常用缓存工具:Ehcache、Guava、Caffeine等。
分布式缓存
- 将数据缓存在NoSQL数据库上,跨服务器。
- 常用缓存工具:MemCache、Redis等。
多级缓存
一级缓存(本地缓存)> 二级缓存(分布式缓存)> DB
- 避免缓存雪崩(缓存失效,大量请求直达DB),提高系统的可用性
本地缓存适合与用户无强关联的信息,Redis可以缓存与用户强关联的信息(如 登录凭证),可以跨服务器,但比本地缓存稍微慢些
两级缓存的过程:
本地缓存
数据变化频率相对较低的数据比较使用于缓存。将热门帖子列表缓存到本地缓存中。使用Caffeine。
1、自定义配置
# caffeine
caffeine.posts.max-size=15 // 设置缓存空间里缓存多少对象,缓存的列表的对象是page
caffeine.posts.expire-seconds=180 // 3min
缓存数据的更新一般有两种方式:1、数据发生更新 2、缓存到了过期时间
3、优化业务方法,一般是优化Service。
使用Caffeine 缓存 帖子列表和总的行数
Caffeine的核心接口是Cache,其有两个子接口 LoadingCache, AsyncLoadingCache。LoadingCache 是同步缓存,AsyncLoadingCache可以实现异步、并发。
@Value("${caffeine.posts.max-size}")
private int maxSize;
@Value("${caffeine.posts.expire-seconds}")
private int expireSeconds;
// Caffeine核心接口: Cache, LoadingCache, AsyncLoadingCache
// 帖子列表缓存
private LoadingCache<String, List<DiscussPost>> postListCache;
// 帖子总数缓存
private LoadingCache<Integer, Integer> postRowsCache;
@PostConstruct
public void init() {
// 初始化帖子列表缓存
postListCache = Caffeine.newBuilder()
.maximumSize(maxSize)
.expireAfterWrite(expireSeconds, TimeUnit.SECONDS)
.build(new CacheLoader<String, List<DiscussPost>>() {
@Nullable
@Override
public List<DiscussPost> load(@NonNull String key) throws Exception {
// 该方法即是 当本地缓存不存在 所需的数据是,从数据库查找,并存入缓存中
if (key == null || key.length() == 0) {
throw new IllegalArgumentException("参数错误!");
}
String[] params = key.split(":");
if (params == null || params.length != 2) {
throw new IllegalArgumentException("参数错误!");
}
int offset = Integer.valueOf(params[0]);
int limit = Integer.valueOf(params[1]);
// 可以在这里添加二级缓存: Redis -> mysql
logger.debug("load post list from DB.");
return discussPostMapper.selectDiscussPosts(0, offset, limit, 1);
}
});
// 初始化帖子总数缓存
postRowsCache = Caffeine.newBuilder()
.maximumSize(maxSize)
.expireAfterWrite(expireSeconds, TimeUnit.SECONDS)
.build(new CacheLoader<Integer, Integer>() {
@Nullable
@Override
public Integer load(@NonNull Integer key) throws Exception {
logger.debug("load post rows from DB.");
return discussPostMapper.selectDiscussPostRows(key);
}
});
}
public List<DiscussPost> findDiscussPosts(int userId, int offset, int limit, int orderMode) {
if (userId == 0 && orderMode == 1) {
// 只有当访问首页热门帖子时才从缓存中取数据
return postListCache.get(offset + ":" + limit);
}
logger.debug("load post list from DB.");
return discussPostMapper.selectDiscussPosts(userId, offset, limit, orderMode);
}
public int findDiscussPostRows(int userId) {
if (userId == 0) {
return postRowsCache.get(userId);
}
logger.debug("load post rows from DB.");
return discussPostMapper.selectDiscussPostRows(userId);
}
压力测试
二级缓存
19、项目监控
https://blog.csdn.net/weixin_42033436/article/details/117781598?spm=1001.2014.3001.5502
边栏推荐
猜你喜欢
MySQL - based
Mysql error 2003 solution Can 't connect to Mysql server on' localhost '(10061).
MySQL-底层设置
The best interests of buying and selling stocks with handling fees [What is missing in the definition of DP status?]
MySQL-索引优化和查询优化
HCIP 第十一天
MySQL事务(transaction) (有这篇就足够了..)
WebForm DropDownList分别绑定年月
Understand Chisel language. 31. Chisel advanced communication state machine (3) - Ready-Valid interface: definition, timing and implementation in Chisel
设置工作模式与环境(中):建造二级引导器
随机推荐
HCIP第三天
How to export multiple query results at once in SQL server 2014?
ROS file system and related commands
Comprehensive experiment of MPLS and BGP
Visual Analysis of DeadLock
MySQL-FlinkCDC-Hudi enters the lake in real time
Please tell me, how to write Flink SQL and JDBC sink into mysql library and want to create an auto-incrementing primary key
CSRF-Cross-site request forgery-related knowledge
Splunk Filed extraction field interception
spark read local file
MySQL报错1055解决办法:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains
Go implements distributed locks
【CV】OpenVINO installation tutorial
flutter在导航栏处实现对两个列表的点击事件
MySQL database design specification
Debian 10 dhcp relay (dhcp 中继) dhcp 固定分配
MySQL - low level settings
WebForm DropDownList bind year and month respectively
MGRE环境下的OSPF
redis-高级篇