com.googlecode.concurrentlinkedhashmap.EvictionListener Java Examples

The following examples show how to use com.googlecode.concurrentlinkedhashmap.EvictionListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LeastRecentlyUsedTransienceManager.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
LeastRecentlyUsedTransienceManager(int minimunSize, int maximunSize, int concurrencyLevel, TransienceMonitor monitor) {
    Builder<K, T> builder = new Builder<>();
    builder.initialCapacity(minimunSize);
    builder.maximumWeightedCapacity(maximunSize);
    builder.concurrencyLevel(concurrencyLevel);
    if (monitor != null) {
        builder.listener(new EvictionListener<K, T>() {

            public void onEviction(K key, T value) {
                monitor.notifyExchanged(key, value);
            };

        });
    }
    this.transience = builder.build();
}
 
Example #2
Source File: SerializingCache.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private SerializingCache(long capacity, Weigher<RefCountedMemory> weigher, ISerializer<V> serializer)
{
    this.serializer = serializer;

    EvictionListener<K,RefCountedMemory> listener = new EvictionListener<K, RefCountedMemory>()
    {
        public void onEviction(K k, RefCountedMemory mem)
        {
            mem.unreference();
        }
    };

    this.map = new ConcurrentLinkedHashMap.Builder<K, RefCountedMemory>()
               .weigher(weigher)
               .maximumWeightedCapacity(capacity)
               .concurrencyLevel(DEFAULT_CONCURENCY_LEVEL)
               .listener(listener)
               .build();
}
 
Example #3
Source File: DeepPagingCache.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
public DeepPagingCache(long maxEntriesForDeepPaging) {
  _hits = Metrics.newMeter(new MetricName(ORG_APACHE_BLUR, DEEP_PAGING_CACHE, HIT), HIT, TimeUnit.SECONDS);
  _misses = Metrics.newMeter(new MetricName(ORG_APACHE_BLUR, DEEP_PAGING_CACHE, MISS), MISS, TimeUnit.SECONDS);
  _evictions = Metrics.newMeter(new MetricName(ORG_APACHE_BLUR, DEEP_PAGING_CACHE, EVICTION), EVICTION,
      TimeUnit.SECONDS);
  _lruCache = new ConcurrentLinkedHashMap.Builder<DeepPageKeyPlusPosition, DeepPageContainer>()
      .maximumWeightedCapacity(maxEntriesForDeepPaging)
      .listener(new EvictionListener<DeepPageKeyPlusPosition, DeepPageContainer>() {
        @Override
        public void onEviction(DeepPageKeyPlusPosition key, DeepPageContainer value) {
          _positionCache.remove(key);
          _evictions.mark();
        }
      }).build();
  Metrics.newGauge(new MetricName(ORG_APACHE_BLUR, DEEP_PAGING_CACHE, SIZE), new Gauge<Long>() {
    @Override
    public Long value() {
      return _lruCache.weightedSize();
    }
  });
  _positionCache = new ConcurrentSkipListMap<DeepPageKeyPlusPosition, DeepPageContainer>();
}
 
Example #4
Source File: CacheMatrix.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
public CacheMatrix(int minimunSize, int maximunSize, int concurrencyLevel) {
    Builder<Integer, MathVector> builder = new Builder<>();
    builder.initialCapacity(minimunSize);
    builder.maximumWeightedCapacity(maximunSize);
    builder.concurrencyLevel(concurrencyLevel);
    builder.listener(new EvictionListener<Integer, MathVector>() {

        public void onEviction(Integer key, MathVector value) {
        };

    });
    this.cache = builder.build();
}
 
Example #5
Source File: ClhmStatementCache.java    From vibur-dbcp with Apache License 2.0 5 votes vote down vote up
/**
 * Creates and returns a new EvictionListener for the CLHM. It is worth noting that this
 * EvictionListener is called in the context of the thread that has executed an insert (putIfAbsent)
 * operation which has increased the CLHM size above its maxSize - in which case the CLHM
 * evicts its LRU entry.
 *
 * @return a new EvictionListener for the CLHM
 */
