欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中LocalCache本地緩存實現(xiàn)代碼

 更新時間:2017年05月09日 14:38:38   作者:星力量  
本篇文章主要介紹了Java中LocalCache本地緩存實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

前言

本次分享探討java平臺的本地緩存,是指占用JVM的heap區(qū)域來緩沖存儲數(shù)據(jù)的緩存組件。

一、本地緩存應(yīng)用場景

localcache有著極大的性能優(yōu)勢:

1. 單機(jī)情況下適當(dāng)使用localcache會使應(yīng)用的性能得到很大的提升。

2. 集群環(huán)境下對于敏感性要求不高的數(shù)據(jù)可以使用localcache,只配置簡單的失效機(jī)制來保證數(shù)據(jù)的相對一致性。

哪些數(shù)據(jù)可以存儲到本地緩存?

1.訪問頻繁的數(shù)據(jù);

2.靜態(tài)基礎(chǔ)數(shù)據(jù)(長時間內(nèi)不變的數(shù)據(jù));

3.相對靜態(tài)數(shù)據(jù)(短時間內(nèi)不變的數(shù)據(jù))。

二、java本地緩存標(biāo)準(zhǔn)

Java緩存新標(biāo)準(zhǔn)(javax.cache),這個標(biāo)準(zhǔn)由JSR107所提出,已經(jīng)被包含在Java EE 7中。

特性:
1.原子操作,跟java.util.ConcurrentMap類似
2.從緩存中讀取
3.寫入緩存
4.緩存事件監(jiān)聽器
5.?dāng)?shù)據(jù)統(tǒng)計
6.包含所有隔離(ioslation)級別的事務(wù)
7.緩存注解(annotations)
8.保存定義key和值類型的泛型緩存
9.引用保存(只適用于堆緩存)和值保存定義

但目前應(yīng)用不是很普遍。

三、java開源緩存框架

比較有名的本地緩存開源框架有:

1.EHCache

EHCache是一個純java的在進(jìn)程中的緩存,它具有以下特性:快速,簡單,為Hibernate2.1充當(dāng)可插入的緩存,最小的依賴性,全面的文檔和測試。

BUG: 過期失效的緩存元素?zé)o法被GC掉,時間越長緩存越多,內(nèi)存占用越大,導(dǎo)致內(nèi)存泄漏的概率越大。

2.OSCache

OSCache有以下特點(diǎn):緩存任何對象,你可以不受限制的緩存部分jsp頁面或HTTP請求,任何java對象都可以緩存。擁有全面的API--OSCache API給你全面的程序來控制所有的OSCache特性。永久緩存--緩存能隨意的寫入硬盤,因此允許昂貴的創(chuàng)建(expensive-to-create)數(shù)據(jù)來保持緩存,甚至能讓應(yīng)用重啟。支持集群--集群緩存數(shù)據(jù)能被單個的進(jìn)行參數(shù)配置,不需要修改代碼。緩存記錄的過期--你可以有最大限度的控制緩存對象的過期,包括可插入式的刷新策略(如果默認(rèn)性能不需要時)。

3.JCache

Java緩存新標(biāo)準(zhǔn)(javax.cache)

4.cache4j

cache4j是一個有簡單API與實現(xiàn)快速的Java對象緩存。它的特性包括:在內(nèi)存中進(jìn)行緩存,設(shè)計用于多線程環(huán)境,兩種實現(xiàn):同步與阻塞,多種緩存清除策略:LFU, LRU, FIFO,可使用強(qiáng)引用。

5.ShiftOne

ShiftOne Java Object Cache是一個執(zhí)行一系列嚴(yán)格的對象緩存策略的Java lib,就像一個輕量級的配置緩存工作狀態(tài)的框架。

6.WhirlyCache

Whirlycache是一個快速的、可配置的、存在于內(nèi)存中的對象的緩存。 

四、LocalCache實現(xiàn)

1、LocalCache簡介

LocalCache是一個精簡版本地緩存組件,有以下特點(diǎn):

