コード例 #1
0
  /**
   * Default constructor. Specify the size of the blocks, number of blocks, and the SlabCache this
   * cache will be assigned to.
   *
   * @param blockSize the size of each block, in bytes
   * @param numBlocks the number of blocks of blockSize this cache will hold.
   * @param master the SlabCache this SingleSlabCache is assigned to.
   */
  public SingleSizeCache(int blockSize, int numBlocks, SlabItemActionWatcher master) {
    this.blockSize = blockSize;
    this.numBlocks = numBlocks;
    backingStore = new Slab(blockSize, numBlocks);
    this.stats = new CacheStats();
    this.actionWatcher = master;
    this.size = new AtomicLong(CACHE_FIXED_OVERHEAD + backingStore.heapSize());
    this.timeSinceLastAccess = new AtomicLong();

    // This evictionListener is called whenever the cache automatically
    // evicts something.
    RemovalListener<BlockCacheKey, CacheablePair> listener =
        new RemovalListener<BlockCacheKey, CacheablePair>() {
          @Override
          public void onRemoval(RemovalNotification<BlockCacheKey, CacheablePair> notification) {
            if (!notification.wasEvicted()) {
              // Only process removals by eviction, not by replacement or
              // explicit removal
              return;
            }
            CacheablePair value = notification.getValue();
            timeSinceLastAccess.set(System.nanoTime() - value.recentlyAccessed.get());
            stats.evict();
            doEviction(notification.getKey(), value);
          }
        };

    backingMap =
        CacheBuilder.newBuilder()
            .maximumSize(numBlocks - 1)
            .removalListener(listener)
            .<BlockCacheKey, CacheablePair>build()
            .asMap();
  }
コード例 #2
0
 public long heapSize() {
   return this.size.get() + backingStore.heapSize();
 }