Class MemcachedStorageService

  • All Implemented Interfaces:
    Component, DestructableComponent, IdentifiableComponent, IdentifiedComponent, InitializableComponent, StorageService

    public class MemcachedStorageService
    extends AbstractIdentifiableInitializableComponent
    implements StorageService
    Memcached storage service. The implementation of context names is based on the implementation of simulated namespaces discussed on the Memcached project site:

    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

    1. The memcached binary protocol is strong recommended for efficiency and full versioning support. In particular, deleteWithVersion(long, String, String) and deleteWithVersion(long, Object) will throw runtime errors under the ASCII protocol.
    2. 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.