1.  有容量上限maxCapacity;
2.  緩存達(dá)到容量上限時基于LRU策略來移除緩存元素;
3.  緩存對象的生命周期(緩存失效時間)由調(diào)用方?jīng)Q定;
4.  緩存對象失效后,將會有定時清理線程來清理掉,不會導(dǎo)致內(nèi)存泄漏。
5.  性能比Ehcache稍強(qiáng)。

2、總體設(shè)計

LocalCache總體設(shè)計:

1.  緩存元素 CacheElement;
2.  緩存容器 LRULinkedHashMap;
3.  緩存接口 Cache;
4.  緩存組件實現(xiàn) LocalCache。

3、詳細(xì)設(shè)計

1.  CacheElement設(shè)計

/**
 * 緩存元素
 *
 */
public class CacheElement {
 private Object key;
 private Object value;
 private long createTime;
 private long lifeTime;
 private int hitCount;

 public CacheElement() {
 }

 public CacheElement(Object key ,Object value) {
 this.key = key;
 this.value = value;
 this.createTime = System.currentTimeMillis();
 }
 
 public Object getKey() {
 return key;
 }

 public void setKey(Object key) {
 this.key = key;
 }

 public Object getValue() {
 hitCount++;
 return value;
 }

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

 public long getCreateTime() {
 return createTime;
 }

 public void setCreateTime(long createTime) {
 this.createTime = createTime;
 }

 public int getHitCount() {
 return hitCount;
 }

 public void setHitCount(int hitCount) {
 this.hitCount = hitCount;
 }

 public long getLifeTime() {
 return lifeTime;
 }

 public void setLifeTime(long lifeTime) {
 this.lifeTime = lifeTime;
 }
 
 public boolean isExpired() {
 boolean isExpired = System.currentTimeMillis() - getCreateTime() > getLifeTime();
 return isExpired;
 }

 /*
 * (non-Javadoc)
 * @see java.lang.Object#toString()
 */
 public String toString() {
 StringBuffer sb = new StringBuffer();
 sb.append("[ key=").append(key).append(", isExpired=").append(isExpired())
  .append(", lifeTime=").append(lifeTime).append(", createTime=").append(createTime)
  .append(", hitCount=").append(hitCount)
  .append(", value=").append(value).append(" ]");
 return sb.toString();
 }
 
 /*
 * (non-Javadoc)
 * @see java.lang.Object#hashCode()
 */
 public final int hashCode(){
 if(null == key){
  return "".hashCode();
 }
 return this.key.hashCode();
 }
 
 /*
 * (non-Javadoc)
 * @see java.lang.Object#equals(java.lang.Object)
 */
 public final boolean equals(Object object) {
 if ((object == null) || (!(object instanceof CacheElement))) {
  return false;
 }

 CacheElement element = (CacheElement) object;
 if ((this.key == null) || (element.getKey() == null)) {
  return false;
 }

 return this.key.equals(element.getKey());
 }
}

2.  LRULinkedHashMap實現(xiàn)

import java.util.LinkedHashMap;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
 * 實現(xiàn) LRU策略的 LinkedHashMap
 *
 * @param <K>
 * @param <V>
 */
public class LRULinkedHashMap<K, V> extends LinkedHashMap<K, V> 
{ 
 protected static final long serialVersionUID = 2828675280716975892L;
 
 protected static final int DEFAULT_MAX_ENTRIES = 100;
 
 protected final int initialCapacity; 
 protected final int maxCapacity; 
 protected boolean enableRemoveEldestEntry = true;//是否允許自動移除比較舊的元素(添加元素時)
 
 protected static final float DEFAULT_LOAD_FACTOR = 0.8f; 
 protected final Lock lock = new ReentrantLock(); 

  public LRULinkedHashMap(int initialCapacity) 
  { 
   this(initialCapacity, DEFAULT_MAX_ENTRIES);
  }
  
