Class MemcachedStorageService
- All Implemented Interfaces:
Component,DestructableComponent,IdentifiableComponent,IdentifiedComponent,InitializableComponent,StorageService
https://code.google.com/p/memcached/wiki/NewProgrammingTricks#Namespacing
This storage service supports arbitrary-length context names and keys despite the 250-byte limit on memcached keys. Keys whose length is greater than 250 bytes are hashed using the SHA-512 algorithm and hex encoded to produce a 128-character key that is stored in memcached. Collisions are avoided irrespective of hashing by using the memcached add operation on all create operations which guarantees that an entry is created if and only if a key of the same value does not already exist. Note that context names and keys are assumed to have single-byte encodings in UTF-8 (i.e. ASCII characters) such that key lengths are equal to their size in bytes. Hashed keys naturally meet this requirement.
An optional context key-tracking feature is available to support updateContextExpiration(String, Long).
Key tracking is disabled by default, but can be enabled by setting the enableContextKeyTracking
parameter in the MemcachedStorageService(net.spy.memcached.MemcachedClient, int, boolean) constructor.
While there is modest performance impact for create and delete operations, the feature limits the number of keys
per context. With the default 1M memcached slab size, in the worst case 4180 keys are permitted per context.
In many if not most situations the value is easily double that. The limitation can be overcome by increasing the
slab size, which decreases overall cache memory consumption efficiency. When key tracking is disabled, there is no
limit on the number of keys per context other than overall cache capacity.
Limitations and requirements
- The memcached binary protocol is strong recommended for efficiency and full versioning support.
In particular,
deleteWithVersion(long, String, String)anddeleteWithVersion(long, Object)will throw runtime errors under the ASCII protocol. - Memcached server 1.4.14 or later MUST be used with binary protocol for proper handling of cache entry expiration values. See the 1.4.14 release notes for details.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final StringKey suffix for entry that contains a list of deleted context keys.private static final StringDelimiter of items in the context key list.protected static final StringKey suffix for entry that contains a list of context keys.private final org.slf4j.LoggerLogger instance.private static final intMaximum length in bytes of memcached keys.private final net.spy.memcached.MemcachedClientMemcached client instance.private intMemcached asynchronous operation timeout in seconds.private MemcachedStorageCapabilitiesInvariant storage capabilities.private final net.spy.memcached.transcoders.Transcoder<MemcachedStorageRecord<?>>Handles conversion ofMemcachedStorageRecordto bytes and vice versa.private final net.spy.memcached.transcoders.Transcoder<String>Handles conversion of strings to bytes and vice versa.private booleanFlag that controls context key tracking. -
Constructor Summary
ConstructorsConstructorDescriptionMemcachedStorageService(net.spy.memcached.MemcachedClient client, int timeout) Creates a new instance.MemcachedStorageService(net.spy.memcached.MemcachedClient client, int timeout, boolean enableContextKeyTracking) Creates a new instance with optional context key tracking. -
Method Summary
Modifier and TypeMethodDescriptionbooleanCreates a new record in the store using an annotated object as the source.booleanCreates a new record in the store with an expiration.<T> booleancreate(String context, String key, T value, StorageSerializer<T> serializer, Long expiration) Creates a new record in the store with an expiration, using a custom serialization process for an arbitrary object.protected StringcreateNamespace(String context) Creates a cache-wide unique namespace for the given context name.booleanDeletes an existing record from the store, using an annotated object as the source.booleanDeletes an existing record from the store.voiddeleteContext(String context) Forcibly removes all records in a given context along with any associated resources devoted to maintaining the context.booleandeleteWithVersion(long version, Object value) Deletes an existing record from the store, using an annotated object as the source, if it currently has a specified version.booleandeleteWithVersion(long version, String context, String key) Deletes an existing record from the store if it currently has a specified version.protected voidReturns the capabilities of the underlying store.private <T> ThandleAsyncResult(net.spy.memcached.internal.OperationFuture<T> result) Handle async result.protected StringlookupNamespace(String context) Looks up the namespace for the given context name in the cache.private StringmemcachedKey(String... parts) Creates a memcached key from one or more parts.Returns an existing record from the store, if one exists, and uses it to update the annotated fields of a target object.<T> StorageRecord<T>Returns an existing record from the store, if one exists.<T> Pair<Long,StorageRecord<T>> Returns an existing record from the store, along with its version.voidManually trigger a cleanup of expired records.voidsetCapabilities(MemcachedStorageCapabilities capabilities) Sets the storage capabilities.booleanUpdates an existing record in the store, using an annotated object as the source.booleanUpdates an existing record in the store.<T> booleanupdate(String context, String key, T value, StorageSerializer<T> serializer, Long expiration) Updates an existing record in the store using a custom serialization strategy.voidupdateContextExpiration(String context, Long expiration) Updates the expiration time of all records in the context.private booleanupdateContextKeyList(String suffix, String namespace, String key) Update context key list.booleanupdateExpiration(Object value) Updates expiration of an existing record in the store, using an annotated object as the source.booleanupdateExpiration(String context, String key, Long expiration) Updates expiration of an existing record in the store.updateWithVersion(long version, Object value) Updates an existing record in the store, if a version matches, using an annotated object as the source.updateWithVersion(long version, String context, String key, String value, Long expiration) Updates an existing record in the store, if a version matches.<T> LongupdateWithVersion(long version, String context, String key, T value, StorageSerializer<T> serializer, Long expiration) Updates an existing record in the store, if a version matches, using a custom serialization strategy.Methods inherited from class net.shibboleth.shared.component.AbstractIdentifiableInitializableComponent
setIdMethods inherited from class net.shibboleth.shared.component.AbstractIdentifiedInitializableComponent
doInitialize, ensureId, getId, ifDestroyedThrowDestroyedComponentException, ifInitializedThrowUnmodifiabledComponentException, ifNotInitializedThrowUninitializedComponentExceptionMethods inherited from class net.shibboleth.shared.component.AbstractInitializableComponent
checkComponentActive, checkSetterPreconditions, destroy, initialize, isDestroyed, isInitializedMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface net.shibboleth.shared.component.IdentifiedComponent
getId
-
Field Details
-
CTX_KEY_LIST_SUFFIX
Key suffix for entry that contains a list of context keys.- See Also:
-
CTX_KEY_DELETED_SUFFIX
Key suffix for entry that contains a list of deleted context keys.- See Also:
-
CTX_KEY_LIST_DELIMITER
Delimiter of items in the context key list.- See Also:
-
MAX_KEY_LENGTH
private static final int MAX_KEY_LENGTHMaximum length in bytes of memcached keys.- See Also:
-
logger
@Nonnull private final org.slf4j.Logger loggerLogger instance. -
storageRecordTranscoder
@Nonnull private final net.spy.memcached.transcoders.Transcoder<MemcachedStorageRecord<?>> storageRecordTranscoderHandles conversion ofMemcachedStorageRecordto bytes and vice versa. -
stringTranscoder
Handles conversion of strings to bytes and vice versa. -
storageCapabilities
Invariant storage capabilities. -
memcacheClient
@Nonnull private final net.spy.memcached.MemcachedClient memcacheClientMemcached client instance. -
operationTimeout
Memcached asynchronous operation timeout in seconds. -
trackContextKeys
private boolean trackContextKeysFlag that controls context key tracking.
-
-
Constructor Details
-
MemcachedStorageService
public MemcachedStorageService(@Nonnull net.spy.memcached.MemcachedClient client, @Positive int timeout) Creates a new instance.- Parameters:
client- Memcached client object. The client MUST be configured to use the binary memcached protocol, i.e.BinaryConnectionFactory, in order fordeleteWithVersion(long, String, String)anddeleteWithVersion(long, Object)to work correctly. The binary protocol is recommended for efficiency as well.timeout- Memcached operation timeout in seconds.
-
MemcachedStorageService
public MemcachedStorageService(@Nonnull net.spy.memcached.MemcachedClient client, @Positive int timeout, boolean enableContextKeyTracking) Creates a new instance with optional context key tracking.- Parameters:
client- Memcached client object. The client MUST be configured to use the binary memcached protocol, i.e.BinaryConnectionFactory, in order fordeleteWithVersion(long, String, String)anddeleteWithVersion(long, Object)to work correctly. The binary protocol is recommended for efficiency as well.timeout- Memcached operation timeout in seconds.enableContextKeyTracking- True to enable context key tracking, false otherwise. NOTE this flag must be set totruein order forupdateContextExpiration(String, Long)to work. If that capability is not needed, the flag should be set tofalsefor better performance. The feature is disabled by default.
-
-
Method Details
-
getCapabilities
Returns the capabilities of the underlying store.- Specified by:
getCapabilitiesin interfaceStorageService- Returns:
- interface to access the service's capabilities
-
setCapabilities
Sets the storage capabilities. This method should be used when the default 1M slab size is changed; theMemcachedStorageCapabilities.getValueSize()should be set equal to the chosen slab size.- Parameters:
capabilities- Memcached storage capabilities.
-
create
public boolean create(@Nonnull @NotEmpty String context, @Nonnull @NotEmpty String key, @Nonnull @NotEmpty String value, @Nullable @Positive Long expiration) throws IOException Creates a new record in the store with an expiration.- Specified by:
createin interfaceStorageService- Parameters:
context- a storage context labelkey- a key unique to contextvalue- value to storeexpiration- expiration for record, or null- Returns:
- true iff record was inserted, false iff a duplicate was found
- Throws:
IOException- if fatal errors occur in the insertion process
-
create
public <T> boolean create(@Nonnull @NotEmpty String context, @Nonnull @NotEmpty String key, @Nonnull T value, @Nonnull StorageSerializer<T> serializer, @Nullable @Positive Long expiration) throws IOException Creates a new record in the store with an expiration, using a custom serialization process for an arbitrary object.- Specified by:
createin interfaceStorageService- Type Parameters:
T- type of record- Parameters:
context- a storage context labelkey- a key unique to contextvalue- object to storeserializer- custom serializer for the objectexpiration- expiration for record, or null- Returns:
- true iff record was inserted, false iff a duplicate was found
- Throws:
IOException- if fatal errors occur in the insertion process
-
create
Creates a new record in the store using an annotated object as the source.The individual parameters for the creation are extracted from the object using the annotations in the org.opensaml.storage.annotation package. If any are missing, or a field inaccessible, a runtime exception of some kind will occur.
- Specified by:
createin interfaceStorageService- Parameters:
value- object to store- Returns:
- true iff record was inserted, false iff a duplicate was found
- Throws:
IOException- if fatal errors occur in the insertion process
-
read
public <T> StorageRecord<T> read(@Nonnull @NotEmpty String context, @Nonnull @NotEmpty String key) throws IOException Returns an existing record from the store, if one exists.- Specified by:
readin interfaceStorageService- Type Parameters:
T- type of record- Parameters:
context- a storage context labelkey- a key unique to context- Returns:
- the record read back, if present, or null
- Throws:
IOException- if errors occur in the read process
-
read
Returns an existing record from the store, if one exists, and uses it to update the annotated fields of a target object.The context and key to look up are obtained from the target object, and the value and expiration are written back, using the annotations in the org.opensaml.storage.annotation package. If any are missing, or a field inaccessible, a runtime exception of some kind will occur.
- Specified by:
readin interfaceStorageService- Parameters:
value- object to look up and populate- Returns:
- the updated object passed into the method, or null if no record was found
- Throws:
IOException- if errors occur in the read process
-
read
@Nonnull public <T> Pair<Long,StorageRecord<T>> read(@Nonnull @NotEmpty String context, @Nonnull @NotEmpty String key, @Positive long version) throws IOException Returns an existing record from the store, along with its version.The first member of the pair returned will contain the version of the record in the store, or will be null if no record exists. The second member will contain the record read back. If null, the record either didn't exist (if the first member was also null) or the record was the same version as that supplied by the caller.
- Specified by:
readin interfaceStorageService- Type Parameters:
T- type of record- Parameters:
context- a storage context labelkey- a key unique to contextversion- only return record if newer than supplied version- Returns:
- a pair consisting of the version of the record read back, if any, and the record itself
- Throws:
IOException- if errors occur in the read process
-
update
public boolean update(@Nonnull @NotEmpty String context, @Nonnull @NotEmpty String key, @Nonnull @NotEmpty String value, @Nullable @Positive Long expiration) throws IOException Updates an existing record in the store.- Specified by:
updatein interfaceStorageService- Parameters:
context- a storage context labelkey- a key unique to contextvalue- updated valueexpiration- expiration for record, or null- Returns:
- true if the update succeeded, false if the record does not exist
- Throws:
IOException- if errors occur in the update process
-
update
public <T> boolean update(@Nonnull @NotEmpty String context, @Nonnull @NotEmpty String key, @Nonnull T value, @Nonnull StorageSerializer<T> serializer, @Nullable @Positive Long expiration) throws IOException Updates an existing record in the store using a custom serialization strategy.- Specified by:
updatein interfaceStorageService- Type Parameters:
T- type of record- Parameters:
context- a storage context labelkey- a key unique to contextvalue- updated valueserializer- custom serializerexpiration- expiration for record, or null- Returns:
- true if the update succeeded, false if the record does not exist
- Throws:
IOException- if errors occur in the update process
-
update
Updates an existing record in the store, using an annotated object as the source.The individual parameters for the update are extracted from the object using the annotations in the org.opensaml.storage.annotation package. If any are missing, or a field inaccessible, a runtime exception of some kind will occur.
- Specified by:
updatein interfaceStorageService- Parameters:
value- object to update from- Returns:
- true if the update succeeded, false if the record does not exist
- Throws:
IOException- if errors occur in the update process
-
updateWithVersion
public Long updateWithVersion(@Positive long version, @Nonnull @NotEmpty String context, @Nonnull @NotEmpty String key, @Nonnull @NotEmpty String value, @Nullable @Positive Long expiration) throws IOException, VersionMismatchException Updates an existing record in the store, if a version matches.- Specified by:
updateWithVersionin interfaceStorageService- Parameters:
version- only update if the current version matches this valuecontext- a storage context labelkey- a key unique to contextvalue- updated valueexpiration- expiration for record, or null- Returns:
- the version of the record after update, null if no record exists
- Throws:
IOException- if errors occur in the update processVersionMismatchException- if the record has already been updated to a newer version
-
updateWithVersion
@Nullable public <T> Long updateWithVersion(@Positive long version, @Nonnull @NotEmpty String context, @Nonnull @NotEmpty String key, @Nonnull T value, @Nonnull StorageSerializer<T> serializer, @Nullable @Positive Long expiration) throws IOException, VersionMismatchException Updates an existing record in the store, if a version matches, using a custom serialization strategy.- Specified by:
updateWithVersionin interfaceStorageService- Type Parameters:
T- type of record- Parameters:
version- only update if the current version matches this valuecontext- a storage context labelkey- a key unique to contextvalue- updated valueserializer- custom serializerexpiration- expiration for record, or null- Returns:
- the version of the record after update, null if no record exists
- Throws:
IOException- if errors occur in the update processVersionMismatchException- if the record has already been updated to a newer version
-
updateWithVersion
@Nullable public Long updateWithVersion(@Positive long version, @Nonnull Object value) throws IOException, VersionMismatchException Updates an existing record in the store, if a version matches, using an annotated object as the source.The individual parameters for the update are extracted from the object using the annotations in the org.opensaml.storage.annotation package. If any are missing, or a field inaccessible, a runtime exception of some kind will occur.
- Specified by:
updateWithVersionin interfaceStorageService- Parameters:
version- only update if the current version matches this valuevalue- object to update from- Returns:
- the version of the record after update, null if no record exists
- Throws:
IOException- if errors occur in the update processVersionMismatchException- if the record has already been updated to a newer version
-
updateExpiration
public boolean updateExpiration(@Nonnull @NotEmpty String context, @Nonnull @NotEmpty String key, @Nullable @Positive Long expiration) throws IOException Updates expiration of an existing record in the store.- Specified by:
updateExpirationin interfaceStorageService- Parameters:
context- a storage context labelkey- a key unique to contextexpiration- expiration for record, or null- Returns:
- true if the update succeeded, false if the record does not exist
- Throws:
IOException- if errors occur in the update process
-
updateExpiration
Updates expiration of an existing record in the store, using an annotated object as the source.The individual parameters for the update are extracted from the object using the annotations in the org.opensaml.storage.annotation package. If any are missing, or a field inaccessible, a runtime exception of some kind will occur.
- Specified by:
updateExpirationin interfaceStorageService- Parameters:
value- object to update from- Returns:
- true if the update succeeded, false if the record does not exist
- Throws:
IOException- if errors occur in the update process
-
delete
public boolean delete(@Nonnull @NotEmpty String context, @Nonnull @NotEmpty String key) throws IOException Deletes an existing record from the store.- Specified by:
deletein interfaceStorageService- Parameters:
context- a storage context labelkey- a key unique to context- Returns:
- true iff the record existed and was deleted
- Throws:
IOException- if errors occur in the deletion process
-
delete
Deletes an existing record from the store, using an annotated object as the source.The individual parameters for the deletion are extracted from the object using the annotations in the org.opensaml.storage.annotation package. If any are missing, or a field inaccessible, a runtime exception of some kind will occur.
- Specified by:
deletein interfaceStorageService- Parameters:
value- object to delete- Returns:
- true iff the record existed and was deleted
- Throws:
IOException- if errors occur in the deletion process
-
deleteWithVersion
public boolean deleteWithVersion(@Positive long version, @Nonnull @NotEmpty String context, @Nonnull @NotEmpty String key) throws IOException, VersionMismatchException Deletes an existing record from the store if it currently has a specified version.- Specified by:
deleteWithVersionin interfaceStorageService- Parameters:
version- record version to deletecontext- a storage context labelkey- a key unique to context- Returns:
- true iff the record existed and was deleted
- Throws:
IOException- if errors occur in the deletion processVersionMismatchException- if the record has already been updated to a newer version
-
deleteWithVersion
public boolean deleteWithVersion(@Positive long version, @Nonnull Object value) throws IOException, VersionMismatchException Deletes an existing record from the store, using an annotated object as the source, if it currently has a specified version.The individual parameters for the deletion are extracted from the object using the annotations in the org.opensaml.storage.annotation package. If any are missing, or a field inaccessible, a runtime exception of some kind will occur.
- Specified by:
deleteWithVersionin interfaceStorageService- Parameters:
version- record version to deletevalue- object to delete- Returns:
- true iff the record existed and was deleted
- Throws:
IOException- if errors occur in the deletion processVersionMismatchException- if the record has already been updated to a newer version
-
reap
Manually trigger a cleanup of expired records. The method MAY return without guaranteeing that cleanup has already occurred.- Specified by:
reapin interfaceStorageService- Parameters:
context- a storage context label- Throws:
IOException- if errors occur in the cleanup process
-
updateContextExpiration
public void updateContextExpiration(@Nonnull @NotEmpty String context, @Nullable Long expiration) throws IOException Updates the expiration time of all records in the context.- Specified by:
updateContextExpirationin interfaceStorageService- Parameters:
context- a storage context labelexpiration- a new expiration timestamp, or null- Throws:
IOException- if errors occur in the cleanup process
-
deleteContext
Forcibly removes all records in a given context along with any associated resources devoted to maintaining the context.- Specified by:
deleteContextin interfaceStorageService- Parameters:
context- a storage context label- Throws:
IOException- if errors occur in the cleanup process
-
doDestroy
protected void doDestroy()- Overrides:
doDestroyin classAbstractInitializableComponent
-
lookupNamespace
Looks up the namespace for the given context name in the cache.- Parameters:
context- Context name.- Returns:
- Corresponding namespace for given context or null if no namespace exists for context.
- Throws:
IOException- On memcached operation errors.
-
createNamespace
Creates a cache-wide unique namespace for the given context name. The context-namespace mapping is stored in the cache.- Parameters:
context- Context name.- Returns:
- Namespace name for given context.
- Throws:
IOException- On memcached operation errors.
-
memcachedKey
Creates a memcached key from one or more parts.- Parameters:
parts- Key parts (i.e. namespace, local name)- Returns:
- Key comprised of 250 characters or less.
-
handleAsyncResult
private <T> T handleAsyncResult(@Nonnull net.spy.memcached.internal.OperationFuture<T> result) throws IOException Handle async result.- Type Parameters:
T- type of result- Parameters:
result- the result- Returns:
- the result
- Throws:
IOException- if an error occurs
-
updateContextKeyList
private boolean updateContextKeyList(@Nonnull String suffix, @Nonnull String namespace, @Nonnull String key) throws IOException Update context key list.- Parameters:
suffix- the suffixnamespace- the namespacekey- the storage key- Returns:
- whether the update was a success
- Throws:
IOException- if an error occurs
-