2015/07/26 - Apache DirectMemory has been retired.

For more information, please explore the Attic.

public class

CacheServiceImpl

extends Object
implements MutableCacheService<K, V>
java.lang.Object
   ↳ org.apache.directmemory.cache.CacheServiceImpl<K, V>

Summary

Public Constructors
CacheServiceImpl(ConcurrentMap<K, Pointer<V>> map, MemoryManagerService<V> memoryManager, Serializer serializer)
Constructor
Public Methods
<T extends V> Pointer<V> allocate(K key, Class<T> type, int size)
Explicitly allocated a bunch of bytes in the cache using a given key and type and returns the created Pointer.
void clear()
Clears the whole cache by removing all stored keys.
void close()
void collectAll()
Tells the CacheService to collect and remove all expired AND least frequently used keys.
void collectExpired()
Tells the CacheService to collect and remove all expired keys.
void collectLFU()
Tells the CacheService to collect and remove all least frequently used keys.
void dump()
Dumps information about the actual internal MemoryManagerService to the configured Logger with info loglevel.
void dump(MemoryManagerService<V> mms)
long entries()
Retrieves the count of the current entries.
void free(K key)
Removes the key and frees the underlying memory area.
void free(Pointer<V> pointer)
Removes the pointer and frees the underlying memory area.
ConcurrentMap<K, Pointer<V>> getMap()
Retrieves a map of all available keys and their according Pointers.
MemoryManagerService<V> getMemoryManager()
Retrieves the internally used MemoryManagerService implementation.
Pointer<V> getPointer(K key)
Retrieves the accociated Pointer to the given key or null if no pointer was found.
Serializer getSerializer()
Retrieves the internally used Serializer implementation.
Pointer<V> put(K key, V value, int expiresIn)
Serializes and stored the given value using the key and sets the expiresIn value for the expiration of the key.
Pointer<V> put(K key, V value)
Serializes and stored the given value using the key with no expiration value.
Pointer<V> putByteArray(K key, byte[] payload, long expiresIn)
Stored a preserialized payload with the given key.
Pointer<V> putByteArray(K key, byte[] payload)
Stored a preserialized payload with the given key with no expiration value.
V retrieve(K key)
Retrieves the stored, deserialized value for key.
byte[] retrieveByteArray(K key)
Retrieves the stored payload for key as a bytearray.
void scheduleDisposalEvery(long period, TimeUnit unit)
Schedules the disposal event with the given period with the given TimeUnit.
void scheduleDisposalEvery(long period)
Schedules the disposal event with the given period in milliseconds.
void setMap(ConcurrentMap<K, Pointer<V>> map)
void setMemoryManager(MemoryManagerService<V> memoryManager)
void setSerializer(Serializer serializer)
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.io.Closeable
From interface org.apache.directmemory.cache.CacheService
From interface org.apache.directmemory.cache.MutableCacheService

Public Constructors

public CacheServiceImpl (ConcurrentMap<K, Pointer<V>> map, MemoryManagerService<V> memoryManager, Serializer serializer)

Constructor

Public Methods

public Pointer<V> allocate (K key, Class<T> type, int size)

Explicitly allocated a bunch of bytes in the cache using a given key and type and returns the created Pointer.

Parameters
key The key to store as
type The datatype of the underlying data
size The size to allocate for this pointer

public void clear ()

Clears the whole cache by removing all stored keys. It is up to the underlying MemoryManagerService implementation to free allocated memory or not.

public void close ()

Throws
IOException

public void collectAll ()

Tells the 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 scheduleDisposalEvery(long) and there are very rare cases where this needs to be called manually.

public void collectExpired ()

Tells the CacheService to collect and remove all expired keys. In most cases this is automatically handled by scheduling a disposal interval using scheduleDisposalEvery(long) and there are very rare cases where this needs to be called manually.

public void collectLFU ()

Tells the 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 scheduleDisposalEvery(long) and there are very rare cases where this needs to be called manually.

public void dump ()

Dumps information about the actual internal MemoryManagerService to the configured Logger with info loglevel.

public void dump (MemoryManagerService<V> mms)

public long entries ()

Retrieves the count of the current entries.

Returns
  • Number of entries

public void free (K key)

Removes the key and frees the underlying memory area.

Parameters
key The key to remove

public void free (Pointer<V> pointer)

Removes the pointer and frees the underlying memory area.

Parameters
pointer The pointer to remove

public ConcurrentMap<K, Pointer<V>> getMap ()

Retrieves a map of all available keys and their according Pointers. It is up to the CacheService implementation if the retrieved map is threadsafe or not. The standard implementation uses a ConcurrentHashMap.

Returns
  • A mapping of keys to their according pointers

public MemoryManagerService<V> getMemoryManager ()

Retrieves the internally used MemoryManagerService implementation.

Returns
  • The used memory manager

public Pointer<V> getPointer (K key)

Retrieves the accociated Pointer to the given key or null if no pointer was found.

Parameters
key The key to retrieve
Returns
  • The pointer of the key or null if key was not found

public Serializer getSerializer ()

Retrieves the internally used Serializer implementation.

Returns
  • The used serializer

public Pointer<V> put (K key, V value, int expiresIn)

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.

Parameters
key The key to save the value with
value The value to serialize and store
expiresIn The expiration delay
Returns
  • The created pointer to directly retrieve the payload or null if not enough space was found

public Pointer<V> put (K key, V value)

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.

Parameters
key The key to save the value with
value The value to serialize and store
Returns
  • The created pointer to directly retrieve the payload or null if not enough space was found

public Pointer<V> putByteArray (K key, byte[] payload, long expiresIn)

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.

Parameters
key The key to save the payload with
payload The preserialized payload as bytearray
expiresIn The expiration delay
Returns
  • The created pointer to directly retrieve the payload or null if not enough space was found

public Pointer<V> putByteArray (K key, byte[] payload)

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.

Parameters
key The key to save the payload with
payload The preserialized payload as bytearray
Returns
  • The created pointer to directly retrieve the payload or null if not enough space was found

public V retrieve (K key)

Retrieves the stored, deserialized value for key. If no pointer is found for the given key null is returned.

Parameters
key The key to retrieve
Returns
  • The deserialized value or null if key was not found

public byte[] retrieveByteArray (K key)

Retrieves the stored payload for key as a bytearray. If no pointer is found for the given key null is returned.

Parameters
key The key to retrieve
Returns
  • The payload as bytearray or null if key was not found

public void scheduleDisposalEvery (long period, TimeUnit unit)

Schedules the disposal event with the given period with the given TimeUnit.

Parameters
period The time period
unit The period's timeunit

public void scheduleDisposalEvery (long period)

Schedules the disposal event with the given period in milliseconds.

Parameters
period The time period in milliseconds

public void setMap (ConcurrentMap<K, Pointer<V>> map)

public void setMemoryManager (MemoryManagerService<V> memoryManager)

public void setSerializer (Serializer serializer)