  public LRULinkedHashMap(int initialCapacity ,int maxCapacity) 
  { 
   //set accessOrder=true, LRU
    super(initialCapacity, DEFAULT_LOAD_FACTOR, true); 
    
    this.initialCapacity = initialCapacity; 
    this.maxCapacity = maxCapacity; 
  }

  /*
   *  (non-Javadoc)
   * @see java.util.LinkedHashMap#removeEldestEntry(java.util.Map.Entry)
   */
  protected boolean removeEldestEntry(java.util.Map.Entry<K, V> eldest) 
  { 
    return enableRemoveEldestEntry && ( size() > maxCapacity );
  } 

 /*
 *  (non-Javadoc)
 * @see java.util.LinkedHashMap#get(java.lang.Object)
 */
  public V get(Object key) 
  { 
    try { 
      lock.lock(); 
      return super.get(key); 
    } 
    finally { 
      lock.unlock(); 
    } 
  } 

  /*
   *  (non-Javadoc)
   * @see java.util.HashMap#put(java.lang.Object, java.lang.Object)
   */
  public V put(K key, V value) 
  { 
    try { 
      lock.lock(); 
      return super.put(key, value); 
    } 
    finally { 
      lock.unlock(); 
    } 
  } 
  
  /*
   * (non-Javadoc)
   * @see java.util.HashMap#remove(java.lang.Object)
   */
  public V remove(Object key) {
    try { 
      lock.lock();
      return super.remove(key);
    } 
    finally { 
      lock.unlock(); 
    }
  }
  
  /*
   * (non-Javadoc)
   * @see java.util.LinkedHashMap#clear()
   */
  public void clear() {
   try { 
       lock.lock();
       super.clear();
     } 
     finally { 
       lock.unlock();
     }
  }
  
  /*
   * (non-Javadoc)
   * @see java.util.HashMap#keySet()
   */
  public Set<K> keySet() {
   try { 
      lock.lock();
      return super.keySet();
    } 
    finally { 
      lock.unlock();
    }
  }
  
  public boolean isEnableRemoveEldestEntry() {
 return enableRemoveEldestEntry;
 }
 public void setEnableRemoveEldestEntry(boolean enableRemoveEldestEntry) {
 this.enableRemoveEldestEntry = enableRemoveEldestEntry;
 }
 public int getInitialCapacity() {
 return initialCapacity;
 }
 public int getMaxCapacity() {
 return maxCapacity;
 }
} 

3.  Cache接口設(shè)計

/**
 * 緩存接口
 *
 */
public interface Cache {
 
 /**
 * 獲取緩存
 * @param key
 * @return
 */
 public <T> T getCache(Object key);
 
 /**
 * 緩存對象
 * @param key
 * @param value
 * @param milliSecond 緩存生命周期(毫秒)
 */
 public void putCache(Object key, Object value ,Long milliSecond);
 
 /**
 * 緩存容器中是否包含 key 
 * @param key
 * @return
 */
 public boolean containsKey(Object key);
 
 /**
 * 緩存列表大小
 * @return
 */
 public int getSize();
 
 /**
 * 是否啟用緩存
 */
 public boolean isEnabled();
 /**
 * 啟用 或 停止
 * @param enable
 */
 public void setEnabled(boolean enabled);
 
 /**
 * 移除所有緩存
 */
 public void invalidateCaches();
 
 /**
 * 移除 指定key緩存
 * @param key
 */
 public void invalidateCache(Object key);
}

4.  LocalCache實現(xiàn)

import java.util.Date;
import java.util.Iterator;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 本地緩存組件
 */
public class LocalCache implements Cache{
 private Logger logger = LoggerFactory.getLogger(this.getClass());
 
 private LRULinkedHashMap<Object, CacheElement> cacheMap;
 
 protected boolean initFlag = false;//初始化標(biāo)識
 
 protected final long defaultLifeTime = 5 * 60 * 1000;//5分鐘
 protected boolean warnLongerLifeTime = false;
 
 protected final int DEFAULT_INITIAL_CAPACITY = 100;
 protected final int DEFAULT_MAX_CAPACITY = 100000;
 
