package com.cache; 

    import java.util.*; 

     //Description: 管理缓存 

     //可扩展的功能:当chche到内存溢出时必须清除掉最早期的一些缓存对象,这就要求对每个缓存对象保存创建时间 

    public class CacheManager {
private static HashMap cacheMap = new HashMap(); //单实例构造方法
private CacheManager() {
super();
}
//获取布尔值的缓存
public static boolean getSimpleFlag(String key){
try{
return (Boolean) cacheMap.get(key);
}catch(NullPointerException e){
return false;
}
}
public static long getServerStartdt(String key){
try {
return (Long)cacheMap.get(key);
} catch (Exception ex) {
return ;
}
}
//设置布尔值的缓存
public synchronized static boolean setSimpleFlag(String key,boolean flag){
if (flag && getSimpleFlag(key)) {//假如为真不允许被覆盖
return false;
}else{
cacheMap.put(key, flag);
return true;
}
}
public synchronized static boolean setSimpleFlag(String key,long serverbegrundt){
if (cacheMap.get(key) == null) {
cacheMap.put(key,serverbegrundt);
return true;
}else{
return false;
}
} //得到缓存。同步静态方法
private synchronized static Cache getCache(String key) {
return (Cache) cacheMap.get(key);
} //判断是否存在一个缓存
private synchronized static boolean hasCache(String key) {
return cacheMap.containsKey(key);
} //清除所有缓存
public synchronized static void clearAll() {
cacheMap.clear();
} //清除某一类特定缓存,通过遍历HASHMAP下的所有对象,来判断它的KEY与传入的TYPE是否匹配
public synchronized static void clearAll(String type) {
Iterator i = cacheMap.entrySet().iterator();
String key;
ArrayList arr = new ArrayList();
try {
while (i.hasNext()) {
java.util.Map.Entry entry = (java.util.Map.Entry) i.next();
key = (String) entry.getKey();
if (key.startsWith(type)) { //如果匹配则删除掉
arr.add(key);
}
}
for (int k = ; k < arr.size(); k++) {
clearOnly(arr.get(k));
}
} catch (Exception ex) {
ex.printStackTrace();
}
} //清除指定的缓存
public synchronized static void clearOnly(String key) {
cacheMap.remove(key);
} //载入缓存
public synchronized static void putCache(String key, Cache obj) {
cacheMap.put(key, obj);
} //获取缓存信息
public static Cache getCacheInfo(String key) { if (hasCache(key)) {
Cache cache = getCache(key);
if (cacheExpired(cache)) { //调用判断是否终止方法
cache.setExpired(true);
}
return cache;
}else
return null;
} //载入缓存信息
public static void putCacheInfo(String key, Cache obj, long dt,boolean expired) {
Cache cache = new Cache();
cache.setKey(key);
cache.setTimeOut(dt + System.currentTimeMillis()); //设置多久后更新缓存
cache.setValue(obj);
cache.setExpired(expired); //缓存默认载入时,终止状态为FALSE
cacheMap.put(key, cache);
}
//重写载入缓存信息方法
public static void putCacheInfo(String key,Cache obj,long dt){
Cache cache = new Cache();
cache.setKey(key);
cache.setTimeOut(dt+System.currentTimeMillis());
cache.setValue(obj);
cache.setExpired(false);
cacheMap.put(key,cache);
} //判断缓存是否终止
public static boolean cacheExpired(Cache cache) {
if (null == cache) { //传入的缓存不存在
return false;
}
long nowDt = System.currentTimeMillis(); //系统当前的毫秒数
long cacheDt = cache.getTimeOut(); //缓存内的过期毫秒数
if (cacheDt <= ||cacheDt>nowDt) { //过期时间小于等于零时,或者过期时间大于当前时间时,则为FALSE
return false;
} else { //大于过期时间 即过期
return true;
}
} //获取缓存中的大小
public static int getCacheSize() {
return cacheMap.size();
} //获取指定的类型的大小
public static int getCacheSize(String type) {
int k = ;
Iterator i = cacheMap.entrySet().iterator();
String key;
try {
while (i.hasNext()) {
java.util.Map.Entry entry = (java.util.Map.Entry) i.next();
key = (String) entry.getKey();
if (key.indexOf(type) != -) { //如果匹配则删除掉
k++;
}
}
} catch (Exception ex) {
ex.printStackTrace();
} return k;
} //获取缓存对象中的所有键值名称
public static ArrayList getCacheAllkey() {
ArrayList a = new ArrayList();
try {
Iterator i = cacheMap.entrySet().iterator();
while (i.hasNext()) {
java.util.Map.Entry entry = (java.util.Map.Entry) i.next();
a.add((String) entry.getKey());
}
} catch (Exception ex) {} finally {
return a;
}
} //获取缓存对象中指定类型 的键值名称
public static ArrayList getCacheListkey(String type) {
ArrayList a = new ArrayList();
String key;
try {
Iterator i = cacheMap.entrySet().iterator();
while (i.hasNext()) {
java.util.Map.Entry entry = (java.util.Map.Entry) i.next();
key = (String) entry.getKey();
if (key.indexOf(type) != -) {
a.add(key);
}
}
} catch (Exception ex) {} finally {
return a;
}
} } package com.cache; public class Cache {
private String key;//缓存ID
private Object value;//缓存数据
private long timeOut;//更新时间
private boolean expired; //是否终止
public Cache() {
super();
} public Cache(String key, Object value, long timeOut, boolean expired) {
this.key = key;
this.value = value;
this.timeOut = timeOut;
this.expired = expired;
} public String getKey() {
return key;
} public long getTimeOut() {
return timeOut;
} public Object getValue() {
return value;
} public void setKey(String string) {
key = string;
} public void setTimeOut(long l) {
timeOut = l;
} public void setValue(Object object) {
value = object;
} public boolean isExpired() {
return expired;
} public void setExpired(boolean b) {
expired = b;
}
} //测试类,
class Test {
public static void main(String[] args) {
System.out.println(CacheManager.getSimpleFlag("alksd"));
// CacheManager.putCache("abc", new Cache());
// CacheManager.putCache("def", new Cache());
// CacheManager.putCache("ccc", new Cache());
// CacheManager.clearOnly("");
// Cache c = new Cache();
// for (int i = 0; i < 10; i++) {
// CacheManager.putCache("" + i, c);
// }
// CacheManager.putCache("aaaaaaaa", c);
// CacheManager.putCache("abchcy;alskd", c);
// CacheManager.putCache("cccccccc", c);
// CacheManager.putCache("abcoqiwhcy", c);
// System.out.println("删除前的大小:"+CacheManager.getCacheSize());
// CacheManager.getCacheAllkey();
// CacheManager.clearAll("aaaa");
// System.out.println("删除后的大小:"+CacheManager.getCacheSize());
// CacheManager.getCacheAllkey(); }
}

HashMap实现缓存(二)的更多相关文章

