日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

30秒缓存

發布時間:2025/6/16 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 30秒缓存 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

緩存類:

package test;

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* 用來存儲短暫對象的緩存類,實現Map接口,內部有一個定時器用來清除過期(30秒)的對象。
* 為避免創建過多線程,沒有特殊要求請使用getDefault()方法來獲取本類的實例。
* 此代碼來源于互聯網
* @param <K>
* @param <V>
*/

public class CacheMap<K, V> extends AbstractMap<K, V> {
//過期時間為30秒
private static final long DEFAULT_TIMEOUT = 30000;
private static CacheMap<Object, Object> defaultInstance;

public static synchronized final CacheMap<Object, Object> getDefault() {
if (defaultInstance == null) {
defaultInstance = new CacheMap<Object, Object>(DEFAULT_TIMEOUT);
}
return defaultInstance;
}

private class CacheEntry implements Entry<K, V> {
long time;
V value;
K key;

CacheEntry(K key, V value) {
super();
this.value = value;
this.key = key;
this.time = System.currentTimeMillis();
}

@Override
public K getKey() {
return key;
}

@Override
public V getValue() {
return value;
}

@Override
public V setValue(V value) {
return this.value = value;
}
}

private class ClearThread extends Thread {
ClearThread() {
setName("clear cache thread");
}

public void run() {
while (true) {
try {
long now = System.currentTimeMillis();
Object[] keys = map.keySet().toArray();
for (Object key : keys) {
CacheEntry entry = map.get(key);
if (now - entry.time >= cacheTimeout) {
synchronized (map) {
map.remove(key);
}
}
}
Thread.sleep(cacheTimeout);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

private long cacheTimeout;
private Map<K, CacheEntry> map = new HashMap<K, CacheEntry>();

public CacheMap(long timeout) {
this.cacheTimeout = timeout;
new ClearThread().start();
}

@Override
public Set<Entry<K, V>> entrySet() {
Set<Entry<K, V>> entrySet = new HashSet<Map.Entry<K, V>>();
Set<Entry<K, CacheEntry>> wrapEntrySet = map.entrySet();
for (Entry<K, CacheEntry> entry : wrapEntrySet) {
entrySet.add(entry.getValue());
}
return entrySet;
}

@Override
public V get(Object key) {
CacheEntry entry = map.get(key);
return entry == null ? null : entry.value;
}

@Override
public V put(K key, V value) {
CacheEntry entry = new CacheEntry(key, value);
synchronized (map) {
map.put(key, entry);
}
return value;
}
}

調用緩存類:

package test;

/**
* 調用步驟:
* 1:向緩存類中存放一個key和Value
* 2:30秒之前使用get(key)取出的是value
* 3:30秒后使用get(key)取出的是null
* @author xuming
*
*/
public class run {

public static void main(String[] args) {
//實例化CacheMap方法
CacheMap c = CacheMap.getDefault();
c.put("k1","v1");
//存放時的時間戳
double currentTime =System.currentTimeMillis();
for(int i = 0;i<500000;i++){
//當前的時間差---獲取get(key)
System.out.println((System.currentTimeMillis()-currentTime)/10000+"秒---"+c.get("k1"));
}
}

}

轉載于:https://www.cnblogs.com/Xmingzi/p/7886722.html

總結

以上是生活随笔為你收集整理的30秒缓存的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。