private static EvictionListener<StatementMethod, StatementHolder> getListener() {
    return new EvictionListener<StatementMethod, StatementHolder>() {
        @Override
        public void onEviction(StatementMethod statementMethod, StatementHolder statementHolder) {
            if (statementHolder.state().getAndSet(EVICTED) == AVAILABLE) {
                quietClose(statementHolder.rawStatement());
            }
            if (logger.isTraceEnabled()) {
                logger.trace("Evicted {}", statementHolder.rawStatement());
            }
        }
    };
}
 
Example #6
Source File: GoogleConcurrentLruCache.java    From tddl with Apache License 2.0 5 votes vote down vote up
public GoogleConcurrentLruCache(int capacity, EvictionListener<K, V> listener){
    if (capacity <= 0) {
        capacity = DEFAULT_CAPACITY;
    }

    cache = new ConcurrentLinkedHashMap.Builder<K, V>().maximumWeightedCapacity(capacity)
        .weigher(Weighers.singleton())
        .concurrencyLevel(DEFAULT_CONCURENCY_LEVEL)
        .listener(listener)
        .build();
}
 
Example #7
Source File: GoogleConcurrentLruCacheTest.java    From tddl with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimple() {

    GoogleConcurrentLruCache cache = new GoogleConcurrentLruCache(10, new EvictionListener<String, String>() {

        public void onEviction(String key, String value) {
            System.out.println("evict key:" + key + " values:" + value);
        }
    });

    for (int i = 0; i < 11; i++) {
        cache.put("key" + i, "value" + i);
    }
}
 
Example #8
Source File: TableBlockCache.java    From heftydb with Apache License 2.0 5 votes vote down vote up
public TableBlockCache(long maxSize, Weigher<T> weigher) {
    cache = new ConcurrentLinkedHashMap.Builder<Entry, T>().concurrencyLevel(CONCURRENCY_LEVEL).weigher(weigher)
            .listener(new EvictionListener<Entry, T>() {
        @Override
        public void onEviction(Entry key, T value) {
            totalSize.addAndGet(-(value.memory().size()));
            value.memory().release();
        }
    }).maximumWeightedCapacity(maxSize).build();
    this.maxSize = maxSize;
}
 
Example #9
Source File: BlockCache.java    From incubator-retired-blur with Apache License 2.0 4 votes vote down vote up
public BlockCache(boolean directAllocation, long totalMemory, int slabSize) {
  _numberOfBlocksPerSlab = slabSize / _blockSize;
  _numberOfSlabs = (int) (totalMemory / slabSize);
  _directAllocation = directAllocation;

  _slabs = new ByteBuffer[_numberOfSlabs];
  _locks = new BlockLocks[_numberOfSlabs];
  _lockCounters = new AtomicInteger[_numberOfSlabs];
  _maxEntries = (_numberOfBlocksPerSlab * _numberOfSlabs) - 1;
  for (int i = 0; i < _numberOfSlabs; i++) {
    if (!lazy) {
      if (_directAllocation) {
        _slabs[i] = ByteBuffer.allocateDirect(_numberOfBlocksPerSlab * _blockSize);
      } else {
        _slabs[i] = ByteBuffer.allocate(_numberOfBlocksPerSlab * _blockSize);
      }
    }
    _locks[i] = new BlockLocks(_numberOfBlocksPerSlab);
    _lockCounters[i] = new AtomicInteger();
  }

  evictions = Metrics.newMeter(new MetricName(ORG_APACHE_BLUR, CACHE, EVICTION), EVICTION, TimeUnit.SECONDS);
  Metrics.newGauge(new MetricName(ORG_APACHE_BLUR, CACHE, ENTRIES), new Gauge<Long>() {
    @Override
    public Long value() {
      return (long) getSize();
    }
  });
  Metrics.newGauge(new MetricName(ORG_APACHE_BLUR, CACHE, SIZE), new Gauge<Long>() {
    @Override
    public Long value() {
      return ((long) getSize()) * (long) _8K;
    }
  });

  EvictionListener<BlockCacheKey, BlockCacheLocation> listener = new EvictionListener<BlockCacheKey, BlockCacheLocation>() {
    @Override
    public void onEviction(BlockCacheKey key, BlockCacheLocation location) {
      releaseLocation(location);
      evictions.mark();
    }
  };
  _cache = new ConcurrentLinkedHashMap.Builder<BlockCacheKey, BlockCacheLocation>()
      .maximumWeightedCapacity(_maxEntries).listener(listener).build();
}