「java缓存分类」Java的缓存
本篇文章给大家谈谈java缓存分类,以及Java的缓存对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java静态缓存技术有哪些
1. 独立对象缓存
2. 利用对象缓存
3. 利用应用缓存
4. 利用页面缓存
5. 利用 Ajax 实现缓存
6. 利用 HTTP 头来灵活管理缓存
7. 利用 CDN 实现缓存
JAVA目前比较常用的缓存有哪些? 集中式缓存与分布式缓存有何区别? 它们应用场景是?
java目前常用的缓存:
Generic
JCache (JSR-107) (EhCache 3, Hazelcast, Infinispan, etc)
EhCache 2.x
Hazelcast
Infinispan
Couchbase
Redis
Caffeine
Guava (deprecated)
Simple
建议使用spring boot集成方式,可插拔,简单。
集中式缓存适用场景:
1、服务器集群部署。
2、数据高一致性(任何数据变化都能及时的被查询到)
分布式缓存适用场景:
系统需要缓存的数据量大
对数据的可用性较高的情况
需要横向扩展,从而达到缓存的容量无限的要求
java用户评论缓存在哪里
在java应用中,对于访问频率比较高,又不怎么变化的数据,常用的解决方案是把这些数据加入缓存。相比DB,缓存的读取效率快好不少。java应用缓存一般分两种,一是进程内缓存,就是使用java应用虚拟机内存的缓存;另一个是进程外缓存,现在我们常用的各种分布式缓存。相比较而言,进程内缓存比进程外缓存快很多,而且编码也简单;但是,进程内缓存的存储量有限,使用的是java应用虚拟机的内存,而且每个应用都要存储一份,有一定的资源浪费。进程外缓存相比进程内缓存,会慢些,但是,存储空间可以横向扩展,不受限制。
这里是几中场景的访问时间
-------------------------------------------------------------------
| 从数据库中读取一条数据(有索引) | 十几毫秒 |
| 从远程分布式缓存读取一条数据 | 0.5毫秒 |
| 从内存中读取1MB数据 | 十几微妙 |
-------------------------------------------------------------------
进程内缓存和进程外缓存,各有优缺点,针对不同场景,可以分别采用不同的缓存方案。对于数据量不大的,我们可以采用进程内缓存。或者只要内存足够富裕,都可以采用,但是不要盲目以为自己富裕,不然可能会导致系统内存不够。
下面要分享的是一个代码级别的,对进程内缓存的经验总结。面向jdk1.8版本。
在有效时间内缓存单个对象
public class LiveCacheT { // 缓存时间
private final int cacheMillis; // 缓存对象
private final T element; // 缓存对象创建时间
private final long createTime;
public LiveCache(int cacheMillis, T element) { this.cacheMillis = cacheMillis; this.element = element; this.createTime = System.currentTimeMillis();
}
// 获取缓存对象
public T getElement() { long currentTime = System.currentTimeMillis(); if(cacheMillis 0 currentTime - createTime cacheMillis) { return null;
} else { return element;
}
}
// 获取缓存对象,忽略缓存时间有效性
public T getElementIfNecessary() { return element;
}
}
public static void main(String[] args) { int cacheMilis = 1000 ;
LiveCacheObject liveCache = new LiveCache(cacheMilis, new Object()) ;
liveCache.getElement() ;
liveCache.getElementIfNecessary() ;
}
有效时间内,缓存单个对象,可异步刷新
@FunctionalInterfacepublic interface LiveFetchT { // 刷新缓存接口 T fetch() ;
}
public class LiveManagerT { // 缓存时间
private int cacheMillis; // 缓存对象
private LiveCacheT liveCache; // 刷新缓存的对象
private LiveFetchT liveFetch ;
private Logger logger = LoggerFactory.getLogger(LiveManager.class) ;
// 刷新缓存开关
private boolean refresh = false ;
public LiveManager(int cacheMillis, LiveFetchT liveFetch) { this.cacheMillis = cacheMillis ; this.liveFetch = liveFetch ;
}
/**
* fetch cache ; if cache expired , synchronous fetch
* @return
*/
public T getCache() {
initLiveCache();
if(liveCache != null) {
T t ; if((t= liveCache.getElement()) != null) { return t ;
} else {
t = liveFetch.fetch() ; if(t != null) {
liveCache = new LiveCacheT(cacheMillis, t) ; return t ;
}
}
}
return null ;
}
/**
* fetch cache ; if cache expired , return old cache and asynchronous fetch
* @return
*/
public T getCacheIfNecessary() {
initLiveCache();
if(liveCache != null) {
T t ; if((t= liveCache.getElement()) != null) { return t ;
} else {
refreshCache() ; return liveCache.getElementIfNecessary() ;
}
}
return null ;
}
/**
* init liveCache */
private void initLiveCache() { if(liveCache == null) {
T t = liveFetch.fetch() ; if(t != null) {
liveCache = new LiveCacheT(cacheMillis, t) ;
}
}
}
/**
* asynchronous refresh cache */
private void refreshCache() {
if(refresh) return ;
refresh = true ; try {
Thread thread = new Thread(() - { try {
T t = liveFetch.fetch(); if (t != null) {
liveCache = new LiveCache(cacheMillis, t);
}
} catch (Exception e){
logger.error("LiveManager.refreshCache thread error.", e);
} finally {
refresh = false ;
}
}) ;
thread.start();
} catch (Exception e) {
logger.error("LiveManager.refreshCache error.", e);
}
}
}
public class Test {
public static void main(String[] args) { int cacheMilis = 1000 ;
LiveManagerObject liveManager = new LiveManager(cacheMilis,() - new Test().t1()) ;
liveManager.getCache() ;
liveManager.getCacheIfNecessary() ;
}
public Object t1(){
return new Object() ;
}
}
什么是Java缓存技术Cache
java缓存技术
一、什么是缓存
1、Cache是高速缓冲存储器
一种特殊的存储器子系统,其中复制了频繁使用的数据以利于快速访问
2、凡是位于速度相差较大的两种硬件/软件之间的,用于协调两者数据传输速度差异的结构,均可称之为
Cache
二、缓存的分类
1、基于web应用的系统架构图
2、在系统架构的不同层级之间,为了加快访问速度,都可以存在缓存
操作系统磁盘缓存-减少磁盘机械操作
数据库缓存-减少文件系统I/O
应用程序缓存-减少对数据库的查询
Web服务器缓存-减少应用服务器请求
客户端浏览器缓存-减少对网站的访问。
关于java缓存分类和Java的缓存的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-26,除非注明,否则均为
原创文章,转载请注明出处。