/** * Creates and returns a shared cache with the given maximum heap memory capacity in bytes. * <p> * This factory method does not check whether the provided maximum capacity exceeds any JVM * limit, it's up to the calling code to make sure the provided capacity can be delivered by the * JVM heap. * * @throws IllegalArgumentException if {@code maxCacheSizeBytes} is lower than zero, with zero * meaning no caching at all. */ public static SharedCache build(final long maxCacheSizeBytes) { return SharedCache.build(Impl.L1_CACHE_SIZE, maxCacheSizeBytes); }
@Override public void setMaximumSize(long maxSizeBytes) throws IllegalArgumentException { final long absoluteMaximumSize = getAbsoluteMaximumSize(); checkArgument(maxSizeBytes >= 0 && maxSizeBytes <= absoluteMaximumSize, "Cache max size must be between 0 and %s, got %s", absoluteMaximumSize, maxSizeBytes); SharedCache cache = SharedCache.build(maxSizeBytes); SharedCache old = _SHARED_CACHE; _SHARED_CACHE = cache; if (old != null) { old.invalidateAll(); } this.currentMaxCacheSize = maxSizeBytes; }
public @Test void testBuildPrecondition() { ex.expect(IllegalArgumentException.class); SharedCache.build(-1L); }
public @Before void before() { cache = spy(SharedCache.build(maxCacheSizeBytes)); repo1Id = new CacheIdentifier(1); repo2Id = new CacheIdentifier(1000); store = new HeapObjectStore(); store.open(); obj = RevObjectTestSupport.createFeaturesTree(store, "f-", 10); }
public @Test void testL1WriteBack() { final int L1Capacity = 1000; SharedCache cache = SharedCache.build(L1Capacity, maxCacheSizeBytes); List<RevObject> objects = createObjects(100); objects.forEach((o) -> cache.put(repo1Id.create(o.getId()), o)); objects.forEach((o) -> assertNull(cache.getIfPresent(repo2Id.create(o.getId())))); objects.forEach((o) -> assertNotNull(cache.getIfPresent(repo1Id.create(o.getId())))); }
public @Test void testInvalidateAll() { final int L1Capacity = 10; SharedCache cache = SharedCache.build(L1Capacity, 16 * 1024 * 1024); List<RevObject> objects = createObjects(500); objects.forEach((o) -> cache.put(repo1Id.create(o.getId()), o)); objects.forEach((o) -> cache.put(repo2Id.create(o.getId()), o)); objects.forEach((o) -> assertNotNull(cache.getIfPresent(repo1Id.create(o.getId())))); objects.forEach((o) -> assertNotNull(cache.getIfPresent(repo2Id.create(o.getId())))); cache.invalidateAll(); objects.forEach((o) -> assertNull(cache.getIfPresent(repo2Id.create(o.getId())))); objects.forEach((o) -> assertNull(cache.getIfPresent(repo1Id.create(o.getId())))); }
public @Test void testInvalidateAllForPrefix() { final int L1Capacity = 10; SharedCache cache = SharedCache.build(L1Capacity, 16 * 1024 * 1024); List<RevObject> objects = createObjects(100); objects.forEach((o) -> cache.put(repo1Id.create(o.getId()), o)); objects.forEach((o) -> cache.put(repo2Id.create(o.getId()), o)); objects.forEach((o) -> assertNotNull(cache.getIfPresent(repo1Id.create(o.getId())))); objects.forEach((o) -> assertNotNull(cache.getIfPresent(repo2Id.create(o.getId())))); cache.invalidateAll(repo2Id); objects.forEach((o) -> assertNull(cache.getIfPresent(repo2Id.create(o.getId())))); objects.forEach((o) -> assertNotNull(cache.getIfPresent(repo1Id.create(o.getId())))); cache.invalidateAll(repo1Id); objects.forEach((o) -> assertNull(cache.getIfPresent(repo1Id.create(o.getId())))); }
public @Test void testCacheDisabled() { SharedCache cache = SharedCache.build(0L); Key k1 = repo1Id.create(obj.getId()); cache.put(k1, obj); assertFalse(cache.contains(k1)); assertEquals(0L, cache.sizeBytes()); }