public void testGet() { SimpleCache cache = new SimpleCache(10, 1000); cache.put(123, "First test"); cache.put(234, "Second test"); assertEquals("First test", cache.get(123)); assertEquals(2, cache.size()); try { Thread.sleep(1500); assertNull(cache.get(123)); assertEquals(1, cache.size()); } catch (InterruptedException e) { fail("Interrupted..."); } cache = new SimpleCache(5, SimpleCache.CACHE_FOREVER); cache.put(123, "Val"); cache.put(123, "Val"); cache.put(234, "Val"); cache.put(567, "Val"); cache.put(891, "Val"); cache.put(111, "Val"); cache.put(112, "Val"); cache.put(113, "Val"); assertEquals(5, cache.size()); assertNull(cache.get(123)); assertNull(cache.get(234)); }
public ItemLoadedFuture getPdu( Uri uri, boolean requestSlideshow, final ItemLoadedCallback<PduLoaded> callback) { if (uri == null) { throw new NullPointerException(); } PduCacheEntry cacheEntry = null; synchronized (mPduCache) { if (!mPduCache.isUpdating(uri)) { cacheEntry = mPduCache.get(uri); } } final SlideshowModel slideshow = (requestSlideshow && !DEBUG_DISABLE_CACHE) ? mSlideshowCache.get(uri) : null; final boolean slideshowExists = !requestSlideshow || slideshow != null; final boolean pduExists = cacheEntry != null && cacheEntry.getPdu() != null; final boolean taskExists = mPendingTaskUris.contains(uri); final boolean newTaskRequired = (!pduExists || !slideshowExists) && !taskExists; final boolean callbackRequired = callback != null; if (pduExists && slideshowExists) { if (callbackRequired) { PduLoaded pduLoaded = new PduLoaded(cacheEntry.getPdu(), slideshow); callback.onItemLoaded(pduLoaded, null); } return new NullItemLoadedFuture(); } if (callbackRequired) { addCallback(uri, callback); } if (newTaskRequired) { mPendingTaskUris.add(uri); Runnable task = new PduTask(uri, requestSlideshow); mExecutor.execute(task); } return new ItemLoadedFuture() { private boolean mIsDone; public void cancel(Uri uri) { cancelCallback(callback); removePdu(uri); // the pdu and/or slideshow might be half loaded. Make sure // we load fresh the next time this uri is requested. } public void setIsDone(boolean done) { mIsDone = done; } public boolean isDone() { return mIsDone; } }; }