 protected int initialCapacity = DEFAULT_INITIAL_CAPACITY;//初始化緩存容量
 protected int maxCapacity = DEFAULT_MAX_CAPACITY;//最大緩存容量
 protected int timeout = 20;//存取緩存操作響應(yīng)超時時間(毫秒數(shù))
 
 private boolean enabled = true;
 
 private Thread gcThread = null;
 private String lastGCInfo = null;//最后一次GC清理信息{ size, removeCount, time ,nowTime}
 private boolean logGCDetail = false;//記錄gc清理細(xì)節(jié)
 
 private boolean enableGC = true;//是否允許清理的緩存(添加元素時)
 private int gcMode = 0;//清理過期元素模式 { 0=迭代模式 ; 1=隨機(jī)模式 }
 private int gcIntervalTime = 2 * 60 * 1000;//間隔時間(分鐘)
 
 private boolean iterateScanAll = true;//是否迭代掃描全部
 private float gcFactor = 0.5F;//清理百分比
 private int maxIterateSize = DEFAULT_MAX_CAPACITY/2;//迭代模式下一次最大迭代數(shù)量
 private volatile int iterateLastIndex = 0;//最后迭代下標(biāo)
 private int maxRandomTimes = 100;//隨機(jī)模式下最大隨機(jī)次數(shù)
 
 
 protected final static Random random = new Random();
 private static LocalCache instance = new LocalCache();
 public static LocalCache getInstance() {
 return instance;
 }
 private LocalCache(){
 this.init();
 }
 
 protected synchronized void init() {
 if(initFlag){
  logger.warn("init repeat.");
  return ;
 }
 
 this.initCache();
 
 this.startGCDaemonThread();
 
 initFlag = true;
 
 if(logger.isInfoEnabled()){
  logger.info("init -- OK");
 }
 }
 
 private void startGCDaemonThread(){
 if(initFlag){
  return ;
 }
 
 this.maxIterateSize = maxCapacity /2;
 try{
  this.gcThread = new Thread() {
  public void run() {
   logger.info("[" + (Thread.currentThread().getName()) + "]start...");
   //sleep
   try {
   Thread.sleep(getGcIntervalTime() < 30000 ? 30000 : getGcIntervalTime());
   } catch (Exception e) {
   e.printStackTrace();
   }
   while( true ){
   //gc
   gc();
   //sleep
   try {
    Thread.sleep(getGcIntervalTime() < 30000 ? 30000 : getGcIntervalTime());
   } catch (Exception e) {
    e.printStackTrace();
   }
   }
  }
  };
  this.gcThread.setName("localCache-gcThread");
  this.gcThread.setDaemon(true);
  this.gcThread.start();
  
  if(logger.isInfoEnabled()){
  logger.info("startGCDaemonThread -- OK");
  }
 }catch(Exception e){
  logger.error("[localCache gc]DaemonThread -- error: " + e.getMessage(), e);
 }
 }
 
 private void initCache(){
 if(initFlag){
  return ;
 }
 
 initialCapacity = (initialCapacity <= 0 ? DEFAULT_INITIAL_CAPACITY : initialCapacity);
 maxCapacity = (maxCapacity < initialCapacity ? DEFAULT_MAX_CAPACITY : maxCapacity);
 
 cacheMap = new LRULinkedHashMap<Object, CacheElement>(initialCapacity ,maxCapacity);
 
 if(logger.isInfoEnabled()){
  logger.info("initCache -- OK");
 }
 }
 
 /*
 * (non-Javadoc)
 */
 @SuppressWarnings("unchecked")
 public <T> T getCache(Object key) {
 if(!isEnabled()){
  return null;
 }
 long st = System.currentTimeMillis();
 
 T objValue = null;
 CacheElement cacheObj = cacheMap.get(key);
 
 if (isExpiredCache(cacheObj)) {
  cacheMap.remove(key);
 }else {
  objValue = (T) (cacheObj == null ? null : cacheObj.getValue());
 }
 
 long et = System.currentTimeMillis();
 if((et - st)>timeout){
  if(this.logger.isWarnEnabled()){
  this.logger.warn("getCache_timeout_" + (et - st) + "_[" + key + "]");
  }
 }
 
 if(logger.isDebugEnabled()){
  String message = ("get( " + key + ") return: " + objValue);
  logger.debug(message);
 }
 return objValue;
 }

