CacheValue 封装的对象 支持时效
- /**
- 封装的缓存对象
- @author Administrator
- @param
- */
- public class CacheValue {
- private V value; // 真实数据对象
- private long times; // 缓存到期时间
- public V getValue() {return value;}
- public void setValue(V value) {
- this.value = value;
- }
- public long getTimes() {
- return times;
- }
- public void setTimes(long times) {
- this.times = times;
- }
- }
CacheMap 缓存 Map 集合
- /**
- 缓存的 map 对象
- @author Administrator
- @param
- */
- public class CacheMap{
- // 缓存 Map
- Map cacheMap=new HashMap ();
- // 保证不能被外界 构造
- CacheMap(){
- }
- public void setex(String key,V value,long times){
- CacheValue cValue=new CacheValue();
- cValue.setTimes(System.currentTimeMillis()+times*1000);
- cValue.setValue(value);
- cacheMap.put(key, cValue);
- }
- public V getValue(String key){
- CacheValue cValue=cacheMap.get(key);
- if(cValue!=null && cValue.getTimes()>System.currentTimeMillis()){
- return cValue.getValue();
- }else if(cValue!=null && cacheMap.containsKey(key)){
- // 为了确保数据被清理 再清理一次
- cacheMap.remove(key);
- }
- return null;
- }
- /**
- 获取 key 集合
- @return
- */
- public Set getKeys(){
- return cacheMap.keySet();
- }
- }
WatchClass 线程实现 守护线程清理过期数据 (也可用 java 定时任务实现, 但消耗更高):
- /**
- 线程
- @author Administrator
- */
- public class WatchClass extends Thread {
- // 获取缓存 Map
- CacheMap<??> cacheMap= CacheFactory.newChacheMap();
- @Override
- public synchronized void run() {
- // 轮询清理
- while(true){
- if(cacheMap.cacheMap!=null){
- try{
- // 获取所有的 key
- Set keySet=cacheMap.cacheMap.keySet();
- // 需要清理的 key
- Set removeKey=new HashSet();
- // 遍历查看是否需要清理
- for (String string : keySet) {
- CacheValue<??> cValue= cacheMap.cacheMap.get(string);
- // 如果有效时间小于系统当前时间 则表示需要清理
- if (cValue.getTimes()
- // 记录需要清理的数据
- removeKey.add(string);
- }
- }
- // 清理数据
- for (String string : removeKey) {
- cacheMap.cacheMap.remove(string);
- }
- // 释放内存
- removeKey=null;
- }catch(Exception e){
- } }}
- }
- }
CacheFactory 类 用于创建全局缓存对象:
- /**
- 缓存工厂, 用于创建缓存集合 单例模式
- @author Administrator
- @param
- */
- @SuppressWarnings('all')
- public class CacheFactory {
- private static CacheMap cacheMap;
- private CacheFactory(){
- }
- /**
- 同步确保数据安全
- @return
- */
- public synchronized static CacheMap newChacheMap(){
- if(cacheMap==null){
- cacheMap=new CacheMap();
- // 开启守护线程 用于清理过式的数据
- WatchClass watch=new WatchClass();
- watch.setDaemon(true);
- watch.start();
- }
- return (CacheMap)cacheMap;
- }
- }
Test 类用于测试缓存的使用
- public class Test {
- /**
- @param a
- @throws InterruptedException
- */
- public static void main(String[] args) throws InterruptedException {
- // TODO Auto-generated method stub
- CacheMap newChacheMap = CacheFactory.newChacheMap();
- for (int i = 1; i <= 1000;=""i++)="">
- newChacheMap.setex('zhangsan'+i, i+'', (new Random()).nextInt(10)+1);
- }
- //newChacheMap.setex('lisi', '李四', 20);
- while(true){
- Thread.sleep(10);
- Set keys=newChacheMap.getKeys();
- System.out.println(newChacheMap.getValue('zhangsan'+(new Random()).nextInt(1000)));
- }
- }
- }
来源: http://www.92to.com/bangong/2018/06-11/33901149.html