private void incrementOrPut( final LRUCache<String, AtomicLong> cache, final String requestURI ) { final AtomicLong count = cache.get( requestURI ); if ( count != null ) { count.incrementAndGet(); } else { cache.put( requestURI, new AtomicLong( 1 ) ); } }
@Test public void testLRU() { final LRUCache<String,String> cut = new LRUCache<String, String>( 3 ); final String f = "foo"; final String br = "bar"; cut.put(f, br); cut.put(br, "baz"); Assert.assertTrue( Arrays.equals( new String[]{ f, br }, cut.getKeys().toArray() ), "invalid order of items, initially it should be insertion ordered" ); cut.get( f ); Assert.assertTrue( Arrays.equals( new String[]{ br, f }, cut.getKeys().toArray() ), "invalid order of items, accessing foo should move it to the end" ); cut.get( br ); Assert.assertTrue( Arrays.equals( new String[]{ f, br }, cut.getKeys().toArray() ), "invalid order of items" ); cut.get( f ); Assert.assertTrue( Arrays.equals( new String[]{ br, f }, cut.getKeys().toArray() ), "invalid order of items, accessing foo should move it to the end" ); cut.put( "baz", "foo" ); Assert.assertTrue( Arrays.equals( new String[]{ br, f, "baz" }, cut.getKeys().toArray() ), "invalid order of items, last inserted item should be at last position" ); }
protected MemcachedBackupSession loadFromMemcachedWithCheck( final String sessionId ) { if ( !canHitMemcached( sessionId ) || _invalidSessionsCache.get( sessionId ) != null ) { return null; } return loadFromMemcached( sessionId ); }
@Test public void testCacheSize() { final LRUCache<String,String> cut = new LRUCache<String, String>( 1 ); cut.put("foo", "bar"); Assert.assertEquals( "bar", cut.get("foo") ); cut.put("bar", "baz"); Assert.assertEquals( "baz", cut.get("bar") ); Assert.assertNull( cut.get("foo"), "old key still existing, unexpected cache size" ); }
@Test public void testCacheTTL() throws InterruptedException { final LRUCache<String,String> cut = new LRUCache<String, String>(1, 100); cut.put("foo", "bar"); Assert.assertEquals( "bar", cut.get("foo") ); Thread.sleep( 120 ); Assert.assertNull( cut.get("foo"), "expired key still existing, unexpected cache size" ); }
@Test public void testClear() { final LRUCache<String,Integer> cut = new LRUCache<String, Integer>( 3 ); cut.put("foo", 1); cut.clear(); assertNull(cut.get("foo")); }
private void incrementOrPut( final LRUCache<String, AtomicLong> cache, final String requestURI ) { final AtomicLong count = cache.get( requestURI ); if ( count != null ) { count.incrementAndGet(); } else { cache.put( requestURI, new AtomicLong( 1 ) ); } }
protected MemcachedBackupSession loadFromMemcachedWithCheck( final String sessionId ) { if ( !canHitMemcached( sessionId ) || _invalidSessionsCache.get( sessionId ) != null ) { return null; } return loadFromMemcached( sessionId ); }
else if ( canHitMemcached( id ) && _invalidSessionsCache.get( id ) == null ) {
else if ( canHitMemcached( id ) && _invalidSessionsCache.get( id ) == null ) {