 /*
 * (non-Javadoc)
 */
 public void putCache(Object key, Object value ,Long lifeTime) {
 if(!isEnabled()){
  return;
 }
 Long st = System.currentTimeMillis();
 
 lifeTime = (null == lifeTime ? defaultLifeTime : lifeTime);
 CacheElement cacheObj = new CacheElement();
 cacheObj.setCreateTime(System.currentTimeMillis());
 cacheObj.setLifeTime(lifeTime);
 cacheObj.setValue(value);
 cacheObj.setKey(key);
 cacheMap.put(key, cacheObj);
 
 long et = System.currentTimeMillis();
 if((et - st)>timeout){
  if(this.logger.isWarnEnabled()){
  this.logger.warn("putCache_timeout_" + (et - st) + "_[" + key + "]");
  }
 }
 
 if(logger.isDebugEnabled()){
  String message = ("putCache( " + cacheObj + " ) , 耗時 " + (et - st) + "(毫秒).");
  logger.debug(message);
 }
 if(lifeTime > defaultLifeTime && this.isWarnLongerLifeTime()){
  if(logger.isWarnEnabled()){
  String message = ("LifeTime[" + (lifeTime/1000) + "秒] too long for putCache(" + cacheObj + ")");
  logger.warn(message);
  }
 }
 }
 
 /**
 * key 是否過期
 * @param key
 * @return
 */
 protected boolean isExpiredKey(Object key) {
 CacheElement cacheObj = cacheMap.get(key);
 return this.isExpiredCache(cacheObj);
 }
 
 /**
 * cacheObj 是否過期
 * @param key
 * @return
 */
 protected boolean isExpiredCache(CacheElement cacheObj) {
 if (cacheObj == null) {
  return false;
 }
 return cacheObj.isExpired();
 }
 
 /*
 * (non-Javadoc)
 */
 public void invalidateCaches(){
 try{
  cacheMap.clear();
 }catch(Exception e){
  e.printStackTrace();
 }
 }
 
 /*
 * (non-Javadoc)
 */
 public void invalidateCache(Object key){
 try{
  cacheMap.remove(key);
 }catch(Exception e){
  e.printStackTrace();
 }
 }
 
 /*
 * (non-Javadoc)
 */
 public boolean containsKey(Object key) {
 return cacheMap.containsKey(key);
 }

 /*
 * (non-Javadoc)
 */
 public int getSize() {
 return cacheMap.size();
 }

 /*
 * (non-Javadoc)
 */
 public Iterator<Object> getKeyIterator() {
 return cacheMap.keySet().iterator();
 }

 /*
 * (non-Javadoc)
 */
 public boolean isEnabled() {
 return this.enabled;
 }

 /*
 * (non-Javadoc)
 */
 public void setEnabled(boolean enabled) {
 this.enabled = enabled;
 if(!this.enabled){
  //清理緩存
  this.invalidateCaches();
 }
 }
 
  /**
   * 清理過期緩存
   */
  protected synchronized boolean gc(){
   
   if(!isEnableGC()){
   return false;
   }
   
   try{
   
   iterateRemoveExpiredCache();
   
 }catch(Exception e){
  logger.error("gc() has error: " + e.getMessage(), e);
 }
   return true;
  }
  
