public interface MemcacheService extends BaseMemcacheService
The Java API for the App Engine Memcache service. This offers a fast distributed cache for commonly-used data. The cache is limited both in duration and also in total space, so objects stored in it may be discarded at any time.
Note that null
is a legal value to store in the cache, or to use
as a cache key. Although the API is written for Objects, both
keys and values should be Serializable, although future versions
may someday accept specific types of non-Serializable
Objects
.
The values returned from this API are mutable copies from the cache; altering them has no effect upon the cached value itself until assigned with one of the put methods. Likewise, the methods returning collections return mutable collections, but changes do not affect the cache.
Methods that operate on single entries, including #increment, are atomic, while batch methods such as #getAll, #putAll, and #deleteAll do not provide atomicity. Arbitrary operations on single entries can be performed atomically by using #putIfUntouched in combination with #getIdentifiable.
Increment has a number of caveats to its use; please consult the method documentation.
An ErrorHandler configures how errors are treated. The default
error handler is an instance of LogAndContinueErrorHandler. In most
cases this will log the underlying error condition and emulate cache-miss
behavior instead of throwing an error to the calling code. For example, it
returns null
from #get(Object).
A less permissive alternative is StrictErrorHandler, which will instead throw a MemcacheServiceException to expose any errors for application code to resolve.
To guarantee that all MemcacheServiceException are directed to the error handler use a ConsistentErrorHandler such as ErrorHandlers#getConsistentLogAndContinue(Level) or ErrorHandlers#getStrict().
Implements
BaseMemcacheServiceMethods
deleteAll(Collection keys)
public abstract Set <T>deleteAll(Collection keys)
Batch version of #delete(Object).
Parameter | |
---|---|
Name | Description |
keys |
Collection<T> a collection of keys for entries to delete |
Returns | |
---|---|
Type | Description |
Set<T> |
the Set of keys deleted. Any keys in |
deleteAll(Collection keys, long millisNoReAdd)
public abstract Set <T>deleteAll(Collection keys, long millisNoReAdd)
Batch version of #delete(Object, long).
Parameters | |
---|---|
Name | Description |
keys |
Collection<T> a collection of keys for entries to delete |
millisNoReAdd |
long time during which calls to put using SetPolicy#ADD_ONLY_IF_NOT_PRESENT should be denied. |
Returns | |
---|---|
Type | Description |
Set<T> |
the Set of keys deleted. Any keys in |
getAll(Collection keys)
public abstract Map,Object> <T>getAll(Collection keys)
Performs a get of multiple keys at once. This is more efficient than multiple separate calls to #get(Object), and allows a single call to both test for #contains(Object) and also fetch the value, because the return will not include mappings for keys not found.
If an error deserializing the value occurs, this passes an InvalidValueException to the service's ErrorHandler. If a service error occurs, this passes a MemcacheServiceException. See BaseMemcacheService#setErrorHandler(ErrorHandler).
Parameter | |
---|---|
Name | Description |
keys |
Collection<T> a collection of keys for which values should be retrieved |
Returns | |
---|---|
Type | Description |
Map<T,Object> |
a mapping from keys to values of any entries found. If a requested key is not found in the cache, the key will not be in the returned Map. |
getIdentifiables(Collection keys)
public abstract Map,MemcacheService.IdentifiableValue> <T>getIdentifiables(Collection keys)
Performs a getIdentifiable for multiple keys at once. This is more efficient than multiple separate calls to #getIdentifiable(Object).
If an error deserializing the value occurs, this passes an InvalidValueException to the service's ErrorHandler. If a service error occurs, this passes a MemcacheServiceException. See BaseMemcacheService#setErrorHandler(ErrorHandler).
Parameter | |
---|---|
Name | Description |
keys |
Collection<T> a collection of keys for which values should be retrieved |
Returns | |
---|---|
Type | Description |
Map<T,IdentifiableValue> |
a mapping from keys to values of any entries found. If a requested key is not found in the cache, the key will not be in the returned Map. |
getItemsForPeek(Collection keys)
public default Map,MemcacheService.ItemForPeek> <T>getItemsForPeek(Collection keys)
Performs a getItemForPeek for multiple keys at once. This is more efficient than multiple separate calls to #getItemForPeek(Object).
If an error deserializing the value occurs, this passes an InvalidValueException to the service's ErrorHandler. If a service error occurs, this passes a MemcacheServiceException. See BaseMemcacheService#setErrorHandler(ErrorHandler).
Parameter | |
---|---|
Name | Description |
keys |
Collection<T> a collection of keys for which values should be retrieved |
Returns | |
---|---|
Type | Description |
Map<T,ItemForPeek> |
a mapping from keys to ItemForPeek of any entries found. If a requested key is not found in the cache, the key will not be in the returned Map. |
incrementAll(Collection keys, long delta)
public abstract Map,Long> <T>incrementAll(Collection keys, long delta)
Like normal increment, but increments a batch of separate keys in parallel by the same delta. See Also: #increment(Object, long)
Parameters | |
---|---|
Name | Description |
keys |
Collection<T> |
delta |
long |
Returns | |
---|---|
Type | Description |
Map<T,Long> |
mapping keys to their new values; values will be null if they could not be incremented or were not present in the cache |
incrementAll(Collection keys, long delta, Long initialValue)
public abstract Map,Long> <T>incrementAll(Collection keys, long delta, Long initialValue)
Like normal increment, but increments a batch of separate keys in parallel by the same delta and potentially sets a starting value. See Also: #increment(Object, long)
Parameters | |
---|---|
Name | Description |
keys |
Collection<T> |
delta |
long |
initialValue |
Long value to insert into the cache if the key is not present |
Returns | |
---|---|
Type | Description |
Map<T,Long> |
mapping keys to their new values; values will be null if they could not be incremented for whatever reason |
incrementAll(Map offsets)
public abstract Map,Long> <T>incrementAll(Map,Long> offsets)
Like normal increment, but accepts a mapping of separate controllable offsets for each key individually. Good for incrementing by a sum and a count in parallel. See Also: #increment(Object, long)
Parameter | |
---|---|
Name | Description |
offsets |
Map<T,Long> |
Returns | |
---|---|
Type | Description |
Map<T,Long> |
mapping keys to their new values; values will be null if they could not be incremented for whatever reason |
incrementAll(Map offsets, Long initialValue)
public abstract Map,Long> <T>incrementAll(Map,Long> offsets, Long initialValue)
Like normal increment, but accepts a mapping of separate controllable offsets for each key individually. Good for incrementing by a sum and a count in parallel. Callers may also pass an initial value for the keys to take on if they are not already present in the cache. See Also: #increment(Object, long)
Parameters | |
---|---|
Name | Description |
offsets |
Map<T,Long> |
initialValue |
Long |
Returns | |
---|---|
Type | Description |
Map<T,Long> |
mapping keys to their new values; values will be null if they could not be incremented for whatever reason |
putAll(Map values, Expiration expires, MemcacheService.SetPolicy policy)
public abstract Set <T>putAll(Map,?> values, Expiration expires, MemcacheService.SetPolicy policy)
A batch-processing variant of #put. This is more efficiently implemented by the service than multiple calls.
Parameters | |
---|---|
Name | Description |
values |
Map<T,?> the key/value mappings to add to the cache |
expires |
Expiration the expiration time for all |
policy |
MemcacheService.SetPolicy what to do if the entry is or is not already present |
Returns | |
---|---|
Type | Description |
Set<T> |
the set of keys for which entries were created. Keys in
|
putIfUntouched(Map values)
public abstract Set <T>putIfUntouched(Map,MemcacheService.CasValues> values)
Convenience shortcut, equivalent to
the key/values mappings to compare and swap the set of keys for which the new value was stored. A batch-processing variant of #putIfUntouched(Object,
IdentifiableValue,Object,Expiration). This is more efficient than
multiple single value calls. the key/values mappings to compare and swap an Expiration object to set time-based
expiration for a value with a the set of keys for which the new value was stored. Empties the cache of all values across all namespaces. Statistics are not affected. Tests whether a given value is in cache, even if its value is Note that, because an object may be removed from cache at any time, the
following is not sound code: The problem is that the cache could have dropped the entry between the
call to #contains and #get(Object). This is
a sounder pattern: Another alternative is to prefer the key object used to store the cache entry Removes the key of the entry to delete. Removes the given key from the cache, and prevents it from being added
under the SetPolicy#ADD_ONLY_IF_NOT_PRESENT policy for
key to delete time during which calls to put using
ADD_IF_NOT_PRESENT should be denied. Fetches a previously-stored value, or If an error deserializing the value occurs, this passes
an InvalidValueException to the service's ErrorHandler.
If a service error occurs, this passes a MemcacheServiceException.
See BaseMemcacheService#setErrorHandler(ErrorHandler). the key object used to store the cache entry the value object previously stored, or Similar to #get, but returns an object that can later be used
to perform a #putIfUntouched operation. If an error deserializing the value occurs, this passes
an InvalidValueException to the service's ErrorHandler.
If a service error occurs, this passes a MemcacheServiceException.
See BaseMemcacheService#setErrorHandler(ErrorHandler). the key object used to store the cache entry an IdentifiableValue object that wraps the
value object previously stored. Similar to #get, but returns an object that can provide extra timestamp
metadata. If an error deserializing the value occurs, this passes
an InvalidValueException to the service's ErrorHandler.
If a service error occurs, this passes a MemcacheServiceException.
See BaseMemcacheService#setErrorHandler(ErrorHandler). the key object used to store the cache entry an ItemForPeek object that wraps the
value object stored as well as extra meta data timestamps.
Fetches some statistics about the cache and its usage. statistics for the cache. Note that this method returns
aggregated Stats for all namespaces. Response will never be
Atomically fetches, increments, and stores a given integral value.
"Integral" types are Byte, Short, Integer,
Long, and in some cases String. The entry must already
exist, and have a non-negative value. Incrementing by positive amounts will reach signed 64-bit max (
To facilitate use as an atomic countdown, incrementing by a negative
value (i.e. decrementing) will not go below zero: incrementing Note: The actual representation of all numbers in Memcache is a string.
This means if you initially stored a number as a string (e.g., "10") and
then increment it, everything will work properly. When you #get(Object) a key for a string value, wrapping occurs
after exceeding the max value of an unsigned 64-bit number
( If a service error occurs, this passes a MemcacheServiceException to the service's ErrorHandler. See
BaseMemcacheService#setErrorHandler(ErrorHandler). the key of the entry to manipulate the size of the increment, positive or negative the post-increment value, as a long. However, a
#get(Object) of the key will still have the original type (
Byte, Short, etc.). If there is no entry for
Like normal increment, but allows for an optional initial value for the
key to take on if not already present in the cache. Note, the provided initial value can be negative, which allows
incrementing negative numbers. This is in contrast to the base version of
this method, which requires a pre-existing value (e.g. one stored with
#put) to be non-negative prior to being incremented.
See Also: #increment(Object, long) value to insert into the cache if the key is not
present A convenience shortcut, equivalent to put(key, value, null, SetPolicy.SET_ALWAYS). key of the new entry value for the new entry Convenience put, equivalent to put(key, value, expiration, SetPolicy.SET_ALWAYS). key of the new entry value for the new entry time-based Expiration, or Store a new value into the cache, using the key for the new cache entry the value to be stored an Expiration object to set time-based expiration.
Requests particular handling regarding pre-existing entries
under the same key. This parameter must not be Convenience multi-put, equivalent to putAll(values, expires, SetPolicy.SET_ALWAYS). key/value mappings for new entries to add to the cache Convenience multi-put, equivalent to putAll(values, expires, SetPolicy.SET_ALWAYS). key/value mappings to add to the cache expiration time for the new values, or Convenience shortcut, equivalent to put(key, oldValue, newValue, null). key of the entry identifier for the value to compare against newValue new value to store if oldValue is still there Atomically, store If another value in the cache for Note that storing the same value again does count as a "touch"
for this purpose. Using #getIdentifiable and #putIfUntouched together
constitutes an operation that either succeeds atomically or fails due to
concurrency (or eviction), in which case the entire operation can be
retried by the application. key of the entry identifier for the value to compare against newValue new value to store if oldValue is still there an Expiration object to set time-based expiration.
Deprecated. use MemcacheServiceFactory#getMemcacheService(String)
instead.
Parameter
Name
Description
values
Map<T,CasValues>
Returns
Type
Description
Set<T>
public abstract Set
Parameters
Name
Description
values
Map<T,CasValues>
expiration
Expiration
null
expiration value.
null
may be used to indicate no specific expiration.
Returns
Type
Description
Set<T>
clearAll()
public abstract void clearAll()
contains(Object key)
public abstract boolean contains(Object key)
null
.
if (memcache.contains("key")) {
foo = memcache.get("key");
if (foo == null) {
// continue, assuming foo had the real value null
}
}
foo = memcache.get("key");
if (foo == null) {
if (memcache.contains("key")) {
// continue, assuming foo had the real value null
} else {
// continue; foo may have had a real null, but has been dropped now
}
}
Parameter
Name
Description
key
Object
Returns
Type
Description
boolean
true
if the cache contains an entry for the keydelete(Object key)
public abstract boolean delete(Object key)
key
from the cache.
Parameter
Name
Description
key
Object
Returns
Type
Description
boolean
true
if an entry existed, but was discardeddelete(Object key, long millisNoReAdd)
public abstract boolean delete(Object key, long millisNoReAdd)
millisNoReAdd
milliseconds thereafter. Calls to a #put
method using SetPolicy#SET_ALWAYS are not blocked, however.
Parameters
Name
Description
key
Object
millisNoReAdd
long
Returns
Type
Description
boolean
true
if an entry existed to deleteget(Object key)
public abstract Object get(Object key)
null
if unset. To
distinguish a null
value from unset use
MemcacheService#contains(Object).
Parameter
Name
Description
key
Object
Returns
Type
Description
Object
null
getIdentifiable(Object key)
public abstract MemcacheService.IdentifiableValue getIdentifiable(Object key)
Parameter
Name
Description
key
Object
Returns
Type
Description
MemcacheService.IdentifiableValue
null
is returned if key
is not present in the cache.getItemForPeek(Object key)
public default MemcacheService.ItemForPeek getItemForPeek(Object key)
Parameter
Name
Description
key
Object
Returns
Type
Description
MemcacheService.ItemForPeek
null
is returned if key
is not present in the cache.getStatistics()
public abstract Stats getStatistics()
Returns
Type
Description
Stats
null
.increment(Object key, long delta)
public abstract Long increment(Object key, long delta)
2^63 - 1
) and then wrap-around to signed 64-bit min (-2^63
), continuing increments from that point.2
by
-5
will return 0
, not -3
.2^64 - 1
). When you #get(Object) a key
for a numerical type, wrapping occurs after exceeding the type max value.
Parameters
Name
Description
key
Object
delta
long
Returns
Type
Description
Long
key
, returns null
.increment(Object key, long delta, Long initialValue)
public abstract Long increment(Object key, long delta, Long initialValue)
Parameters
Name
Description
key
Object
delta
long
initialValue
Long
Returns
Type
Description
Long
put(Object key, Object value)
public abstract void put(Object key, Object value)
Parameters
Name
Description
key
Object
value
Object
put(Object key, Object value, Expiration expires)
public abstract void put(Object key, Object value, Expiration expires)
Parameters
Name
Description
key
Object
value
Object
expires
Expiration
null
for noneput(Object key, Object value, Expiration expires, MemcacheService.SetPolicy policy)
public abstract boolean put(Object key, Object value, Expiration expires, MemcacheService.SetPolicy policy)
key
, but subject to the
policy
regarding existing entries.
Parameters
Name
Description
key
Object
value
Object
expires
Expiration
null
may be used indicate no specific expiration.
policy
MemcacheService.SetPolicy
null
.
Returns
Type
Description
boolean
true
if a new entry was created, false
if not
because of the policy
putAll(Map,?> values)
public abstract void putAll(Map<?,?> values)
Parameter
Name
Description
values
Map<?,?>
putAll(Map,?> values, Expiration expires)
public abstract void putAll(Map<?,?> values, Expiration expires)
Parameters
Name
Description
values
Map<?,?>
expires
Expiration
null
for no
time-based expirationputIfUntouched(Object key, MemcacheService.IdentifiableValue oldValue, Object newValue)
public abstract boolean putIfUntouched(Object key, MemcacheService.IdentifiableValue oldValue, Object newValue)
Parameters
Name
Description
key
Object
oldValue
MemcacheService.IdentifiableValue
newValue
Object
Returns
Type
Description
boolean
true
if newValue
was stored,
false
otherwise.putIfUntouched(Object key, MemcacheService.IdentifiableValue oldValue, Object newValue, Expiration expires)
public abstract boolean putIfUntouched(Object key, MemcacheService.IdentifiableValue oldValue, Object newValue, Expiration expires)
newValue
only if no other value has been stored
since oldValue
was retrieved. oldValue
is an
IdentifiableValue that was returned from a previous call to
#getIdentifiable.key
has been stored, or if
this cache entry has been evicted, then nothing is stored by this call and
false
is returned.
Parameters
Name
Description
key
Object
oldValue
MemcacheService.IdentifiableValue
newValue
Object
expires
Expiration
null
may be used to indicate no specific expiration.
Returns
Type
Description
boolean
true
if newValue
was stored,
false
otherwisesetNamespace(String newNamespace) (deprecated)
public abstract void setNamespace(String newNamespace)
Parameter
Name
Description
newNamespace
String
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-06-12 UTC.