public class MemCachedClient
extends java.lang.Object
MemCachedClient mc = new MemCachedClient(); // compression is enabled by default mc.setCompressEnable(true); // set compression threshhold to 4 KB (default: 15 KB) mc.setCompressThreshold(4096); // turn on storing primitive types as a string representation // Should not do this in most cases. mc.setPrimitiveAsString(true);
MemCachedClient mc = new MemCachedClient(); String key = "cacheKey1"; Object value = SomeClass.getObject(); mc.set(key, value);
MemCachedClient mc = new MemCachedClient(); String key = "cacheKey1"; Object value = SomeClass.getObject(); Integer hash = new Integer(45); mc.set(key, value, hash);The set method shown above will always set the object in the cache.
MemCachedClient mc = new MemCachedClient(); String key = "cacheKey1"; mc.delete(key);
MemCachedClient mc = new MemCachedClient(); String key = "cacheKey1"; Integer hash = new Integer(45); mc.delete(key, hashCode);
MemCachedClient mc = new MemCachedClient(); String key = "counterKey"; mc.storeCounter(key, new Integer(100)); System.out.println("counter after adding 1: " mc.incr(key)); System.out.println("counter after adding 5: " mc.incr(key, 5)); System.out.println("counter after subtracting 4: " mc.decr(key, 4)); System.out.println("counter after subtracting 1: " mc.decr(key));
MemCachedClient mc = new MemCachedClient(); String key = "counterKey"; Integer hash = new Integer(45); mc.storeCounter(key, new Integer(100), hash); System.out.println("counter after adding 1: " mc.incr(key, 1, hash)); System.out.println("counter after adding 5: " mc.incr(key, 5, hash)); System.out.println("counter after subtracting 4: " mc.decr(key, 4, hash)); System.out.println("counter after subtracting 1: " mc.decr(key, 1, hash));
MemCachedClient mc = new MemCachedClient(); String key = "key"; Object value = mc.get(key);
MemCachedClient mc = new MemCachedClient(); String key = "key"; Integer hash = new Integer(45); Object value = mc.get(key, hash);
MemCachedClient mc = new MemCachedClient(); String[] keys = { "key", "key1", "key2" }; Map<Object> values = mc.getMulti(keys);
MemCachedClient mc = new MemCachedClient(); String[] keys = { "key", "key1", "key2" }; Integer[] hashes = { new Integer(45), new Integer(32), new Integer(44) }; Map<Object> values = mc.getMulti(keys, hashes);
MemCachedClient mc = new MemCachedClient(); mc.flushAll();
MemCachedClient mc = new MemCachedClient(); Map stats = mc.stats();
Modifier and Type | Class and Description |
---|---|
protected class |
MemCachedClient.NIOLoader |
Modifier and Type | Field and Description |
---|---|
private static byte[] |
B_DELETED |
private static byte[] |
B_END |
private static byte[] |
B_NOTFOUND |
private static byte[] |
B_STORED |
private java.lang.ClassLoader |
classLoader |
private static java.lang.String |
CLIENT_ERROR |
private static int |
COMPRESS_THRESH |
private boolean |
compressEnable |
private long |
compressThreshold |
private java.lang.String |
defaultEncoding |
private static java.lang.String |
DELETED |
private static java.lang.String |
END |
private static java.lang.String |
ERROR |
private ErrorHandler |
errorHandler |
static int |
F_COMPRESSED |
static int |
F_SERIALIZED |
private static java.lang.String |
ITEM |
private static org.apache.log4j.Logger |
log |
static int |
MARKER_BOOLEAN |
static int |
MARKER_BYTE |
static int |
MARKER_BYTEARR |
static int |
MARKER_CHARACTER |
static int |
MARKER_DATE |
static int |
MARKER_DOUBLE |
static int |
MARKER_FLOAT |
static int |
MARKER_INTEGER |
static int |
MARKER_LONG |
static int |
MARKER_SHORT |
static int |
MARKER_STRING |
static int |
MARKER_STRINGBUFFER |
static int |
MARKER_STRINGBUILDER |
private static java.lang.String |
NOTFOUND |
private static java.lang.String |
NOTSTORED |
private static java.lang.String |
OK |
private SockIOPool |
pool |
private java.lang.String |
poolName |
private boolean |
primitiveAsString |
private boolean |
sanitizeKeys |
private static java.lang.String |
SERVER_ERROR |
private static java.lang.String |
STATS |
private static java.lang.String |
STORED |
private static java.lang.String |
VALUE |
Constructor and Description |
---|
MemCachedClient()
Creates a new instance of MemCachedClient.
|
MemCachedClient(java.lang.ClassLoader classLoader)
Creates a new instance of MemCacheClient but
acceptes a passed in ClassLoader.
|
MemCachedClient(java.lang.ClassLoader classLoader,
ErrorHandler errorHandler)
Creates a new instance of MemCacheClient but
acceptes a passed in ClassLoader and a passed
in ErrorHandler.
|
MemCachedClient(java.lang.ClassLoader classLoader,
ErrorHandler errorHandler,
java.lang.String poolName)
Creates a new instance of MemCacheClient but
acceptes a passed in ClassLoader, ErrorHandler,
and SockIOPool name.
|
MemCachedClient(java.lang.String poolName)
Creates a new instance of MemCachedClient
accepting a passed in pool name.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(java.lang.String key,
java.lang.Object value)
Adds data to the server; only the key and the value are specified.
|
boolean |
add(java.lang.String key,
java.lang.Object value,
java.util.Date expiry)
Adds data to the server; the key, value, and an expiration time are specified.
|
boolean |
add(java.lang.String key,
java.lang.Object value,
java.util.Date expiry,
java.lang.Integer hashCode)
Adds data to the server; the key, value, and an expiration time are specified.
|
boolean |
add(java.lang.String key,
java.lang.Object value,
java.lang.Integer hashCode)
Adds data to the server; the key, value, and an optional hashcode are passed in.
|
long |
addOrDecr(java.lang.String key)
Thread safe way to initialize and decrement a counter.
|
long |
addOrDecr(java.lang.String key,
long inc)
Thread safe way to initialize and decrement a counter.
|
long |
addOrDecr(java.lang.String key,
long inc,
java.lang.Integer hashCode)
Thread safe way to initialize and decrement a counter.
|
long |
addOrIncr(java.lang.String key)
Thread safe way to initialize and increment a counter.
|
long |
addOrIncr(java.lang.String key,
long inc)
Thread safe way to initialize and increment a counter.
|
long |
addOrIncr(java.lang.String key,
long inc,
java.lang.Integer hashCode)
Thread safe way to initialize and increment a counter.
|
long |
decr(java.lang.String key)
Decrement the value at the specified key by 1, and then return it.
|
long |
decr(java.lang.String key,
long inc)
Decrement the value at the specified key by passed in value, and then return it.
|
long |
decr(java.lang.String key,
long inc,
java.lang.Integer hashCode)
Decrement the value at the specified key by the specified increment, and then return it.
|
boolean |
delete(java.lang.String key)
Deletes an object from cache given cache key.
|
boolean |
delete(java.lang.String key,
java.util.Date expiry)
Deletes an object from cache given cache key and expiration date.
|
boolean |
delete(java.lang.String key,
java.lang.Integer hashCode,
java.util.Date expiry)
Deletes an object from cache given cache key, a delete time, and an optional hashcode.
|
boolean |
flushAll()
Invalidates the entire cache.
|
boolean |
flushAll(java.lang.String[] servers)
Invalidates the entire cache.
|
java.lang.Object |
get(java.lang.String key)
Retrieve a key from the server, using a specific hash.
|
java.lang.Object |
get(java.lang.String key,
java.lang.Integer hashCode)
Retrieve a key from the server, using a specific hash.
|
java.lang.Object |
get(java.lang.String key,
java.lang.Integer hashCode,
boolean asString)
Retrieve a key from the server, using a specific hash.
|
long |
getCounter(java.lang.String key)
Returns value in counter at given key as long.
|
long |
getCounter(java.lang.String key,
java.lang.Integer hashCode)
Returns value in counter at given key as long.
|
java.util.Map<java.lang.String,java.lang.Object> |
getMulti(java.lang.String[] keys)
Retrieve multiple objects from the memcache.
|
java.util.Map<java.lang.String,java.lang.Object> |
getMulti(java.lang.String[] keys,
java.lang.Integer[] hashCodes)
Retrieve multiple keys from the memcache.
|
java.util.Map<java.lang.String,java.lang.Object> |
getMulti(java.lang.String[] keys,
java.lang.Integer[] hashCodes,
boolean asString)
Retrieve multiple keys from the memcache.
|
java.lang.Object[] |
getMultiArray(java.lang.String[] keys)
Retrieve multiple objects from the memcache.
|
java.lang.Object[] |
getMultiArray(java.lang.String[] keys,
java.lang.Integer[] hashCodes)
Retrieve multiple objects from the memcache.
|
java.lang.Object[] |
getMultiArray(java.lang.String[] keys,
java.lang.Integer[] hashCodes,
boolean asString)
Retrieve multiple objects from the memcache.
|
long |
incr(java.lang.String key)
Increment the value at the specified key by 1, and then return it.
|
long |
incr(java.lang.String key,
long inc)
Increment the value at the specified key by passed in val.
|
long |
incr(java.lang.String key,
long inc,
java.lang.Integer hashCode)
Increment the value at the specified key by the specified increment, and then return it.
|
private long |
incrdecr(java.lang.String cmdname,
java.lang.String key,
long inc,
java.lang.Integer hashCode)
Increments/decrements the value at the specified key by inc.
|
private void |
init()
Initializes client object to defaults.
|
boolean |
keyExists(java.lang.String key)
Checks to see if key exists in cache.
|
private void |
loadMulti(LineInputStream input,
java.util.Map<java.lang.String,java.lang.Object> hm,
boolean asString)
This method loads the data from cache into a Map.
|
boolean |
replace(java.lang.String key,
java.lang.Object value)
Updates data on the server; only the key and the value are specified.
|
boolean |
replace(java.lang.String key,
java.lang.Object value,
java.util.Date expiry)
Updates data on the server; the key, value, and an expiration time are specified.
|
boolean |
replace(java.lang.String key,
java.lang.Object value,
java.util.Date expiry,
java.lang.Integer hashCode)
Updates data on the server; the key, value, and an expiration time are specified.
|
boolean |
replace(java.lang.String key,
java.lang.Object value,
java.lang.Integer hashCode)
Updates data on the server; only the key and the value and an optional hash are specified.
|
private java.lang.String |
sanitizeKey(java.lang.String key) |
boolean |
set(java.lang.String key,
java.lang.Object value)
Stores data on the server; only the key and the value are specified.
|
boolean |
set(java.lang.String key,
java.lang.Object value,
java.util.Date expiry)
Stores data on the server; the key, value, and an expiration time are specified.
|
boolean |
set(java.lang.String key,
java.lang.Object value,
java.util.Date expiry,
java.lang.Integer hashCode)
Stores data on the server; the key, value, and an expiration time are specified.
|
boolean |
set(java.lang.String key,
java.lang.Object value,
java.lang.Integer hashCode)
Stores data on the server; only the key and the value are specified.
|
private boolean |
set(java.lang.String cmdname,
java.lang.String key,
java.lang.Object value,
java.util.Date expiry,
java.lang.Integer hashCode,
boolean asString)
Stores data to cache.
|
void |
setClassLoader(java.lang.ClassLoader classLoader)
Sets an optional ClassLoader to be used for
serialization.
|
void |
setCompressEnable(boolean compressEnable)
Enable storing compressed data, provided it meets the threshold requirements.
|
void |
setCompressThreshold(long compressThreshold)
Sets the required length for data to be considered for compression.
|
void |
setDefaultEncoding(java.lang.String defaultEncoding)
Sets default String encoding when storing primitives as Strings.
|
void |
setErrorHandler(ErrorHandler errorHandler)
Sets an optional ErrorHandler.
|
void |
setPrimitiveAsString(boolean primitiveAsString)
Enables storing primitive types as their String values.
|
void |
setSanitizeKeys(boolean sanitizeKeys)
Enables/disables sanitizing keys by URLEncoding.
|
java.util.Map |
stats()
Retrieves stats for all servers.
|
java.util.Map |
stats(java.lang.String[] servers)
Retrieves stats for passed in servers (or all servers).
|
private java.util.Map |
stats(java.lang.String[] servers,
java.lang.String command,
java.lang.String lineStart) |
java.util.Map |
statsCacheDump(int slabNumber,
int limit)
Retrieves items cachedump for all servers.
|
java.util.Map |
statsCacheDump(java.lang.String[] servers,
int slabNumber,
int limit)
Retrieves stats for passed in servers (or all servers).
|
java.util.Map |
statsItems()
Retrieves stats items for all servers.
|
java.util.Map |
statsItems(java.lang.String[] servers)
Retrieves stats for passed in servers (or all servers).
|
java.util.Map |
statsSlabs()
Retrieves stats items for all servers.
|
java.util.Map |
statsSlabs(java.lang.String[] servers)
Retrieves stats for passed in servers (or all servers).
|
boolean |
storeCounter(java.lang.String key,
long counter)
Store a counter to memcached given a key
|
boolean |
storeCounter(java.lang.String key,
java.lang.Long counter)
Store a counter to memcached given a key
|
boolean |
storeCounter(java.lang.String key,
java.lang.Long counter,
java.lang.Integer hashCode)
Store a counter to memcached given a key
|
private static org.apache.log4j.Logger log
private static final java.lang.String VALUE
private static final java.lang.String STATS
private static final java.lang.String ITEM
private static final java.lang.String DELETED
private static final java.lang.String NOTFOUND
private static final java.lang.String STORED
private static final java.lang.String NOTSTORED
private static final java.lang.String OK
private static final java.lang.String END
private static final java.lang.String ERROR
private static final java.lang.String CLIENT_ERROR
private static final java.lang.String SERVER_ERROR
private static final byte[] B_END
private static final byte[] B_NOTFOUND
private static final byte[] B_DELETED
private static final byte[] B_STORED
private static final int COMPRESS_THRESH
public static final int MARKER_BYTE
public static final int MARKER_BOOLEAN
public static final int MARKER_INTEGER
public static final int MARKER_LONG
public static final int MARKER_CHARACTER
public static final int MARKER_STRING
public static final int MARKER_STRINGBUFFER
public static final int MARKER_FLOAT
public static final int MARKER_SHORT
public static final int MARKER_DOUBLE
public static final int MARKER_DATE
public static final int MARKER_STRINGBUILDER
public static final int MARKER_BYTEARR
public static final int F_COMPRESSED
public static final int F_SERIALIZED
private boolean sanitizeKeys
private boolean primitiveAsString
private boolean compressEnable
private long compressThreshold
private java.lang.String defaultEncoding
private SockIOPool pool
private java.lang.String poolName
private java.lang.ClassLoader classLoader
private ErrorHandler errorHandler
public MemCachedClient()
public MemCachedClient(java.lang.String poolName)
poolName
- name of SockIOPoolpublic MemCachedClient(java.lang.ClassLoader classLoader)
classLoader
- ClassLoader object.public MemCachedClient(java.lang.ClassLoader classLoader, ErrorHandler errorHandler)
classLoader
- ClassLoader object.errorHandler
- ErrorHandler object.public MemCachedClient(java.lang.ClassLoader classLoader, ErrorHandler errorHandler, java.lang.String poolName)
classLoader
- ClassLoader object.errorHandler
- ErrorHandler object.poolName
- SockIOPool nameprivate void init()
public void setClassLoader(java.lang.ClassLoader classLoader)
classLoader
- public void setErrorHandler(ErrorHandler errorHandler)
errorHandler
- public void setSanitizeKeys(boolean sanitizeKeys)
sanitizeKeys
- if true, then URLEncode all keyspublic void setPrimitiveAsString(boolean primitiveAsString)
primitiveAsString
- if true, then store all primitives as their string value.public void setDefaultEncoding(java.lang.String defaultEncoding)
defaultEncoding
- public void setCompressEnable(boolean compressEnable)
compressEnable
- true
to enable compression, false
to disable compressionpublic void setCompressThreshold(long compressThreshold)
compressThreshold
- required length of data to consider compressionpublic boolean keyExists(java.lang.String key)
key
- the key to look forpublic boolean delete(java.lang.String key)
key
- the key to be removedtrue
, if the data was deleted successfullypublic boolean delete(java.lang.String key, java.util.Date expiry)
key
- the key to be removedexpiry
- when to expire the record.true
, if the data was deleted successfullypublic boolean delete(java.lang.String key, java.lang.Integer hashCode, java.util.Date expiry)
add
and replace
set
will succeed,key
- the key to be removedhashCode
- if not null, then the int hashcode to useexpiry
- when to expire the record.true
, if the data was deleted successfullypublic boolean set(java.lang.String key, java.lang.Object value)
key
- key to store data undervalue
- value to storepublic boolean set(java.lang.String key, java.lang.Object value, java.lang.Integer hashCode)
key
- key to store data undervalue
- value to storehashCode
- if not null, then the int hashcode to usepublic boolean set(java.lang.String key, java.lang.Object value, java.util.Date expiry)
key
- key to store data undervalue
- value to storeexpiry
- when to expire the recordpublic boolean set(java.lang.String key, java.lang.Object value, java.util.Date expiry, java.lang.Integer hashCode)
key
- key to store data undervalue
- value to storeexpiry
- when to expire the recordhashCode
- if not null, then the int hashcode to usepublic boolean add(java.lang.String key, java.lang.Object value)
key
- key to store data undervalue
- value to storepublic boolean add(java.lang.String key, java.lang.Object value, java.lang.Integer hashCode)
key
- key to store data undervalue
- value to storehashCode
- if not null, then the int hashcode to usepublic boolean add(java.lang.String key, java.lang.Object value, java.util.Date expiry)
key
- key to store data undervalue
- value to storeexpiry
- when to expire the recordpublic boolean add(java.lang.String key, java.lang.Object value, java.util.Date expiry, java.lang.Integer hashCode)
key
- key to store data undervalue
- value to storeexpiry
- when to expire the recordhashCode
- if not null, then the int hashcode to usepublic boolean replace(java.lang.String key, java.lang.Object value)
key
- key to store data undervalue
- value to storepublic boolean replace(java.lang.String key, java.lang.Object value, java.lang.Integer hashCode)
key
- key to store data undervalue
- value to storehashCode
- if not null, then the int hashcode to usepublic boolean replace(java.lang.String key, java.lang.Object value, java.util.Date expiry)
key
- key to store data undervalue
- value to storeexpiry
- when to expire the recordpublic boolean replace(java.lang.String key, java.lang.Object value, java.util.Date expiry, java.lang.Integer hashCode)
key
- key to store data undervalue
- value to storeexpiry
- when to expire the recordhashCode
- if not null, then the int hashcode to useprivate boolean set(java.lang.String cmdname, java.lang.String key, java.lang.Object value, java.util.Date expiry, java.lang.Integer hashCode, boolean asString)
cmdname
- action to take (set, add, replace)key
- key to store cache undervalue
- object to cacheexpiry
- expirationhashCode
- if not null, then the int hashcode to useasString
- store this object as a string?public boolean storeCounter(java.lang.String key, long counter)
key
- cache keycounter
- number to storepublic boolean storeCounter(java.lang.String key, java.lang.Long counter)
key
- cache keycounter
- number to storepublic boolean storeCounter(java.lang.String key, java.lang.Long counter, java.lang.Integer hashCode)
key
- cache keycounter
- number to storehashCode
- if not null, then the int hashcode to usepublic long getCounter(java.lang.String key)
key
- cache ketpublic long getCounter(java.lang.String key, java.lang.Integer hashCode)
key
- cache kethashCode
- if not null, then the int hashcode to usepublic long addOrIncr(java.lang.String key)
key
- key where the data is storedpublic long addOrIncr(java.lang.String key, long inc)
key
- key where the data is storedinc
- value to set or increment bypublic long addOrIncr(java.lang.String key, long inc, java.lang.Integer hashCode)
key
- key where the data is storedinc
- value to set or increment byhashCode
- if not null, then the int hashcode to usepublic long addOrDecr(java.lang.String key)
key
- key where the data is storedpublic long addOrDecr(java.lang.String key, long inc)
key
- key where the data is storedinc
- value to set or increment bypublic long addOrDecr(java.lang.String key, long inc, java.lang.Integer hashCode)
key
- key where the data is storedinc
- value to set or increment byhashCode
- if not null, then the int hashcode to usepublic long incr(java.lang.String key)
key
- key where the data is storedpublic long incr(java.lang.String key, long inc)
key
- key where the data is storedinc
- how much to increment bypublic long incr(java.lang.String key, long inc, java.lang.Integer hashCode)
key
- key where the data is storedinc
- how much to increment byhashCode
- if not null, then the int hashcode to usepublic long decr(java.lang.String key)
key
- key where the data is storedpublic long decr(java.lang.String key, long inc)
key
- key where the data is storedinc
- how much to increment bypublic long decr(java.lang.String key, long inc, java.lang.Integer hashCode)
key
- key where the data is storedinc
- how much to increment byhashCode
- if not null, then the int hashcode to useprivate long incrdecr(java.lang.String cmdname, java.lang.String key, long inc, java.lang.Integer hashCode)
cmdname
- increment/decrementkey
- cache keyinc
- amount to incr or decrhashCode
- if not null, then the int hashcode to usepublic java.lang.Object get(java.lang.String key)
key
- key where data is storedpublic java.lang.Object get(java.lang.String key, java.lang.Integer hashCode)
key
- key where data is storedhashCode
- if not null, then the int hashcode to usepublic java.lang.Object get(java.lang.String key, java.lang.Integer hashCode, boolean asString)
key
- key where data is storedhashCode
- if not null, then the int hashcode to useasString
- if true, then return string valpublic java.lang.Object[] getMultiArray(java.lang.String[] keys)
get()
, since itkeys
- String array of keys to retrievepublic java.lang.Object[] getMultiArray(java.lang.String[] keys, java.lang.Integer[] hashCodes)
get()
, since itkeys
- String array of keys to retrievehashCodes
- if not null, then the Integer array of hashCodespublic java.lang.Object[] getMultiArray(java.lang.String[] keys, java.lang.Integer[] hashCodes, boolean asString)
get()
, since itkeys
- String array of keys to retrievehashCodes
- if not null, then the Integer array of hashCodesasString
- if true, retrieve string valspublic java.util.Map<java.lang.String,java.lang.Object> getMulti(java.lang.String[] keys)
get()
, since itkeys
- String array of keys to retrievepublic java.util.Map<java.lang.String,java.lang.Object> getMulti(java.lang.String[] keys, java.lang.Integer[] hashCodes)
get()
, since itkeys
- keys to retrievehashCodes
- if not null, then the Integer array of hashCodespublic java.util.Map<java.lang.String,java.lang.Object> getMulti(java.lang.String[] keys, java.lang.Integer[] hashCodes, boolean asString)
get()
, since itkeys
- keys to retrievehashCodes
- if not null, then the Integer array of hashCodesasString
- if true then retrieve using String valprivate void loadMulti(LineInputStream input, java.util.Map<java.lang.String,java.lang.Object> hm, boolean asString) throws java.io.IOException
sock
- socket waiting to pass back datahm
- hashmap to store data intoasString
- if true, and if we are using NativehHandler, return string valjava.io.IOException
- if io exception happens while reading from socketprivate java.lang.String sanitizeKey(java.lang.String key) throws java.io.UnsupportedEncodingException
java.io.UnsupportedEncodingException
public boolean flushAll()
public boolean flushAll(java.lang.String[] servers)
servers
- optional array of host(s) to flush (host:port)public java.util.Map stats()
public java.util.Map stats(java.lang.String[] servers)
servers
- string array of servers to retrieve stats from, or all if this is nullpublic java.util.Map statsItems()
public java.util.Map statsItems(java.lang.String[] servers)
servers
- string array of servers to retrieve stats from, or all if this is nullpublic java.util.Map statsSlabs()
public java.util.Map statsSlabs(java.lang.String[] servers)
servers
- string array of servers to retrieve stats from, or all if this is nullpublic java.util.Map statsCacheDump(int slabNumber, int limit)
slabNumber
- the item number of the cache dumppublic java.util.Map statsCacheDump(java.lang.String[] servers, int slabNumber, int limit)
servers
- string array of servers to retrieve stats from, or all if this is nullslabNumber
- the item number of the cache dumpprivate java.util.Map stats(java.lang.String[] servers, java.lang.String command, java.lang.String lineStart)
Copyright © 2005 - greg whalin