 /**
 * 迭代模式 - 移除過期的 key
 * @param exceptKey
 */
 private void iterateRemoveExpiredCache(){
 long startTime = System.currentTimeMillis(); 
 
 int size = cacheMap.size();
 if(size ==0){
  return;
 }
 
 int keyCount = 0;
 int removedCount = 0 ;
 
 int startIndex = 0;
 int endIndex = 0;
 
 try{
  Object [] keys = cacheMap.keySet().toArray();
  keyCount = keys.length;
  int maxIndex = keyCount -1 ;
  
  //初始化掃描下標(biāo)
  if(iterateScanAll){
  startIndex = 0;
  endIndex = maxIndex;
  }else {
  int gcThreshold = this.getGcThreshold();
  int iterateLen = gcThreshold > this.maxIterateSize ? this.maxIterateSize : gcThreshold;
  
  startIndex = this.iterateLastIndex;
  startIndex = ( (startIndex < 0 || startIndex > maxIndex) ? 0 : startIndex );
  endIndex = (startIndex + iterateLen);
  endIndex = (endIndex > maxIndex ? maxIndex : endIndex);
  }
  
  //迭代清理
  boolean flag = false;
  for(int i=startIndex; i<= endIndex; i++){
  flag = this.removeExpiredKey(keys[i]);
  if(flag){
   removedCount++;
  }
  }
  
  this.iterateLastIndex = endIndex;
  keys = null;
  
 }catch(Exception e){
  logger.error("iterateRemoveExpiredCache -- 移除過期的 key時出現(xiàn)異常: " + e.getMessage(), e);
 }
 
 long endTime = System.currentTimeMillis();
 
 StringBuffer sb = new StringBuffer();
 sb.append("iterateRemoveExpiredCache [ size: ").append(size).append(", keyCount: ").append(keyCount)
  .append(", startIndex: ").append(startIndex).append(", endIndex: ").append(iterateLastIndex)
  .append(", removedCount: ").append(removedCount).append(", currentSize: ").append(this.cacheMap.size())
  .append(", timeConsuming: ").append(endTime - startTime).append(", nowTime: ").append(new Date())
  .append(" ]");
 this.lastGCInfo = sb.toString();
 
 if(logger.isInfoEnabled()){
  logger.info("iterateRemoveExpiredCache -- 清理結(jié)果 -- "+ lastGCInfo);
 }
 }
 
 /**
 * 隨機(jī)模式 - 移除過期的 key
 */
 private void randomRemoveExpiredCache(){
 long startTime = System.currentTimeMillis(); 
 
 int size = cacheMap.size();
 if(size ==0){
  return;
 }
 
 int removedCount = 0 ;
 try{
  Object [] keys = cacheMap.keySet().toArray();
  int keyCount = keys.length;
  
  boolean removeFlag = false;
  
  int removeRandomTimes = this.getGcThreshold();
  
  removeRandomTimes = ( removeRandomTimes > this.getMaxRandomTimes() ? this.getMaxRandomTimes() : removeRandomTimes );
  while(removeRandomTimes-- > 0){
  
  int index = random.nextInt(keyCount);
  boolean flag = this.removeExpiredKey(keys[index]);
  if(flag){
   removeFlag = true;
   removedCount ++;
  }
  }
  //嘗試 移除 首尾元素
  if(!removeFlag){
   this.removeExpiredKey(keys[0]);
   this.removeExpiredKey(keys[keyCount-1]);
  }
  keys=null;
  
 }catch(Exception e){
      logger.error("randomRemoveExpiredCache -- 移除過期的 key時出現(xiàn)異常: " + e.getMessage(), e);
 }
 long endTime = System.currentTimeMillis();
 
 StringBuffer sb = new StringBuffer();
 sb.append("randomRemoveExpiredCache [ size: ").append(size).append(", removedCount: ").append(removedCount)
  .append(", currentSize: ").append(this.cacheMap.size()).append(", timeConsuming: ").append(endTime - startTime)
  .append(", nowTime: ").append(new Date())
  .append(" ]");
 this.lastGCInfo = sb.toString();
 
 if(logger.isInfoEnabled()){
  logger.info("randomRemoveExpiredCache -- 清理結(jié)果 -- "+ lastGCInfo); 
 }
 }
 
