@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" ); }
@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" ); }
if( _invalidSessionsCache.containsKey(session.getId()) ){ if ( _log.isDebugEnabled() ) { _log.debug( "Remove session id " + session.getId() + " from _invalidSessionsCache, marking new session valid" ); _invalidSessionsCache.remove(session.getId());
/** * Determines, if the given requestURI is a readOnly request and not blacklisted as a modifying request. * @param requestId the request uri to check * @return <code>true</code> if the given request uri can be regarded as read only. */ public boolean isReadOnlyRequest( final String requestId ) { if ( _log.isDebugEnabled() ) { _log.debug( "Asked for readonly request: " + requestId + " ("+ _readOnlyRequests.containsKey( requestId ) +")" ); } // TODO: add some threshold return _readOnlyRequests.containsKey( requestId ); }
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;
protected MemcachedBackupSession loadFromMemcachedWithCheck( final String sessionId ) { if ( !canHitMemcached( sessionId ) || _invalidSessionsCache.get( sessionId ) != null ) { return null; } return loadFromMemcached( sessionId ); }
/** * Registers the given requestURI as a modifying request, which can be seen as a blacklist for * readonly requests. There's a limit on number and time for modifying requests beeing stored. * @param requestId the request uri to track. */ public void modifyingRequest( final String requestId ) { if ( _log.isDebugEnabled() ) { _log.debug( "Registering modifying request: " + requestId ); } incrementOrPut( _blacklist, requestId ); _readOnlyRequests.remove( requestId ); }
/** * The readonly requests, ordered by last accessed time, from least-recently accessed to most-recently. * @return a list of readonly requests. */ public List<String> getReadOnlyRequests() { return _readOnlyRequests.getKeys(); }
/** * The readonly requests, ordered by the frequency they got registered, from least-frequently to most-frequently. * @return a list of readonly requests. */ public List<String> getReadOnlyRequestsByFrequency() { return _readOnlyRequests.getKeysSortedByValue( ATOMLONG_COMP ); }
public void shutdown() { _log.info( "Stopping services." ); _manager.getContext().getParent().getPipeline().removeValve(_trackingHostValve); _manager.getContext().getPipeline().removeValve(_trackingContextValve); _backupSessionService.shutdown(); if ( _lockingStrategy != null ) { _lockingStrategy.shutdown(); } if ( _storage != null ) { _storage.shutdown(); _storage = null; } _transcoderFactory = null; _invalidSessionsCache.clear(); }
@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" ); }
if( _invalidSessionsCache.containsKey(session.getId()) ){ if ( _log.isDebugEnabled() ) { _log.debug( "Remove session id " + session.getId() + " from _invalidSessionsCache, marking new session valid" ); _invalidSessionsCache.remove(session.getId());
/** * Registers the given requestURI as a readonly request, as long as it has not been tracked * before as a modifying request (via {@link #modifyingRequest(String)}). * <p> * There's a limit on the number and the time readonly requests are beeing stored (simply a LRU cache), * so that the most frequently accessed readonly requests are stored. * </p> * @param requestId the request uri to track. * @return <code>true</code> if the requestURI was stored as readonly, <code>false</code> if it was on the blacklist. * @see #modifyingRequest(String) */ public boolean readOnlyRequest( final String requestId ) { if ( !_blacklist.containsKey( requestId ) ) { if ( _log.isDebugEnabled() ) { _log.debug( "Registering readonly request: " + requestId ); } incrementOrPut( _readOnlyRequests, requestId ); return true; } return false; }
/** * 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); } }