给自己做的网站换首页,深圳设计总院,网站建设招标公告,上哪儿找做网站jetcache方法缓存
我们可以给每个方法配置缓存方案
JetCache 是一个基于 Java 的缓存库#xff0c;支持多种缓存方案和缓存策略#xff0c;主要用于提升应用程序的性能和响应速度。它提供了多种缓存模式和特性#xff0c;可以根据需求选择合适的缓存方案。
JetCache 的主…jetcache方法缓存
我们可以给每个方法配置缓存方案
JetCache 是一个基于 Java 的缓存库支持多种缓存方案和缓存策略主要用于提升应用程序的性能和响应速度。它提供了多种缓存模式和特性可以根据需求选择合适的缓存方案。
JetCache 的主要特点和功能 支持多种缓存后端 JetCache 支持多种缓存后端包括 Redis、Caffeine、LevelDB 等可以根据项目的需要选择合适的后端存储。 多种缓存模式 Local Cache本地缓存使用 Caffeine 或者 ConcurrentHashMap 等适用于单个应用节点内的快速访问。Remote Cache远程缓存例如 Redis适用于分布式环境下的数据共享和访问。 缓存注解支持 提供了注解方式来标记需要缓存的方法或者数据简化了缓存的配置和管理。 缓存策略 支持多种缓存策略例如基于 TTLTime-To-Live的过期策略、基于定时刷新的刷新策略、基于手动更新的策略等可以根据业务需求选择合适的策略。 二级缓存支持 支持二级缓存例如先从本地快速缓存中获取数据如果未命中再从远程缓存中获取可以有效减少远程调用带来的延迟。 数据序列化和反序列化 提供了对缓存数据的序列化和反序列化支持可以灵活地处理不同类型的数据对象。 监控和统计 提供了监控和统计功能可以实时查看缓存的使用情况、命中率等指标便于性能调优和监控。
使用 JetCache 的步骤
使用 JetCache 进行方法级别的缓存通常涉及以下几个步骤 引入 JetCache 依赖 在项目的 Maven 或者 Gradle 配置文件中引入 JetCache 的相关依赖。 配置缓存后端 配置 JetCache 使用的缓存后端例如 Redis、Caffeine 等。需要配置缓存的地址、端口、密码等参数。 定义缓存配置 可以通过 Java 代码或者配置文件的方式定义缓存的配置信息包括缓存的名称、过期时间、缓存模式等。 在方法上添加缓存注解 使用 JetCache 提供的注解如 Cached、CacheInvalidate 等来标记需要进行缓存操作的方法。可以指定缓存的 key、条件、过期时间等参数。 调整缓存策略 根据具体的业务需求和性能要求调整缓存的策略和参数例如缓存的过期时间、刷新策略等。 启动应用程序 启动应用程序JetCache 将根据配置和注解来管理和处理缓存提升应用的性能和响应速度。
我们是在业务层的实现类里操作的 我们首先要在启动响应类里打开开关 EnableCreateCacheAnnotation
EnableMethodCache(basePackages com.example.demo)
在业务层的实现类里去修改代码 Override
Cached(namebook,key#id,expire 3600)
public User getById(Integer id) {return userDao.selectById(id);
}
注意 预防空指针异常 注意 预防序列化反序列化 配置中同样要进行修改
意思是告诉框架 这是个java对象 启动成功 但是我们有一个问题
如果取缓存
缓存中的数据变化了
我们确没有更新
我们怎么去做
我们要在更新操作里去修改
我们每次调用更新操作或者是删除操作的时候
都会有个数值的更新操作
Override
CacheUpdate(namebook_,key#book.id,value#book)
public boolean update(User user) {return userDao.updateById(user)0;
} 如果我们有两个系统
同时对一个表进行操作
一个删除 对方也不知道
这就叫我们内存中的数据和系统中的数据不同步
我们要进行一个刷新 设置每十秒进行一次刷新
CacheRefresh(refresh 10)
package com.example.demo.service.impl;import com.alicp.jetcache.anno.CacheInvalidate;
import com.alicp.jetcache.anno.CacheRefresh;
import com.alicp.jetcache.anno.CacheUpdate;
import com.alicp.jetcache.anno.Cached;
import com.example.demo.dao.UserDao;
import com.example.demo.domain.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;import java.util.List;Service
public class UserServiceImpl implements UserService {Autowiredprivate UserDao userDao;OverrideCached(namebook_,key#id,expire 3600)CacheRefresh(refresh 10)public User getById(Integer id) {return userDao.selectById(id);}Overridepublic boolean save(User user) {return userDao.insert(user)0;}OverrideCacheUpdate(namebook_,key#book.id,value#book)CacheRefresh(refresh 10)public boolean update(User user) {return userDao.updateById(user)0;}OverrideCacheInvalidate(namebook_,key#book.id)CacheRefresh(refresh 10)public boolean delete(Integer id) {return userDao.deleteById(id)0;}Overridepublic ListUser getAll() {return userDao.selectList(null);}
}
我们还可以加上一个配置
设置每隔一分钟会进行jetcache信息的打印 jetcache:statIntervalMinutes: 1local:default:type: linkedhashmapkeyConvertor: fastjson
你看在控制台就能打印了 我们进行查询后
有了数据 小结 个人号推广
博客主页
多多!-CSDN博客
Web后端开发
https://blog.csdn.net/qq_30500575/category_12624592.html?spm1001.2014.3001.5482
Web前端开发
https://blog.csdn.net/qq_30500575/category_12642989.html?spm1001.2014.3001.5482
数据库开发
https://blog.csdn.net/qq_30500575/category_12651993.html?spm1001.2014.3001.5482
项目实战
https://blog.csdn.net/qq_30500575/category_12699801.html?spm1001.2014.3001.5482
算法与数据结构
https://blog.csdn.net/qq_30500575/category_12630954.html?spm1001.2014.3001.5482
计算机基础
https://blog.csdn.net/qq_30500575/category_12701605.html?spm1001.2014.3001.5482
回忆录
https://blog.csdn.net/qq_30500575/category_12620276.html?spm1001.2014.3001.5482