java实现缓存

缓存可以分为两大类:

1、通过文件缓存,就是把数据存储在磁盘上,不管是以XML格式、序列化文件DAT还是其他文件格式;

2、内存缓存,也就是实现一个类中静态Map,对这个Map进行常规的增删查

public class Cache {
    /*缓存ID*/
    private String key;
    /*缓存数据*/
    private Object value;
    /*更新时间*/
    private long timeout;
    /*是否终止*/
    private boolean expired;

    public Cache() {
    }

    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 void setKey(String key) {
        this.key = key;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public long getTimeout() {
        return timeout;
    }

    public void setTimeout(long timeout) {
        this.timeout = timeout;
    }

    public boolean isExpired() {
        return expired;
    }

    public void setExpired(boolean expired) {
        this.expired = expired;
    }
}
* 内存缓存就是实现一个类中静态的Map,对这个Map进行常规的增删改查
 *
 * 可扩展功能:当cache内存溢出时必须清理掉最早期的一些缓存对象,
 * 这就要求每个缓存对象保存创建时间
 */
public class CacheManager {
    private static Map cacheMap = new HashMap();
    //单实例构造函数
    public CacheManager() {
        super();
    }
    //获取Boolean值的缓存
    public static boolean getSimpleFlag(String key){
        return (boolean) cacheMap.get(key);
    }

    public static long getServerStartdt(String key){
        return (long) cacheMap.get(key);
    }

    //设置Boolean值的缓存
    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 clearOneType(String type){
        Iterator iterator = cacheMap.keySet().iterator();
        String key;
        List<String> list = new ArrayList<String>();
        while (iterator.hasNext()){
              key = (String) iterator.next();
              if (key.equals(type)){
                  list.add(key);
              }
        }
        for (int i = 0; i < list.size(); i++) {
            cacheMap.remove(list.get(i));
        }
    }

    //载入缓存
    public synchronized static void putCache(String key,Cache ojb){
        cacheMap.put(key,ojb );
    }

    //获取缓存信息
    public static Cache getCacheInfo(String key){
        if (hasCache(key)){
            Cache cache = (Cache) cacheMap.get(key);
            if (cacheExpired(cache)){//判断是否终止方法

            }
        }
        return null;
    }

    private static boolean cacheExpired(Cache cache) {
        if (null == cache){
            return false;//传入的缓存不存在
        }
        /*系统当前毫秒数*/
        long nowTime = System.currentTimeMillis();
        /*缓存内的过期毫秒数*/
        long cacheTimeout = cache.getTimeout();
        /*过期时间小于等于零时,或者过期时间大于当前时间则为false*/
        if (cacheTimeout <= 0 || cacheTimeout > nowTime){
            return false;
        }else {//大于过期时间,即过期
            return true;
        }
    }

    //获取缓存大小
    public static int getCacheSize(){
        return cacheMap.size();
    }
    //获取指定类型的大小
    public static int getCacheSize(String type){
        int k = 0;
        Iterator iterator = cacheMap.keySet().iterator();
        String key;
        while (iterator.hasNext()){
            key = (String) iterator.next();
            if (key.indexOf(type) != -1){//如果匹配则删除掉
                k++;
            }
        }
        return k;
    }

    //获取缓存对象中所有键值名称
    public static List<Set> getAllCacheKeys(){
        return (List<Set>) cacheMap.keySet();
    }
    //获取缓存对象中指定类型的键值名称
    public static List getAllCacheKeyValue(){
        return (List) cacheMap.entrySet();
    }


}

 转:http://www.iteye.com/topic/544021

猜你喜欢

转载自blog.csdn.net/qq_30604989/article/details/81777999