LimitedHashMapCacheImpl.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.cache;

  21. import java.io.OutputStream;
  22. import java.io.Serializable;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.concurrent.ConcurrentHashMap;

  27. import org.openspcoop2.utils.UtilsException;

  28. /**
  29.  * LimitedHashMapCacheImpl
  30.  *
  31.  * @author Poli Andrea (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class LimitedHashMapCacheImpl extends AbstractCacheImpl {


  36.     private Map<String, Serializable> cache = null;
  37.     private int maxSize = -1;
  38.     private int maxLifeTime = -1; // secondi
  39.    

  40.     public LimitedHashMapCacheImpl(String name) throws UtilsException{
  41.         this(name, Cache.DEFAULT_DISABLE_SYNCRONIZED_GET);
  42.     }
  43.     @Deprecated
  44.     private LimitedHashMapCacheImpl(String name, boolean disableSyncronizedGet) throws UtilsException{
  45.         super(CacheType.LimitedHashMap, name);
  46.     }
  47.    
  48.    
  49.     //  *** Inizializzazione ***
  50.    
  51.     @Override
  52.     public int getCacheSize() {
  53.         return this.maxSize;
  54.     }
  55.     @Override
  56.     public void setCacheSize(int cacheSize) {
  57.         this.maxSize = cacheSize;
  58.     }
  59.    
  60.     @Override
  61.     public CacheAlgorithm getCacheAlgoritm() {
  62.         return CacheAlgorithm.LRU;
  63.     }
  64.     @Override
  65.     public void setCacheAlgoritm(CacheAlgorithm cacheAlgoritm) {
  66.         // unsupported
  67.     }
  68.    
  69.     @Override
  70.     public long getItemIdleTime() throws UtilsException{
  71.         return -1;
  72.     }
  73.     @Override
  74.     public void setItemIdleTime(long itemIdleTimeCache) throws UtilsException{
  75.         // unsupported
  76.     }

  77.     @Override
  78.     public long getItemLifeTime() throws UtilsException{
  79.         return this.maxLifeTime;
  80.     }
  81.     @Override
  82.     public void setItemLifeTime(long itemLifeTimeCache) throws UtilsException{
  83.         this.maxLifeTime = (int) itemLifeTimeCache;
  84.     }
  85.    
  86.     @Override
  87.     public void build() throws UtilsException{
  88.         if ( this.maxSize <= 0 && this.maxLifeTime > 0 )
  89.             throw new UtilsException( "Cannot use maxLifeTime without maxSize" );
  90.         if ( this.maxSize <= 0 ) {
  91.             this.cache = new ConcurrentHashMap<String, Serializable>();
  92.         }
  93.         else {
  94.             this.cache = new LimitedHashMap<String, Serializable>( this.cacheName, this.maxSize, this.maxLifeTime );
  95.         }
  96.     }
  97.    
  98.    
  99.    
  100.    
  101.    
  102.     //  *** Gestione ***
  103.    
  104.     @Override
  105.     public void clear() throws UtilsException{
  106.         this.cache.clear();
  107.         if ( this.maxSize > 0 ) {
  108.             this.build(); // reinizializzo
  109.         }
  110.     }
  111.    
  112.     @Override
  113.     public Object get(String key){
  114.         return this.cache.get(key);
  115.     }
  116.    
  117.     @Override
  118.     public void remove(String key) throws UtilsException{
  119.         try{
  120.             this.cache.remove(key);
  121.         }catch(Exception e){
  122.             throw new UtilsException(e.getMessage(),e);
  123.         }  
  124.     }
  125.    
  126.     @Override
  127.     public void put(String key,org.openspcoop2.utils.cache.CacheResponse value) throws UtilsException{
  128.         try{
  129.             this.cache.put(key, value);
  130.         }catch(Exception e){
  131.             throw new UtilsException(e.getMessage(),e);
  132.         }
  133.     }
  134.     @Override
  135.     public void put(String key,Serializable value) throws UtilsException{
  136.         try{
  137.             this.cache.put(key, value);
  138.         }catch(Exception e){
  139.             throw new UtilsException(e.getMessage(),e);
  140.         }
  141.     }
  142.    
  143.     @Override
  144.     public int getItemCount()  throws UtilsException {
  145.         return this.cache.size();
  146.     }
  147.    
  148.     @Override
  149.     public List<String> keys() throws UtilsException {
  150.         try{
  151.             List<String> keys = new ArrayList<>();
  152.             keys.addAll(this.cache.keySet());
  153.             return keys;
  154.         }catch(Exception e){
  155.             throw new UtilsException(e.getMessage(),e);
  156.         }
  157.     }
  158.    
  159.    
  160.    

  161.    
  162.    
  163.     //  *** Info ***
  164.    
  165.     @Override
  166.     public void printStats(OutputStream out, String separator) throws UtilsException{
  167.         this._printStats(out,separator,true);
  168.     }
  169.    
  170.    
  171. }