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 ) ); } }
final ManagedItem<V> item = _map.get( key ); if ( item == null || item._value == null || !item._value.equals( value ) ) { return put( key, value ); } else { return item._value;
@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 testGetKeysSortedByValue() { final LRUCache<String,Integer> cut = new LRUCache<String, Integer>( 3 ); final String f = "foo"; final String br = "bar"; cut.put(f, 1); cut.put(br, 2); final Comparator<Integer> c = new Comparator<Integer>() { @Override public int compare( final Integer o1, final Integer o2 ) { return o1.compareTo( o2 ); } }; Assert.assertTrue( Arrays.equals( new String[]{ f, br }, cut.getKeysSortedByValue( c ).toArray() ), "invalid order of items, the keys are not order by their values" ); cut.put(f, 3); Assert.assertTrue( Arrays.equals( new String[]{ br, f }, cut.getKeysSortedByValue( c ).toArray() ), "invalid order of items, the keys are not order by their values" ); }
@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")); }
@Test public void testRemove() { final LRUCache<String,String> cut = new LRUCache<String, String>( 3 ); cut.put("foo", "bar"); assertTrue( cut.containsKey( "foo" ) ); assertEquals( cut.remove( "foo" ), "bar" ); assertFalse( cut.containsKey( "foo" ) ); }
@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" ); }
/** * Is invoked when a session was removed from the manager, e.g. because the * session has been invalidated. * * Is used to release a lock if the non-stick session was locked * * It's also used to keep track of such sessions in non-sticky mode, so that * lockingStrategy.onBackupWithoutLoadedSession is not invoked (see issue 116). * * @param session the removed session. */ public void sessionRemoved(final MemcachedBackupSession session) { if(!_sticky) { if(session.isLocked()) { _lockingStrategy.releaseLock(session.getIdInternal()); session.releaseLock(); } _invalidSessionsCache.put(session.getIdInternal(), Boolean.TRUE); } }
_invalidSessionsCache.put( sessionId, Boolean.TRUE ); if ( _log.isDebugEnabled() ) { _log.debug( "Session " + sessionId + " not found in memcached." ); releaseIfLocked( sessionId, lockStatus ); _storage.delete( _memcachedNodesManager.getStorageKeyFormat().format(sessionId) ); _invalidSessionsCache.put( sessionId, Boolean.TRUE ); } catch ( final Exception e ) { _log.warn( "Could not load session with id " + sessionId + " from memcached.", e );
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 ) ); } }
final ManagedItem<V> item = _map.get( key ); if ( item == null || item._value == null || !item._value.equals( value ) ) { return put( key, value ); } else { return item._value;
/** * Is invoked when a session was removed from the manager, e.g. because the * session has been invalidated. * * Is used to release a lock if the non-stick session was locked * * It's also used to keep track of such sessions in non-sticky mode, so that * lockingStrategy.onBackupWithoutLoadedSession is not invoked (see issue 116). * * @param session the removed session. */ public void sessionRemoved(final MemcachedBackupSession session) { if(!_sticky) { if(session.isLocked()) { _lockingStrategy.releaseLock(session.getIdInternal()); session.releaseLock(); } _invalidSessionsCache.put(session.getIdInternal(), Boolean.TRUE); } }
_invalidSessionsCache.put( sessionId, Boolean.TRUE ); if ( _log.isDebugEnabled() ) { _log.debug( "Session " + sessionId + " not found in memcached." ); releaseIfLocked( sessionId, lockStatus ); _storage.delete( _memcachedNodesManager.getStorageKeyFormat().format(sessionId) ); _invalidSessionsCache.put( sessionId, Boolean.TRUE ); } catch ( final Exception e ) { _log.warn( "Could not load session with id " + sessionId + " from memcached.", e );