@Test public void invalidate() { PlatformLruCache cache = new PlatformLruCache(3); cache.set("Hello\nAlice!", A); assertThat(cache.size()).isEqualTo(1); cache.clearKeyUri("Hello"); assertThat(cache.size()).isEqualTo(0); }
@Test public void evictAll() { PlatformLruCache cache = new PlatformLruCache(4); cache.set("a", A); cache.set("b", B); cache.set("c", C); cache.clear(); assertThat(cache.cache.snapshot()).isEmpty(); }
@Test public void throwsWithNullKey() { PlatformLruCache cache = new PlatformLruCache(1); try { cache.get(null); fail("Expected NullPointerException"); } catch (NullPointerException expected) { } }
StatsSnapshot createSnapshot() { return new StatsSnapshot(cache.maxSize(), cache.size(), cacheHits, cacheMisses, totalDownloadSize, totalOriginalBitmapSize, totalTransformedBitmapSize, averageDownloadSize, averageOriginalBitmapSize, averageTransformedBitmapSize, downloadCount, originalBitmapCount, transformedBitmapCount, System.currentTimeMillis()); }
/** * Replacing the value for a key doesn't cause an eviction but it does bring the replaced entry to * the front of the queue. */ @Test public void putCauseEviction() { PlatformLruCache cache = new PlatformLruCache(3); cache.set("a", A); cache.set("b", B); cache.set("c", C); cache.set("b", D); assertSnapshot(cache, "a", A, "c", C, "b", D); }
@Test public void overMaxSizeRemovesExisting() { PlatformLruCache cache = new PlatformLruCache(20); Bitmap size4 = Bitmap.createBitmap(2, 2, ALPHA_8); Bitmap size16 = Bitmap.createBitmap(4, 4, ALPHA_8); Bitmap size25 = Bitmap.createBitmap(5, 5, ALPHA_8); cache.set("small", size4); expectedPutCount++; assertHit(cache, "small", size4); cache.set("big", size16); expectedPutCount++; assertHit(cache, "small", size4); assertHit(cache, "big", size16); cache.set("big", size25); assertHit(cache, "small", size4); assertMiss(cache, "big"); assertThat(cache.size()).isEqualTo(4); }
@Test public void clearPrefixedKey() { PlatformLruCache cache = new PlatformLruCache(3); cache.set("Hello\nAlice!", A); cache.set("Hello\nBob!", B); cache.set("Hello\nEve!", C); cache.set("Hellos\nWorld!", D); cache.clearKeyUri("Hello"); assertThat(cache.cache.snapshot()).hasSize(1); assertThat(cache.cache.snapshot()).containsKey("Hellos\nWorld!"); }
/** * Specify the memory cache size in bytes to use for the most recent images. * A size of 0 disables in-memory caching. */ @NonNull public Builder withCacheSize(int maxByteCount) { if (maxByteCount < 0) { throw new IllegalArgumentException("maxByteCount < 0: " + maxByteCount); } cache = new PlatformLruCache(maxByteCount); return this; }
private void assertStatistics(PlatformLruCache cache) { assertThat(cache.putCount()).isEqualTo(expectedPutCount); assertThat(cache.hitCount()).isEqualTo(expectedHitCount); assertThat(cache.missCount()).isEqualTo(expectedMissCount); assertThat(cache.evictionCount()).isEqualTo(expectedEvictionCount); }
@Nullable Bitmap quickMemoryCacheCheck(String key) { Bitmap cached = cache.get(key); if (cached != null) { stats.dispatchCacheHit(); } else { stats.dispatchCacheMiss(); } return cached; }
void performComplete(BitmapHunter hunter) { if (shouldWriteToMemoryCache(hunter.data.memoryPolicy)) { RequestHandler.Result result = hunter.getResult(); if (result != null) { Bitmap bitmap = result.getBitmap(); if (bitmap != null) { cache.set(hunter.getKey(), bitmap); } } } hunterMap.remove(hunter.getKey()); deliver(hunter); }
@Test public void huntReturnsWhenResultInCache() throws Exception { cache.set(URI_KEY_1 + KEY_SEPARATOR, bitmap); Action action = mockAction(URI_KEY_1, URI_1, mockImageViewTarget()); TestableBitmapHunter hunter = new TestableBitmapHunter(picasso, dispatcher, cache, stats, action, bitmap); RequestHandler.Result result = hunter.hunt(); assertThat(cache.hitCount()).isEqualTo(1); Request request = action.request; verify(hunter.requestHandler, never()) .load(eq(picasso), eq(request), any(RequestHandler.Callback.class)); assertThat(result.getBitmap()).isEqualTo(bitmap); }
/** * Clear all the bitmaps from the memory cache. */ public void evictAll() { cache.clear(); }
/** * Invalidate all memory cached images for the specified {@code uri}. * * @see #invalidate(String) * @see #invalidate(File) */ public void invalidate(@Nullable Uri uri) { if (uri != null) { cache.clearKeyUri(uri.toString()); } }
@Test public void performCompleteWithNoStoreMemoryPolicy() { Request data = new Request.Builder(URI_1).memoryPolicy(MemoryPolicy.NO_STORE).build(); Action action = noopAction(data); BitmapHunter hunter = new BitmapHunter(mockPicasso(), dispatcher, cache, stats, action, EMPTY_REQUEST_HANDLER); hunter.result = new RequestHandler.Result(bitmap1, MEMORY); dispatcher.performComplete(hunter); assertThat(dispatcher.hunterMap).isEmpty(); assertThat(cache.size()).isEqualTo(0); }
void set(@NonNull String key, @NonNull Bitmap bitmap) { if (key == null || bitmap == null) { throw new NullPointerException("key == null || bitmap == null"); } int byteCount = BitmapCompat.getAllocationByteCount(bitmap); // If the bitmap is too big for the cache, don't even attempt to store it. Doing so will cause // the cache to be cleared. Instead just evict an existing element with the same key if it // exists. if (byteCount > maxSize()) { cache.remove(key); return; } cache.put(key, new BitmapAndSize(bitmap, byteCount)); }
@Test public void huntDecodesWhenNotInCache() throws Exception { Action action = mockAction(URI_KEY_1, URI_1, mockImageViewTarget()); TestableBitmapHunter hunter = new TestableBitmapHunter(picasso, dispatcher, cache, stats, action, bitmap); RequestHandler.Result result = hunter.hunt(); assertThat(cache.missCount()).isEqualTo(1); Request request = action.request; verify(hunter.requestHandler) .load(eq(picasso), eq(request), any(RequestHandler.Callback.class)); assertThat(result.getBitmap()).isEqualTo(bitmap); }
@Test public void cannotPutNullKey() { PlatformLruCache cache = new PlatformLruCache(3); try { cache.set(null, A); fail(); } catch (NullPointerException expected) { } }