一般cache的做法
2015-05-26 10:47
211 查看
其实比较简单,直接来个例子,e.g.
TryCache.java
GetData.java
跨节点情况请参考:一致性hash算法: cache、负载均衡应用
http://blog.csdn.net/textboy/article/details/46004117
TryCache.java
public class InforoneCache<K, V> implements Cache<K, V> { class CacheObject<K2, V2> { final K2 key; final V2 value; long lastModified; // 最后添加时间 long expire; // 对象存活时间 CacheObject(K2 key, V2 value, long expire) { this.key = key; this.value = value; this.lastModified = System.currentTimeMillis(); this.expire = expire; } boolean isExpired() { if (expire == 0) { return false; } return lastModified + expire < System.currentTimeMillis(); } V2 getObject() { return value; } } protected Map<K, CacheObject<K, V>> cacheMap; private final ReentrantReadWriteLock cacheLock = new ReentrantReadWriteLock(); private final Lock readLock = cacheLock.readLock(); private final Lock writeLock = cacheLock.writeLock(); protected int cacheSize; // 缓存大小 , 0 -> 无限制 protected boolean existCustomExpire; // 是否设置默认过期时间 public int getCacheSize() { return cacheSize; } protected long defaultExpire; // 默认过期时间, 0 -> 永不过期 public TryCache(int cacheSize, long defaultExpire) { this.cacheSize = cacheSize; this.defaultExpire = defaultExpire; cacheMap = new HashMap<K, CacheObject<K, V>>(cacheSize + 1); } public long getDefaultExpire() { return defaultExpire; } public void put(K key, V value) { put(key, value, defaultExpire); } public void put(K key, V value, long expire) { writeLock.lock(); try { CacheObject<K, V> co = new CacheObject<K, V>(key, value, expire); if (expire != 0) { existCustomExpire = true; } cacheMap.put(key, co); } finally { writeLock.unlock(); } } /** * {@inheritDoc} */ public V get(K key) { readLock.lock(); try { CacheObject<K, V> co = cacheMap.get(key); if (co == null) { return null; } if (co.isExpired() == true) { cacheMap.remove(key); return null; } return co.getObject(); } finally { readLock.unlock(); } } public boolean isFull() { if (cacheSize == 0) {// o -> 无限制 return false; } return cacheMap.size() >= cacheSize; } public void remove(K key) { writeLock.lock(); try { cacheMap.remove(key); } finally { writeLock.unlock(); } } public void clear() { writeLock.lock(); try { cacheMap.clear(); } finally { writeLock.unlock(); } } public int size() { return cacheMap.size(); } public boolean isEmpty() { return size() == 0; } }
GetData.java
static final InforoneCache<String, DatumMining> cache = new InforoneCache<String, DatumMining>(0, 30 * 60 * 1000L); public getData() { Xxx xxx = cache.get(key); if (DatumMining == null) { ... } cache.put(key, xxx); }
跨节点情况请参考:一致性hash算法: cache、负载均衡应用
http://blog.csdn.net/textboy/article/details/46004117
相关文章推荐
- VB中操作Excel的一般做法(读取)
- 提高二维矢量绘图效率之一般做法
- DSP的cache一般在何时会生效,防止在cache使用造成数据不一致
- [导入]VB中操作Excel的一般做法(读取)
- 第4章 O/R Mapping的一般做法
- 提高二维矢量绘图效率之一般做法
- 关于“分叉/联接方案”的一般做法
- WinForm 使用TreeView控件作为导航的 TreeNode(TreeView)点击事件(Click、DoubleClick ~)的一般做法
- [ACM] hihocoder 1062 最近公共祖先·一 (一般做法)
- 关于在C#中使用SQL语的中like及使用查询参数的一般做法__net-技术研发_百度空间
- 第4章 O/R Mapping的一般做法
- 论解决问题的一般做法 - 以 MapServer 为例
- 清除BSS段的一般做法
- DSP的cache一般在何时会生效,防止在cache使用造成数据不一致
- 得到配置资源的一般做法--CLASS.getResourceAsStream(String resource)
- 第4章 O/R Mapping的一般做法
- php数据库配置文件一般做法
- [转]VB中操作Excel的一般做法(读取)
- php数据库配置文件一般做法
- 读取Properties文件的一般做法