 private boolean removeExpiredKey(Object key){
 boolean flag = false;
 
 CacheElement cacheObj = null;
 if(null != key){
  try{
  cacheObj = cacheMap.get(key);
  boolean isExpiredCache = this.isExpiredCache(cacheObj);
  if(isExpiredCache){
   cacheMap.remove(key);
   flag = true;
  }
  }catch(Exception e){
  logger.error("removeExpired(" + key + ") -- error: " + e.getMessage(), e);
  }
 }
 
 if(!flag && logGCDetail){
  this.logger.warn("removeExpiredKey(" + key + ") return [" + flag + "]--" + cacheObj);
 }
 
 return flag;
 }
 
 public int getInitialCapacity() {
 return initialCapacity;
 }

 public int getMaxCapacity() {
 return maxCapacity;
 }

 public int getGcMode() {
 return gcMode;
 }

 public void setGcMode(int gcMode) {
 this.gcMode = gcMode;
 }

 public int getGcIntervalTime() {
 return gcIntervalTime;
 }

 public void setGcIntervalTime(int gcIntervalTime) {
 this.gcIntervalTime = gcIntervalTime;
 }

 public boolean isEnableGC() {
 return enableGC;
 }

 public void setEnableGC(boolean enableGC) {
 this.enableGC = enableGC;
 }
 
 public boolean isIterateScanAll() {
 return iterateScanAll;
 }
 public void setIterateScanAll(boolean iterateScanAll) {
 this.iterateScanAll = iterateScanAll;
 }
 public float getGcFactor() {
 return gcFactor;
 }

 public void setGcFactor(float gcFactor) {
 this.gcFactor = gcFactor;
 }
 
 /**
 * gc 閥值
 * @return
 */
 public int getGcThreshold() {
 int threshold = (int)( this.cacheMap.getMaxCapacity() * gcFactor );
 return threshold;
 }

 public String getLastGCInfo() {
 return lastGCInfo;
 }

 public void setLastGCInfo(String lastGCInfo) {
 this.lastGCInfo = lastGCInfo;
 }
 
 public boolean isLogGCDetail() {
 return logGCDetail;
 }
 public void setLogGCDetail(boolean logGCDetail) {
 this.logGCDetail = logGCDetail;
 }
 public int getTimeout() {
 return timeout;
 }
 public void setTimeout(int timeout) {
 this.timeout = timeout;
 }
 public int getMaxIterateSize() {
 return maxIterateSize;
 }
 public void setMaxIterateSize(int maxIterateSize) {
 this.maxIterateSize = maxIterateSize;
 }
 public int getMaxRandomTimes() {
 return maxRandomTimes;
 }
 public void setMaxRandomTimes(int maxRandomTimes) {
 this.maxRandomTimes = maxRandomTimes;
 }
 public boolean isInitFlag() {
 return initFlag;
 }
 public long getDefaultLifeTime() {
 return defaultLifeTime;
 }

 public boolean isWarnLongerLifeTime() {
 return warnLongerLifeTime;
 }

 public void setWarnLongerLifeTime(boolean warnLongerLifeTime) {
 this.warnLongerLifeTime = warnLongerLifeTime;
 }

