java本地Cache缓存的使用

说明:

java本地缓存的使用,方便快捷

代码:

1、新建一个Cache实体

package com.jdc.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; 
        } 
} 
 

2、管理缓存

package com.jdc.cache;
import java.util.HashMap;
 
 //Description: 管理缓存 
 
 //可扩展的功能:当chche到内存溢出时必须清除掉最早期的一些缓存对象,这就要求对每个缓存对象保存创建时间 

public class CacheManager { 
    private volatile static  HashMap cacheMap ;
    //单实例构造方法 
    private CacheManager() { 
        super(); 
    } 
    
    public static HashMap getCache() {  
        if (cacheMap == null) {  
            synchronized (HashMap.class) {  
                if (cacheMap == null) { 
                    
                    cacheMap = new HashMap();  
                }  
            }  
        }  
        return cacheMap;  
    }


    //得到缓存。同步静态方法 
    private synchronized static Cache getCache(String key) { 
        return (Cache) getCache().get(key); 
    } 
 
    //判断是否存在一个缓存 
    public synchronized static boolean hasCache(String key) { 
        return getCache().containsKey(key); 
    } 

    //获取缓存信息 
    public static Cache getCacheInfo(String key) { 
 
        if (hasCache(key)) { 
            Cache cache = getCache(key); 
            if (cacheExpired(cache)) { //调用判断是否终止方法 
                cache.setExpired(true); 
                return null;
            } 
            return cache; 
        }else 
            return null; 
    } 

    //重写载入缓存信息方法 
    public static void putCacheInfo(String key,Object obj,long dt){ 
        Cache cache = new Cache(); 
        cache.setKey(key);
        if(dt<0){
            cache.setTimeOut(dt);
        }else {
            cache.setTimeOut(dt+System.currentTimeMillis());
        }
        cache.setValue(obj);
        cache.setExpired(false); 
        getCache().put(key,cache); 
    } 
 
    //判断缓存是否终止 
    public static boolean cacheExpired(Cache cache) { 
        if (null == cache) { //传入的缓存不存在 
            return false; 
        } 
        long nowDt = System.currentTimeMillis(); //系统当前的毫秒数 
        long cacheDt = cache.getTimeOut(); //缓存内的过期毫秒数 
        if (cacheDt <= 0||cacheDt>nowDt) { //过期时间小于等于零时,或者过期时间大于当前时间时,则为FALSE 
            return false; 
        } else { //大于过期时间 即过期 
            return true; 
        } 
    }
 
} 
 
 

3、测试缓存

package com.jdc.cache;


public class Test {

    public static void main(String[] args){

        CacheManager.putCacheInfo("pda", "test", 1000*60*60);

        System.out.println("缓存已存入");

        Cache cache = CacheManager.getCacheInfo(String.valueOf("pda"));

        String value = (String)cache.getValue();

        System.out.println(value);
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值