BigCache implements ICache<K> { public BigCache(String dir, CacheConfig config) throws IOException { this.cacheDir = dir; if (!this.cacheDir.endsWith(File.separator)) { this.cacheDir += File.separator; } if (!FileUtil.isFilenameValid(this.cacheDir)) { throw new IllegalArgumentException("Invalid cache data directory : " + this.cacheDir); } FileUtil.deleteDirectory(new File(this.cacheDir)); this.storageManager = new StorageManager(this.cacheDir, config.getCapacityPerBlock(), config.getInitialNumberOfBlocks(), config.getStorageMode(), config.getMaxOffHeapMemorySize()); this.readWriteLock = new StripedReadWriteLock(config.getConcurrencyLevel()); ses = new ScheduledThreadPoolExecutor(2); ses.scheduleWithFixedDelay(new CacheCleaner(this), config.getPurgeInterval(), config.getPurgeInterval(), TimeUnit.MILLISECONDS); ses.scheduleWithFixedDelay(new CacheMerger(this), config.getMergeInterval(), config.getMergeInterval(), TimeUnit.MILLISECONDS); dirtyRatioThreshold = config.getDirtyRatioThreshold(); } BigCache(String dir, CacheConfig config); @Override void put(K key, byte[] value); @Override void put(K key, byte[] value, long tti); @Override byte[] get(K key); @Override byte[] delete(K key); @Override boolean contains(K key); @Override void clear(); @Override double hitRatio(); @Override void close(); long count(); BigCacheStats getStats(); static final long DEFAULT_PURGE_INTERVAL; static final long DEFAULT_MERGE_INTERVAL; static final double DEFAULT_DIRTY_RATIO_THRESHOLD; static final int DEFAULT_CONCURRENCY_LEVEL; static final int MAX_VALUE_LENGTH; }
@Test public void testBigCache() throws IOException { CacheConfig config = new CacheConfig(); config.setStorageMode(storageMode); cache = new BigCache<String>(TEST_DIR, config); Set<String> rndStringSet = new HashSet<String>(); for (int i = 0; i < 2000000 * STRESS_FACTOR; i++) { String rndString = TestUtil.randomString(64); rndStringSet.add(rndString); cache.put(rndString, rndString.getBytes()); if ((i % 50000) == 0 && i != 0) { System.out.println(i + " rows written"); } } for (String rndString : rndStringSet) { byte[] value = cache.get(rndString); assertNotNull(value); assertEquals(rndString, new String(value)); } for (String rndString : rndStringSet) { cache.delete(rndString); } for (String rndString : rndStringSet) { byte[] value = cache.get(rndString); assertNull(value); } }