1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. 2 // This source code is licensed under both the GPLv2 (found in the 3 // COPYING file in the root directory) and Apache 2.0 License 4 // (found in the LICENSE.Apache file in the root directory). 5 6 package org.rocksdb; 7 8 /** 9 * Least Recently Used Cache 10 */ 11 public class LRUCache extends Cache { 12 13 /** 14 * Create a new cache with a fixed size capacity 15 * 16 * @param capacity The fixed size capacity of the cache 17 */ 18 public LRUCache(final long capacity) { 19 this(capacity, -1, false, 0.0); 20 } 21 22 /** 23 * Create a new cache with a fixed size capacity. The cache is sharded 24 * to 2^numShardBits shards, by hash of the key. The total capacity 25 * is divided and evenly assigned to each shard. 26 * numShardBits = -1 means it is automatically determined: every shard 27 * will be at least 512KB and number of shard bits will not exceed 6. 28 * 29 * @param capacity The fixed size capacity of the cache 30 * @param numShardBits The cache is sharded to 2^numShardBits shards, 31 * by hash of the key 32 */ 33 public LRUCache(final long capacity, final int numShardBits) { 34 super(newLRUCache(capacity, numShardBits, false,0.0)); 35 } main(void)36 37 /** 38 * Create a new cache with a fixed size capacity. The cache is sharded 39 * to 2^numShardBits shards, by hash of the key. The total capacity 40 * is divided and evenly assigned to each shard. If strictCapacityLimit 41 * is set, insert to the cache will fail when cache is full. 42 * numShardBits = -1 means it is automatically determined: every shard 43 * will be at least 512KB and number of shard bits will not exceed 6. 44 * 45 * @param capacity The fixed size capacity of the cache 46 * @param numShardBits The cache is sharded to 2^numShardBits shards, 47 * by hash of the key 48 * @param strictCapacityLimit insert to the cache will fail when cache is full 49 */ 50 public LRUCache(final long capacity, final int numShardBits, 51 final boolean strictCapacityLimit) { 52 super(newLRUCache(capacity, numShardBits, strictCapacityLimit,0.0)); 53 } 54 55 /** 56 * Create a new cache with a fixed size capacity. The cache is sharded 57 * to 2^numShardBits shards, by hash of the key. The total capacity 58 * is divided and evenly assigned to each shard. If strictCapacityLimit 59 * is set, insert to the cache will fail when cache is full. User can also 60 * set percentage of the cache reserves for high priority entries via 61 * highPriPoolRatio. 62 * numShardBits = -1 means it is automatically determined: every shard 63 * will be at least 512KB and number of shard bits will not exceed 6. 64 * 65 * @param capacity The fixed size capacity of the cache 66 * @param numShardBits The cache is sharded to 2^numShardBits shards, 67 * by hash of the key 68 * @param strictCapacityLimit insert to the cache will fail when cache is full 69 * @param highPriPoolRatio percentage of the cache reserves for high priority 70 * entries 71 */ 72 public LRUCache(final long capacity, final int numShardBits, 73 final boolean strictCapacityLimit, final double highPriPoolRatio) { 74 super(newLRUCache(capacity, numShardBits, strictCapacityLimit, 75 highPriPoolRatio)); 76 } 77 78 private native static long newLRUCache(final long capacity, 79 final int numShardBits, final boolean strictCapacityLimit, 80 final double highPriPoolRatio); 81 @Override protected final native void disposeInternal(final long handle); 82 } 83