  1. OpenJDK1.8.0 源码解析————HashMap的实现(二)

    上一篇文章介绍了HashMap的一部分的知识,算是为下面HashMap的进一步学习做准备吧. 然后写的时候一直在思考的一个问题是,这方面的知识网上的资料也是一抓一大把,即使是这样我为什么还要花费时间去 ...

  2. mybatis一级缓存和二级缓存(二)

    注意事项与示例配置 一级缓存 Mybatis对缓存提供支持,但是在没有配置的默认情况下,它只开启一级缓存,一级缓存只是相对于同一个SqlSession而言.所以在参数和SQL完全一样的情况下,我们使用 ...

  3. HashMap实现缓存

    package com.cache; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.a ...

  4. C#分布式缓存二:Asp.Net中使用Couchbase

    前言 上一篇<C#分布式缓存一:Couchbase的安装与简单使用>主要讲解对Couchbase服务端的安装配置和客户端的引用调用,通过代码来完成最简单的实现调用.本次通过简单的配置,来完 ...

  5. Android中FragmentPagerAdapter对Fragment的缓存(二)

    上一篇我们谈到了,当应用程序恢复时,由于FragmentPagerAdapter对Fragment进行了缓存的读取,导致其并未使用在Activity中新创建的Fragment实例.今天我们来看如何解决 ...

  6. HashMap深度解析(二)

    本文来自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/16890151 上一篇比较深入的分析了HashMap在put元素时的整体过 ...

  7. redis(五)redis与Mybatis的无缝整合让MyBatis透明的管理缓存二

    在上一篇文中的Cahe类存在各种问题如:一直使用同一个连接,每次都创建新的Cache,项目中老是爆出connection timeout 的异常,存储的key过长等等一系列的问题,解决问题最好的办法就 ...

  8. Spring Cache扩展:注解失效时间+主动刷新缓存(二)

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  9. hashmap为什么是二倍扩容?

    这个很简单,首先我们考虑一个问题,为什么hashmap的容量为2的幂次方,查看源码即可发现在计算存储位置时,计算式为: (n-1)&hash(key) 容量n为2的幂次方,n-1的二进制会全为 ...

随机推荐

  1. matlab绘图基础

    matlab绘制条形图并分组显示: a =[1 2 3] b =[4 5 6] >> d=[a;b] d = 1 2 3 4 5 6 >> bar(d,'group') 修改横 ...

  2. gnu coreutils-8.25 for win32 static - Beta

    gnu.win32-coreutils-8.25.7z 2.7 Mb bc-1.06.tar.gz coreutils-8.25.tar.xz diffutils-3.5.tar.xz gawk-4. ...

  3. java---构造器

    public class SomeTrying{ public static void main(String[] args){ new Son(); new Son().Father(); } } ...

  4. 412. Fizz Buzz

    https://leetcode.com/problems/fizz-buzz/ 没什么好说的,上一个小学生解法 class Solution(object): def fizzBuzz(self, ...

  5. USB_HID读写上位机VC++

    在工程属性-->链接器-->添加以下库 open 打开,close 关闭,打开后将获得reader 与writer 的handle,分别进行读写即可 #pragma once #ifdef ...

  6. 获取json数据

    通过异步获取json来展示数据表格,性能提高不少.实例如下: 前台: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999 ...

  7. Delphi之通过代码示例学习XML解析、StringReplace的用法

    这个程序可以用于解析任何合法的XML字符串. 首先是看一下程序的运行效果: 以解析这样一个XML的字符串为例: <?xml version="1.0" encoding=&q ...

  8. 【Java EE 学习 72 上】【数据采集系统第四天】【增加调查logo】【文件上传】【动态错误页指定】【上传限制】【国际化】

    增加logo的技术点:文件上传,国际化 文件上传的功能在struts2中是使用文件上传拦截器完成的. 1.首先需要在页面上添加一个文件上传的超链接. 点击该超链接能够跳转到文件上传页面.我给该表单页面 ...

  9. Scrapy shell调试网页的信息

    通过scrapy shell "http://www.thinkive.cn:10000/zentaopms/www/index.php?m=user&f=login"

  10. apt28组织新的flash漏洞利用包dealerschoice分析

    17号paloalto发布了文章dealerschoice-sofacys-flash-player-exploit-platform,文中提到apt28正在编写adobe flash player的 ...