You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directmemory.apache.org by no...@apache.org on 2012/12/26 14:33:29 UTC

svn commit: r1425903 - in /directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache: CacheService.java CacheServiceImpl.java

Author: noctarius
Date: Wed Dec 26 13:33:29 2012
New Revision: 1425903

URL: http://svn.apache.org/viewvc?rev=1425903&view=rev
Log:
DIRECTMEMORY-121, DIRECTMEMORY-122 Added void scheduleDisposalEvery( long period, TimeUnit unit ) to CacheService.java to give users a nice interface to schedule the disposal interval and started documenting the API interfaces

Modified:
    directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheService.java
    directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheServiceImpl.java

Modified: directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheService.java
URL: http://svn.apache.org/viewvc/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheService.java?rev=1425903&r1=1425902&r2=1425903&view=diff
==============================================================================
--- directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheService.java (original)
+++ directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheService.java Wed Dec 26 13:33:29 2012
@@ -19,67 +19,188 @@ package org.apache.directmemory.cache;
  * under the License.
  */
 
+import java.io.Closeable;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.TimeUnit;
+
 import org.apache.directmemory.memory.MemoryManagerService;
 import org.apache.directmemory.memory.Pointer;
 import org.apache.directmemory.serialization.Serializer;
-
-import java.io.Closeable;
-import java.util.concurrent.ConcurrentMap;
+import org.slf4j.Logger;
 
 public interface CacheService<K, V>
     extends Closeable
 {
 
-    void scheduleDisposalEvery( long l );
+    /**
+     * Schedules the disposal event with the given period in milliseconds.
+     * 
+     * @param period The time period in milliseconds
+     */
+    void scheduleDisposalEvery( long period );
 
     /**
-     * @param key
-     * @param payload
-     * @param expiresIn in ms
-     * @return
+     * Schedules the disposal event with the given period with the given {@link TimeUnit}.
+     * 
+     * @param period The time period
+     * @param unit The period's timeunit
      */
-    Pointer<V> putByteArray( K key, byte[] payload, int expiresIn );
+    void scheduleDisposalEvery( long period, TimeUnit unit );
 
-    Pointer<V> putByteArray( K key, byte[] payload );
+    /**
+     * Stored a preserialized payload with the given key. The expiration is set to the given expiresIn value in
+     * milliseconds. If not enough space found to store the payload the returned pointer is null.
+     * 
+     * @param key The key to save the payload with
+     * @param payload The preserialized payload as bytearray
+     * @param expiresIn The expiration delay
+     * @return The created pointer to directly retrieve the payload or null if not enough space was found
+     */
+    Pointer<V> putByteArray( K key, byte[] payload, long expiresIn );
 
-    Pointer<V> put( K key, V value );
+    /**
+     * Stored a preserialized payload with the given key with no expiration value. If not enough space found to store
+     * the payload the returned pointer is null.
+     * 
+     * @param key The key to save the payload with
+     * @param payload The preserialized payload as bytearray
+     * @return The created pointer to directly retrieve the payload or null if not enough space was found
+     */
+    Pointer<V> putByteArray( K key, byte[] payload );
 
     /**
-     * @param key
-     * @param value
-     * @param expiresIn in ms
-     * @return
+     * Serializes and stored the given value using the key and sets the expiresIn value for the expiration of the key.
+     * If not enough space found to store the payload the returned pointer is null.
+     * 
+     * @param key The key to save the value with
+     * @param value The value to serialize and store
+     * @param expiresIn The expiration delay
+     * @return The created pointer to directly retrieve the payload or null if not enough space was found
      */
     Pointer<V> put( K key, V value, int expiresIn );
 
+    /**
+     * Serializes and stored the given value using the key with no expiration value. If not enough space found to store
+     * the payload the returned pointer is null.
+     * 
+     * @param key The key to save the value with
+     * @param value The value to serialize and store
+     * @return The created pointer to directly retrieve the payload or null if not enough space was found
+     */
+    Pointer<V> put( K key, V value );
+
+    /**
+     * Retrieves the stored payload for key as a bytearray. If no pointer is found for the given key null is returned.
+     * 
+     * @param key The key to retrieve
+     * @return The payload as bytearray or null if key was not found
+     */
     byte[] retrieveByteArray( K key );
 
+    /**
+     * Retrieves the stored, deserialized value for key. If no pointer is found for the given key null is returned.
+     * 
+     * @param key The key to retrieve
+     * @return The deserialized value or null if key was not found
+     */
     V retrieve( K key );
 
+    /**
+     * Retrieves the accociated {@link Pointer} to the given key or null if no pointer was found.
+     * 
+     * @param key The key to retrieve
+     * @return The pointer of the key or null if key was not found
+     */
     Pointer<V> getPointer( K key );
 
+    /**
+     * Removes the key and frees the underlying memory area.
+     * 
+     * @param key The key to remove
+     */
     void free( K key );
 
+    /**
+     * Removes the pointer and frees the underlying memory area.
+     * 
+     * @param key The pointer to remove
+     */
     void free( Pointer<V> pointer );
 
+    /**
+     * Tells the {@link CacheService} to collect and remove all expired keys. In most cases this is automatically
+     * handled by scheduling a disposal interval using {@link CacheService#scheduleDisposalEvery} and there are very
+     * rare cases where this needs to be called manually.
+     */
     void collectExpired();
 
+    /**
+     * Tells the {@link CacheService} to collect and remove all least frequently used keys. This operation could
+     * possibly clear the whole cache if there were no recent actions. In most cases this is automatically handled by
+     * scheduling a disposal interval using {@link CacheService#scheduleDisposalEvery} and there are very rare cases
+     * where this needs to be called manually.
+     */
     void collectLFU();
 
+    /**
+     * Tells the {@link CacheService} to collect and remove all expired AND least frequently used keys. In most cases
+     * this is automatically handled by scheduling a disposal interval using {@link CacheService#scheduleDisposalEvery}
+     * and there are very rare cases where this needs to be called manually.
+     */
     void collectAll();
 
+    /**
+     * Clears the whole cache by removing all stored keys. It is up to the underlying {@link MemoryManagerService}
+     * implementation to free allocated memory or not.
+     */
     void clear();
 
+    /**
+     * Retrieves the count of the current entries.
+     * 
+     * @return Number of entries
+     */
     long entries();
 
+    /**
+     * Dumps information about the actual internal {@link MemoryManagerService} to the configured {@link Logger} with
+     * info loglevel.
+     */
     void dump();
 
-    ConcurrentMap<K, Pointer<V>> getMap();
+    /**
+     * Retrieves a map of all available keys and their according {@link Pointer}s. It is up to the {@link CacheService}
+     * implementation if the retrieved map is threadsafe or not. The standard implementation uses a
+     * {@link ConcurrentHashMap}.
+     * 
+     * @return A mapping of keys to their according pointers
+     */
+    Map<K, Pointer<V>> getMap();
 
+    /**
+     * Retrieves the internally used {@link Serializer} implementation.
+     * 
+     * @return The used serializer
+     */
     Serializer getSerializer();
 
+    /**
+     * Retrieves the internally used {@link MemoryManagerService} implementation.
+     * 
+     * @return The used memory manager
+     */
     MemoryManagerService<V> getMemoryManager();
 
+    /**
+     * Explicitly allocated a bunch of bytes in the cache using a given key and type and returns the created
+     * {@link Pointer}.
+     * 
+     * @param key The key to store as
+     * @param type The datatype of the underlying data
+     * @param size The size to allocate for this pointer
+     * @return
+     */
     <T extends V> Pointer<V> allocate( K key, Class<T> type, int size );
 
 }