 //======================== dynMaxCapacity ========================
 private int dynMaxCapacity = maxCapacity;
 public int getDynMaxCapacity() {
 return dynMaxCapacity;
 }
 public void setDynMaxCapacity(int dynMaxCapacity) {
 this.dynMaxCapacity = dynMaxCapacity;
 }
 public void resetMaxCapacity(){
 if(dynMaxCapacity > initialCapacity && dynMaxCapacity != maxCapacity){
  
  if(logger.isInfoEnabled()){
  logger.info("resetMaxCapacity( " + dynMaxCapacity + " ) start...");
  }
  
  synchronized(cacheMap){
  LRULinkedHashMap<Object, CacheElement> cacheMap0 = new LRULinkedHashMap<Object, CacheElement>(initialCapacity ,dynMaxCapacity);
  cacheMap.clear();
  cacheMap = cacheMap0;
  this.maxCapacity = dynMaxCapacity;
  }
  
  if(logger.isInfoEnabled()){
  logger.info("resetMaxCapacity( " + dynMaxCapacity + " ) OK.");
  }
 }else {
  if(logger.isWarnEnabled()){
  logger.warn("resetMaxCapacity( " + dynMaxCapacity + " ) NO.");
  }
 }
 }
 //======================== showCacheElement ========================
 private String showCacheKey;
 public String getShowCacheKey() {
 return showCacheKey;
 }
 public void setShowCacheKey(String showCacheKey) {
 this.showCacheKey = showCacheKey;
 }
 public Object showCacheElement(){
 Object v = null;
 
 if(null != this.showCacheKey){
  v = cacheMap.get(showCacheKey);
 }
 
 return v;
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 使用ObjectMapper把Json轉(zhuǎn)換為復(fù)雜的實體類

    使用ObjectMapper把Json轉(zhuǎn)換為復(fù)雜的實體類

    這篇文章主要介紹了使用ObjectMapper把Json轉(zhuǎn)換為復(fù)雜的實體類操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • SpringBoot加載多個配置文件實現(xiàn)dev、product多環(huán)境切換的方法

    SpringBoot加載多個配置文件實現(xiàn)dev、product多環(huán)境切換的方法

    這篇文章主要介紹了SpringBoot加載多個配置文件實現(xiàn)dev、product多環(huán)境切換,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • Java NumberFormat格式化float類型的bug

    Java NumberFormat格式化float類型的bug

    今天小編就為大家分享一篇關(guān)于Java NumberFormat格式化float類型的bug,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • Java中獲取類路徑classpath的簡單方法(推薦)

    Java中獲取類路徑classpath的簡單方法(推薦)

    下面小編就為大家?guī)硪黄狫ava中獲取類路徑classpath的簡單方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-09-09
  • 如何通過Java實現(xiàn)修改視頻分辨率

    如何通過Java實現(xiàn)修改視頻分辨率

    Java除了可以修改圖片的分辨率,還可以實現(xiàn)修改視頻的分辨率,這篇文章就將帶大家學(xué)習(xí)如果編寫這一工具類,感興趣的同學(xué)可以了解一下
    2021-12-12
  • Java編碼算法與哈希算法深入分析使用方法

    Java編碼算法與哈希算法深入分析使用方法

    首先,我們一起來學(xué)習(xí)一下編碼算法,舉例說明,ASCII碼就是我們常見的一種編碼,字母a的編碼是十六進(jìn)制的0x61,字母b是0x62,以此類推。哈希算法,可被稱為摘要算法。因此,哈希算法的加密是單向的,不可用密文解密得到明文
    2022-11-11
  • Java數(shù)據(jù)結(jié)構(gòu)之線索化二叉樹的實現(xiàn)

    Java數(shù)據(jù)結(jié)構(gòu)之線索化二叉樹的實現(xiàn)

    在二叉樹的結(jié)點(diǎn)上加上線索的二叉樹稱為線索二叉樹,對二叉樹以某種遍歷方式進(jìn)行遍歷,使其變?yōu)榫€索二叉樹的過程稱為對二叉樹進(jìn)行線索化。本文將詳解如何實現(xiàn)線索化二叉樹,需要的可以參考一下
    2022-05-05
  • JavaWeb Session失效時間設(shè)置方法

    JavaWeb Session失效時間設(shè)置方法

    這篇文章主要介紹了JavaWeb Session失效時間設(shè)置方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-12-12
  • java?字段值為null,不返回該字段的問題

    java?字段值為null,不返回該字段的問題

    這篇文章主要介紹了java?字段值為null,不返回該字段的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • ReentrantLock 非公平鎖實現(xiàn)原理詳解

    ReentrantLock 非公平鎖實現(xiàn)原理詳解

    這篇文章主要為大家介紹了ReentrantLock 非公平鎖實現(xiàn)原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12

最新評論