本文目錄一覽:
Java實現簡單的緩存機制原理
package qinbo hui;
/*
設計思想來自-回欽波(qq: )
*/
public class CacheConfModel implements java io Serializable{
private long beginTime;
private boolean isForever = false;
private int durableTime;
public long getBeginTime() {
return beginTime;
}
public void setBeginTime(long beginTime) {
this beginTime = beginTime;
}
public boolean isForever() {
return isForever;
}
public void setForever(boolean isForever) {
this isForever = isForever;
}
public int getDurableTime() {
return durableTime;
}
public void setDurableTime(int durableTime) {
this durableTime = durableTime;
}
}
package qinbo hui;
import java util *;
import test CacheConfModel;
/*
設計思想來自-回欽波(qq: )
*/
public class CacheMgr {
private static Map cacheMap = new HashMap();
private static Map cacheConfMap = new HashMap();
private CacheMgr(){
}
private static CacheMgr cm = null;
public static CacheMgr getInstance(){
if(cm==null){
cm = new CacheMgr();
Thread t = new ClearCache();
t start();
}
return cm;
}
/**
* 增加緩存
* @param key
* @param value
* @param ccm 緩存對象
* @return
*/
public boolean addCache(Object key Object value CacheConfModel ccm){
boolean flag = false;
cacheMap put(key value);
cacheConfMap put(key ccm);
System out println( now addcache== +cacheMap size());
return true;
}
/**
* 刪除緩存
* @param key
* @return
*/
public boolean removeCache(Object key){
cacheMap remove(key);
cacheConfMap remove(key);
System out println( now removeCache== +cacheMap size());
return true;
}
/**
* 清除緩存的類
* @author wanglj
* 繼承Thread線程類
*/
private static class ClearCache extends Thread{
public void run(){
while(true){
Set tempSet = new HashSet();
Set set = cacheConfMap keySet();
Iterator it = erator();
while(it hasNext()){
Object key = it next();
CacheConfModel ccm = (CacheConfModel)cacheConfMap get(key);
//比較是否需要清除
if(!ccm isForever()){
if((new Date() getTime() ccm getBeginTime())= ccm getDurableTime()* * ){
//可以清除 先記錄下來
tempSet add(key);
}
}
}
//真正清除
Iterator tempIt = erator();
while(tempIt hasNext()){
Object key = tempIt next();
cacheMap remove(key);
cacheConfMap remove(key);
}
System out println( now thread================ +cacheMap size());
//休息
try {
Thread sleep( * L);
} catch (InterruptedException e) {
// TODO Auto generated catch block
e printStackTrace();
}
}
}
}
lishixinzhi/Article/program/Java/hx/201311/25737
什麼是Java緩存技術Cache
java緩存技術
一、什麼是緩存
1、Cache是高速緩衝存儲器
一種特殊的存儲器子系統,其中複製了頻繁使用的數據以利於快速訪問
2、凡是位於速度相差較大的兩種硬件/軟件之間的,用於協調兩者數據傳輸速度差異的結構,均可稱之為
Cache
二、緩存的分類
1、基於web應用的系統架構圖
2、在系統架構的不同層級之間,為了加快訪問速度,都可以存在緩存
操作系統磁盤緩存-減少磁盤機械操作
數據庫緩存-減少文件系統I/O
應用程序緩存-減少對數據庫的查詢
Web服務器緩存-減少應用服務器請求
客戶端瀏覽器緩存-減少對網站的訪問。
cache java
cache java是什麼, 讓我們一起了解一下?
Cache 是一個像 Map 一樣的數據結構,它允許基於 Key 的臨時儲存。緩存被單個 CacheManager 擁有。
Java 的緩存 API 定義了五個核心接口:CachingProvider,CacheManager,Cache,Entry 和 ExpiryPolicy。
Java實現cache的基本機制是什麼?
我這裡說的cache不是指CPU和RAM之間的緩存,而是java應用中間常用的緩存。最常使用的場合就是訪問數據庫的時候為了提高效率而使用的 cache。一般的用法就是把數據從數據庫讀到內存,然後之後的數據訪問都從內存來讀,從而減少對數據庫的讀取次數來提高效率。
說了這麼多,Java 下到底如何實現Cache,希望下面的實際案例可以幫助到你。 public class CacheFactory { private static ConcurrentHashMap caches = new ConcurrentHashMap(); private static ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1); private static void register(Cache cache) { caches.put(cache.category(), cache); } private static void registerAll() { register(new StockCache()); } public static void init() { registerAll(); for (Cache cache : caches.values()) { executorService.scheduleAtFixedRate(new Runnable() { @Override public void run() { cache.refresh(); } }, 0, cache.interval(), TimeUnit.MILLISECONDS); } } public static Cache getCache(String key) { if (caches.contains(key)) { return caches.get(key); } return null; } } // cache接口除了需要提供interval和refresh以外,還需要提供一個category來區分不同的Cache public interface Cache { /** * Refresh the cache. If succeed, return true, else return false; * * @return */ boolean refresh(); /** * How much time it will refresh the cache. * * @return */ long interval(); /** * Cache’s category. Each cache has distinct category. * * @return */ String category(); }
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/157281.html