欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

本地并发缓存方案

程序员文章站 2022-06-19 11:00:25
...
package com.hcyt.common.sqnUtils;
import java.io.Serializable;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;



//-- 本地并发缓存方案-----\\
/** 
 * @author jack.liang
 *
 */
@Component
public class CacheConrrentUtils  {
	 
	 private static final Logger logger = LogManager.getLogger(CacheConrrentUtils.class);  
	
	 public static  ConcurrentHashMap<String, Serializable>  cacheMap =  new  ConcurrentHashMap<String, Serializable>();
	 
	 public static  ConcurrentHashMap<String, Object>  _cacheMapO =  new  ConcurrentHashMap<String, Object>();
	 
     private volatile static CacheConrrentUtils mapCacheObject;// 缓存实例对象  
     
     private  static final ReentrantLock lock = new ReentrantLock ();
     
     private long DEFAULT_EXPIRY = 1000*60*60*24*1;  //默认两天
     
     private HashMap<String, Long> expiryMap = new HashMap<>();  
     
    /** 
     * 采用单例模式获取缓存对象实例 
     *  
     * @return 
     */  
    public static CacheConrrentUtils getInstance()
    {  
    	if (null == mapCacheObject) 
    	{  
    		    try {
    		    	lock.lock();
				
    		    	if (null == mapCacheObject) 
                {  
                    mapCacheObject = new CacheConrrentUtils();  
                    logger.info("本都缓存单例实例创建",mapCacheObject);
                }  
 			} finally {
				lock.unlock();
	   	  }
             
        }  
        return mapCacheObject;  
    }  
	/**  缓存KEY
	 * @param key
	 * @param account
	 * @return
	 */
	public  String  setCacheKey(String key ,String account)
	{
		if (StringUtils.hasLength(key)&&(account!=null))
		{
			expiryMap.put(key, System.currentTimeMillis()+DEFAULT_EXPIRY); 
			cacheMap.put(key, account);
			return key;
		} 
		else 
			return "null";
			
	}
	
	
	
	public  String  setCacheKeyO(String key ,Object account)
	{
		if (StringUtils.hasLength(key)&&(account!=null))
		{
			expiryMap.put(key, System.currentTimeMillis()+DEFAULT_EXPIRY); 
			_cacheMapO.put(key, account);
			return key;
		} 
		else 
			return "null";
			
	}
	
	
	
	/**  拿出缓存KEY
	 * @param key
	 * @param account
	 * @return
	 */
	public  String  getCachevalue(String key )
	{
		  if (StringUtils.hasLength(key)&&!checkExpiry(key))
			  key=   classInstanceOfDetectType(cacheMap.get(key)+"");
		  else 
			  key=null;
		  return key;
		
	}
	
	
	private String   classInstanceOfDetectType(String obj)
	{
   		    if(obj instanceof String)
   		    	return  (String) obj;
   		    else
   		    {
   		    	if(obj==null) 
   		    		obj=null;
   		    	else{
   		    		
   		    		String name= obj.getClass().toString();
   		    		String substring = name.substring(name.lastIndexOf('.') + 1, name.length());//类型
   		    		throw new  ClassCastException("类型转换异常"+substring);
   		    	} 
   		    	
   		    	return "null"; 
   		    } 
   		    	
	}
	
	/*
	 * 过期SET_TTL
	 */
	@SuppressWarnings("unused")
	private  String  setTllKey(String key , String val ,long  ttl)
	{
		if(!StringUtils.hasLength(key))
			return null ; 
	    
		if (ttl==0l)
			expiryMap.put(key, System.currentTimeMillis()+DEFAULT_EXPIRY);
		else 
	 	    expiryMap.put(key, System.currentTimeMillis()+ttl);  
		
	    
	    cacheMap.put(key, val);

	return key;
	}
	
	
	protected boolean checkExpiry(String key)
	 {  
          
		 	if(!expiryMap.containsKey(key))
	       		return Boolean.FALSE;  
	  	 	 
	        boolean flag = System.currentTimeMillis()>expiryMap.get(key);  

	        return flag;  
	    }  
	 
	 
	public boolean isEmpty() 
	{
		return cacheMap.size() == 0||cacheMap == null;
	}
	
	public  int  size ()
	{
		return cacheMap.size();
	}

	
}

 

相关标签: 怀旧 技术记录