本篇内容主要讲解“weed3-2.3.3.查询的缓存控制是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“weed3-2.3.3.查询的缓存控制是什么”吧!

创新互联建站于2013年成立,是专业互联网技术服务公司,拥有项目网站设计制作、网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元巍山做网站,已为上家服务,为巍山各地企业和个人服务,联系电话:18980820575
Weed3 一个微型ORM框架(只有0.1Mb哦)
源码:https://github.com/noear/weed3 源码:https://gitee.com/noear/weed3
缓存控制,是查询中的重点
框架提供的是控制服务。而非缓存服务本身,了解这个很重要。
缓存控制需要两个重要的接口定义:
1.缓存服务适配接口 ICacheService
(平常用它的加强版 ICacheServiceEx)
//用它可以包装各种缓存服务
public interface ICacheService {
void store(String key, Object obj, int seconds);
Object get(String key);
void remove(String key);
int getDefalutSeconds();
String getCacheKeyHead();
}
/** weed3内置了三个实现类:
*EmptyCache,空缓存
*LocalCache,本地缓存
*SecondCache,二级缓存容器(可以把两个 ICacheService 拼到一起,变成一个二级缓存服务;多嵌套一下就是三级缓存服务了)
*/2.在缓存服务上进行的操控接口:ICacheController
public interface ICacheController{ //使用哪个缓存服务 T caching(ICacheService service); //是否使用缓存 T usingCache(boolean isCache); //使用缓存并设置时间 T usingCache(int seconds); //为缓存添加标签 T cacheTag(String tag); }
有了上面的基础后,现在开始使用缓存控制
1.先搞个服务实例出来
ICacheService cache = new LocalCache();
2.用起来
使用缓存,时间为默认(会自动产生稳定的缓存key)
db.table("test").select("*").caching(cache).getMapList();使用缓存,并缓存30s
db.table("test")
.caching(cache).usingCache(30) //也可以放在table() 和 select()之间
.select("*").getMapList();给缓存加个tag(tag 相当于 缓存key的虚拟文件夹)
db.table("test")
.caching(cache)
.usingCache(30).cacheTag('test_all') //这是tag,不是key
.limit(10,20)
.select("*").getMapList();*3.精细控制
根据查询结果控制缓存时间
db.table("test").where("id=?",10)
.caching(cache)
.select("*").getItem(UserModel.class,(cu,m)->{
if(m.hot > 2){
uc.usingCache(60 * 5); //热门用户缓存5分钟
}else{
uc.usingCache(30);
}
});4.缓存清除
以一个分页查询为例
db.table("test").limit(20,10)
.caching(cache).cacheTag("test_all")
.select("*").getMapList();
db.table("test").limit(30,10)
.caching(cache).cacheTag("test_all")
.select("*").getMapList();
//不管你有多少分页,一个tag把它清光
cache.clear("test_all");5.缓存更新
这个极少用(需要单项更新的缓存,建议用redis)
db.table("test").where("id=?",10)
.caching(cache).cacheTag("test_"+10)
.select("*").getItem(UserModel.class);
cache.update("test_"+10,(UserModel m)->{
m.sex = 10;
return m;
});框架的缓存控制,也是极为自由的哟。应该是的吧?哈合。
缓存服务的可用情况
org.noear.weed.cache.EmptyCache // 空缓存
org.noear.weed.cache.LocalCache // 轻量级本地缓存(基于Map实现)
org.noear.weed.cache.SecondCache // 二级缓存(组装两个 ICacheServiceEx 实现)
org.noear.weed.cache.ehcache.EhCache // 基于ehcache封装
org.noear weed3.cache.ehcache 3.2.1.1
org.noear.weed.cache.j2cache.J2Cache // 基于国人开发的J2Cache封装
org.noear weed3.cache.j2cache 3.2.1.1
org.noear.weed.cache.memcached.MemCache // 基于memcached封装
org.noear weed3.cache.memcached 3.2.1.1
org.noear.weed.cache.redis.RedisCache // 基于redis封装
org.noear weed3.cache.redis 3.2.1.1
也可以自己封装个 ICacheServiceEx ...
到此,相信大家对“weed3-2.3.3.查询的缓存控制是什么”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
网页标题:weed3-2.3.3.查询的缓存控制是什么
转载来于:http://lzwzjz.cn/article/jpchgs.html


咨询
建站咨询