Modified: directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheServiceImpl.java
URL: http://svn.apache.org/viewvc/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheServiceImpl.java?rev=1425903&r1=1425902&r2=1425903&view=diff
==============================================================================
--- directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheServiceImpl.java (original)
+++ directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheServiceImpl.java Wed Dec 26 13:33:29 2012
@@ -31,6 +31,7 @@ import java.io.IOException;
 import java.util.Timer;
 import java.util.TimerTask;
 import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.TimeUnit;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static java.lang.String.format;
@@ -65,7 +66,13 @@ public class CacheServiceImpl<K, V>
     }
 
     @Override
-    public void scheduleDisposalEvery( long l )
+    public void scheduleDisposalEvery( long period, TimeUnit unit )
+    {
+        scheduleDisposalEvery( unit.toMillis( period ) );
+    }
+    
+    @Override
+    public void scheduleDisposalEvery( long period )
     {
         timer.schedule( new TimerTask()
         {
@@ -78,9 +85,9 @@ public class CacheServiceImpl<K, V>
 
                 logger.info( "scheduled disposal complete" );
             }
-        }, l, l );
+        }, period, period );
 
-        logger.info( "disposal scheduled every {} milliseconds", l );
+        logger.info( "disposal scheduled every {} milliseconds", period );
     }
 
     @Override
@@ -90,7 +97,7 @@ public class CacheServiceImpl<K, V>
     }
 
     @Override
-    public Pointer<V> putByteArray( K key, byte[] payload, int expiresIn )
+    public Pointer<V> putByteArray( K key, byte[] payload, long expiresIn )
     {
         return store( key, payload, expiresIn );
     }
@@ -132,7 +139,7 @@ public class CacheServiceImpl<K, V>
         }
     }
 
-    private Pointer<V> store( K key, byte[] payload, int expiresIn )
+    private Pointer<V> store( K key, byte[] payload, long expiresIn )
     {
         Pointer<V> pointer = map.get( key );
         if ( pointer != null )