博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CacheMap缓存的多种方法
阅读量:5088 次
发布时间:2019-06-13

本文共 6869 字,大约阅读时间需要 22 分钟。

(一)

Map
> cache = new ConcurrentHashMap
>();
cache.remove("str"); cache.put("str",getCmsCfgAll());
List
name = cache.get("str"); (二)
package com.jdjinsui.controlservice.tool; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; /**  * 缓存管理类  * Created by zxy on 2017/5/16.  */ public class CacheMgr {
private static Map cacheMap = new HashMap(); private static Map cacheConfMap = new HashMap(); private static CacheMgr cm = null; //构造方法 private CacheMgr(){
} public static CacheMgr getInstance(){
if(cm==null){
cm = new CacheMgr(); Thread t = new ClearCache(); t.start(); } return cm; } /** * 增加缓存 * @param key * @param value * @param ccm 缓存对象 * @return */ public static boolean addCache(Object key,Object value,CacheConfModel ccm){
System.out.println("开始增加缓存-------------"); boolean flag = false; try {
cacheMap.put(key, value); cacheConfMap.put(key, ccm); System.out.println("增加缓存结束-------------"); System.out.println("now addcache=="+cacheMap.size()); flag=true; } catch (Exception e) {
e.printStackTrace(); } return flag; } /** * 获取缓存实体 */ public static Object getValue(String key){
Object ob=cacheMap.get(key); if(ob!=null){
return ob; }else{
return null; } } /** * 获取缓存数据的数量 * @return */ public static int getSize(){
return cacheMap.size(); } /** * 删除缓存 * @param key * @return */ public static boolean removeCache(Object key){
boolean flag=false; try {
cacheMap.remove(key); cacheConfMap.remove(key); flag=true; } catch (Exception e) {
e.printStackTrace(); } return flag; } /** * 清除缓存的类 * 继承Thread线程类 */ private static class ClearCache extends Thread{
public void run(){
while(true){
Set tempSet = new HashSet(); Set set = cacheConfMap.keySet(); Iterator it = set.iterator(); while(it.hasNext()){
Object key = it.next(); CacheConfModel ccm = (CacheConfModel)cacheConfMap.get(key); //比较是否需要清除 if(!ccm.isForever()){
if((new Date().getTime()-ccm.getBeginTime())>= ccm.getDurableTime()*60*1000){
//可以清除,先记录下来 tempSet.add(key); } } } //真正清除 Iterator tempIt = tempSet.iterator(); while(tempIt.hasNext()){
Object key = tempIt.next(); cacheMap.remove(key); cacheConfMap.remove(key); } System.out.println("now thread================>"+cacheMap.size()); //休息 try {
Thread.sleep(60*1000L); } catch (InterruptedException e) {
e.printStackTrace(); } } } } }
package com.jdjinsui.controlservice.tool; /**  * 缓存属性类  * Created by zxy on 2017/5/17.  */ public class CacheConfModel implements java.io.Serializable {
private long beginTime;//缓存开始时间 private boolean isForever = false;//是否持久 private int durableTime;//持续时间 public long getBeginTime() {
return beginTime; } public void setBeginTime(long beginTime) {
this.beginTime = beginTime; } public boolean isForever() {
return isForever; } public void setForever(boolean isForever) {
this.isForever = isForever; } public int getDurableTime() {
return durableTime; } public void setDurableTime(int durableTime) {
this.durableTime = durableTime; } }
//测试方法
Gson gson=new Gson(); CacheMgr cm=CacheMgr.getInstance(); CacheConfModel cModel=new CacheConfModel(); Date d=new Date(); cModel.setBeginTime(d.getTime()); cModel.setDurableTime(60); cModel.setForever(true); cm.addCache("cms", getCmsCfgAll(), cModel);//在缓存加值 CacheMgr cn=CacheMgr.getInstance(); String js=gson.toJson(cn.getValue("cms")); List
user=gson.fromJson(js,new TypeToken
>() {}.getType());
(三)
package com.jdjinsui.controlservice.tool; import java.util.AbstractMap; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; /**  * Created by zxy on 2017/5/17.  */ public class CacheMap
extends AbstractMap
{
private static final long DEFAULT_TIMEOUT = 30000; private static CacheMap
defaultInstance; public static synchronized final CacheMap
getDefault() {
if (defaultInstance == null) {
defaultInstance = new CacheMap
(DEFAULT_TIMEOUT); } return defaultInstance; } private class CacheEntry implements Entry
{ long time; V value; K key; CacheEntry(K key, V value) { super(); this.value = value; this.key = key; this.time = System.currentTimeMillis(); } @Override public K getKey() { return key; } @Override public V getValue() { return value; } @Override public V setValue(V value) { return this.value = value; } } private class ClearThread extends Thread { ClearThread() { setName("clear cache thread"); } public void run() { while (true) { try { long now = System.currentTimeMillis(); Object[] keys = map.keySet().toArray(); for (Object key : keys) { CacheEntry entry = map.get(key); if (now - entry.time >= cacheTimeout) { synchronized (map) { map.remove(key); } } } Thread.sleep(cacheTimeout); } catch (Exception e) { e.printStackTrace(); } } } } private long cacheTimeout; private Map
map = new HashMap
(); public CacheMap(long timeout) { this.cacheTimeout = timeout; new ClearThread().start(); } @Override public Set
> entrySet() { Set
> entrySet = new HashSet
>(); Set
> wrapEntrySet = map.entrySet(); for (Entry
entry : wrapEntrySet) { entrySet.add(entry.getValue()); } return entrySet; } @Override public V get(Object key) { CacheEntry entry = map.get(key); return entry == null ? null : entry.value; } @Override public V put(K key, V value) { CacheEntry entry = new CacheEntry(key, value); synchronized (map) { map.put(key, entry); } return value; } }
 

转载于:https://www.cnblogs.com/ghost-fire/p/6867402.html

你可能感兴趣的文章
Unity 移动主角的时候,鼠标被固定在屏幕中心而且被隐藏
查看>>
自已接触过的数据访问方式总结
查看>>
[2017.02.18] 《调试九法》略读
查看>>
(转) ns2/nam与nam实现相关的文件
查看>>
计算任意文件夹的大小
查看>>
xcode之语法高亮效果消失解决办法
查看>>
Junit--单元测试
查看>>
Win10系列:C#应用控件基础15
查看>>
IIS 错误代码
查看>>
python 字典排序
查看>>
TF-IDF与余弦相似性的应用
查看>>
关于系统用户数,并发用户数,在线用户数,吞吐量(摘)
查看>>
CSS3动画入门 CSS动画如何使用(举例说明)
查看>>
Spring Cloud微服务笔记(五)Feign
查看>>
Canvas 获得键盘焦点的方法
查看>>
Visual Studio Code 写Python 代码
查看>>
django里的http协议
查看>>
判断一个文件是否是指定后缀名的文件
查看>>
随机时间.排序,分出时 分 秒
查看>>
MyBatis开发环境搭建
查看>>