public class MemCachedClient extends Object
static {
String[] serverlist = { "server1.com:port", "server2.com:port" };
SockIOPool pool = SockIOPool.getInstance();
pool.setServers(serverlist);
pool.initialize();
}
MemCachedClient mc = new MemCachedClient();
MemCachedClient mc = new MemCachedClient(); String key = "cacheKey1"; Object value = SomeClass.getObject(); mc.set(key, value);
MemCachedClient mc = new MemCachedClient(); String key = "cacheKey1"; mc.delete(key);
MemCachedClient mc = new MemCachedClient(); String key = "key"; Object value = mc.get(key);
MemCachedClient mc = new MemCachedClient();
String[] keys = { "key", "key1", "key2" };
Map<Object> values = mc.getMulti(keys);
MemCachedClient mc = new MemCachedClient(); mc.flushAll();
| Modifier and Type | Field and Description |
|---|---|
static byte |
AUTH_FAILED |
static byte[] |
B_DELETED |
static byte[] |
B_END |
static byte[] |
B_NOTFOUND |
static byte[] |
B_RETURN |
static byte[] |
B_STORED |
byte[] |
BLAND_DATA_SIZE
This is used for set command only.
The reason for use this that we have to put the size of the object bytes into the command line. |
protected ClassLoader |
classLoader |
static String |
CLIENT_ERROR |
static int |
COMPRESS_THRESH |
static String |
DELETED |
static String |
END |
static String |
ERROR |
protected ErrorHandler |
errorHandler |
static int |
F_COMPRESSED |
static int |
F_SERIALIZED |
static byte |
FURTHER_AUTH |
static String |
ITEM |
static org.slf4j.Logger |
log |
static byte |
MAGIC_REQ
The following static constants are used for the binary protocol.
|
static byte |
MAGIC_RESP |
static int |
MARKER_BOOLEAN |
static int |
MARKER_BYTE
values for cache flags
|
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_OTHERS |
static int |
MARKER_SHORT |
static int |
MARKER_STRING |
static int |
MARKER_STRINGBUFFER |
static int |
MARKER_STRINGBUILDER |
static String |
NOTFOUND |
static String |
OK |
static byte |
OPCODE_ADD |
static byte |
OPCODE_APPEND |
static byte |
OPCODE_AUTH_LIST |
static byte |
OPCODE_AUTH_STEPS |
static byte |
OPCODE_DECREMENT |
static byte |
OPCODE_DELETE |
static byte |
OPCODE_FLUSH |
static byte |
OPCODE_GET |
static byte |
OPCODE_GETK |
static byte |
OPCODE_GETKQ |
static byte |
OPCODE_GETQ |
static byte |
OPCODE_INCREMENT |
static byte |
OPCODE_NOOP |
static byte |
OPCODE_PREPEND |
static byte |
OPCODE_QUIT |
static byte |
OPCODE_REPLACE |
static byte |
OPCODE_SET |
static byte |
OPCODE_START_AUTH |
static byte |
OPCODE_STAT |
static byte |
OPCODE_VERSION |
static String |
SERVER_ERROR |
static int |
STAT_INVALID_ARGUMENTS |
static int |
STAT_ITEM_NOT_STORED |
static int |
STAT_KEY_EXISTS |
static int |
STAT_KEY_NOT_FOUND |
static int |
STAT_NO_ERROR |
static int |
STAT_OUT_OF_MEMORY |
static int |
STAT_UNKNOWN_COMMAND |
static int |
STAT_VALUE_TOO_BIG |
static String |
STATS |
static String |
STORED |
static String |
SYNCED |
static String |
VALUE |
| Modifier | Constructor and Description |
|---|---|
|
MemCachedClient()
Creates a new instance of MemCachedClient.
|
|
MemCachedClient(boolean binaryProtocal)
Creates a new instance of MemCachedClient.
Use binary protocol with TCP. |
|
MemCachedClient(boolean isTCP,
boolean binaryProtocal)
Create default memcached client.
|
|
MemCachedClient(ClassLoader classLoader)
Deprecated.
will be removed in next release.
Please use customized transcoder AbstractTransCoder to achieve
the same objective. |
|
MemCachedClient(ClassLoader classLoader,
ErrorHandler errorHandler)
Deprecated.
will be removed in next release.
Please use customized transcoder AbstractTransCoder to achieve
the same objective. |
|
MemCachedClient(ClassLoader classLoader,
ErrorHandler errorHandler,
String poolName)
Deprecated.
will be removed in next release.
Please use customized transcoder AbstractTransCoder to achieve
the same objective. |
protected |
MemCachedClient(MemCachedClient client)
used when you want to create a ASCIIClient/BinaryClient/UDPASCIIClient
instance
|
|
MemCachedClient(String poolName)
Creates a new instance of MemCachedClient accepting a passed in pool
name.
|
|
MemCachedClient(String poolName,
boolean binaryProtocal)
Creates a new instance of MemCachedClient accepting a passed in pool
name.
|
|
MemCachedClient(String poolName,
boolean isTcp,
boolean binaryProtocal)
Create memcached client.
|
|
MemCachedClient(String poolName,
boolean isTcp,
boolean binaryProtocol,
ClassLoader cl,
ErrorHandler eh)
Deprecated.
will be removed in next release.
Please use customized transcoder AbstractTransCoder to achieve
the same objective. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(String key,
Object value)
Adds data to the server; only the key and the value are specified.
|
boolean |
add(String key,
Object value,
Date expiry)
Adds data to the server; the key, value, and an expiration time are
specified.
|
boolean |
add(String key,
Object value,
Date expiry,
Integer hashCode)
Adds data to the server; the key, value, and an expiration time are
specified.
|
boolean |
add(String key,
Object value,
Integer hashCode)
Adds data to the server; the key, value, and an optional hashcode are
passed in.
|
long |
addOrDecr(String key)
Thread safe way to initialize and decrement a counter.
|
long |
addOrDecr(String key,
long inc)
Thread safe way to initialize and decrement a counter.
|
long |
addOrDecr(String key,
long inc,
Integer hashCode)
Thread safe way to initialize and decrement a counter.
|
long |
addOrIncr(String key)
Thread safe way to initialize and increment a counter.
|
long |
addOrIncr(String key,
long inc)
Thread safe way to initialize and increment a counter.
|
long |
addOrIncr(String key,
long inc,
Integer hashCode)
Thread safe way to initialize and increment a counter.
|
boolean |
append(String key,
Object value) |
boolean |
append(String key,
Object value,
Integer hashCode) |
boolean |
cas(String key,
Object value,
Date expiry,
Integer hashCode,
long casUnique) |
boolean |
cas(String key,
Object value,
Date expiry,
long casUnique) |
boolean |
cas(String key,
Object value,
Integer hashCode,
long casUnique) |
boolean |
cas(String key,
Object value,
long casUnique) |
long |
decr(String key)
Decrement the value at the specified key by 1, and then return it.
Please make sure setPrimitiveAsString=true if the key/value pair is stored with set command. |
long |
decr(String key,
long inc)
Decrement the value at the specified key by passed in value, and then
return it.
Please make sure setPrimitiveAsString=true if the key/value pair is stored with set command. |
long |
decr(String key,
long inc,
Integer hashCode)
Decrement the value at the specified key by the specified increment, and
then return it.
Please make sure setPrimitiveAsString=true if the key/value pair is stored with set command. |
boolean |
delete(String key)
Deletes an object from cache given cache key.
|
boolean |
delete(String key,
Date expiry)
Deprecated.
not supported in memcached 1.4+
|
boolean |
delete(String key,
Integer hashCode,
Date expiry)
Deprecated.
not supported in memcached 1.4+
|
boolean |
flushAll()
Invalidates the entire cache.
|
boolean |
flushAll(String[] servers)
Invalidates the entire cache.
|
Object |
get(String key)
Retrieve a key from the server, using a specific hash.
|
Object |
get(String key,
Integer hashCode)
Retrieve a key from the server, using a specific hash.
|
Object |
get(String key,
Integer hashCode,
boolean asString)
Retrieve a key from the server, using a specific hash.
|
long |
getCounter(String key)
Returns value in counter at given key as long.
|
long |
getCounter(String key,
Integer hashCode)
Returns value in counter at given key as long.
|
Map<String,Object> |
getMulti(String[] keys)
Retrieve multiple objects from the memcache.
|
Map<String,Object> |
getMulti(String[] keys,
Integer[] hashCodes)
Retrieve multiple keys from the memcache.
|
Map<String,Object> |
getMulti(String[] keys,
Integer[] hashCodes,
boolean asString)
Retrieve multiple keys from the memcache.
|
Object[] |
getMultiArray(String[] keys)
Retrieve multiple objects from the memcache.
|
Object[] |
getMultiArray(String[] keys,
Integer[] hashCodes)
Retrieve multiple objects from the memcache.
|
Object[] |
getMultiArray(String[] keys,
Integer[] hashCodes,
boolean asString)
Retrieve multiple objects from the memcache.
|
MemcachedItem |
gets(String key) |
MemcachedItem |
gets(String key,
Integer hashCode) |
long |
incr(String key)
Increment the value at the specified key by 1, and then return it.
Please make sure setPrimitiveAsString=true if the key/value pair is stored with set command. |
long |
incr(String key,
long inc)
Increment the value at the specified key by passed in val.
Please make sure setPrimitiveAsString=true if the key/value pair is stored with set command. |
long |
incr(String key,
long inc,
Integer hashCode)
Increment the value at the specified key by the specified increment, and
then return it.
Please make sure setPrimitiveAsString=true if the key/value pair is stored with set command. |
boolean |
isUseBinaryProtocol() |
boolean |
keyExists(String key)
Checks to see if key exists in cache.
|
boolean |
prepend(String key,
Object value) |
boolean |
prepend(String key,
Object value,
Integer hashCode) |
boolean |
replace(String key,
Object value)
Updates data on the server; only the key and the value are specified.
|
boolean |
replace(String key,
Object value,
Date expiry)
Updates data on the server; the key, value, and an expiration time are
specified.
|
boolean |
replace(String key,
Object value,
Date expiry,
Integer hashCode)
Updates data on the server; the key, value, and an expiration time are
specified.
|
boolean |
replace(String key,
Object value,
Integer hashCode)
Updates data on the server; only the key and the value and an optional
hash are specified.
|
boolean |
set(String key,
Object value)
Stores data on the server; only the key and the value are specified.
|
boolean |
set(String key,
Object value,
Date expiry)
Stores data on the server; the key, value, and an expiration time are
specified.
|
boolean |
set(String key,
Object value,
Date expiry,
Integer hashCode)
Stores data on the server; the key, value, and an expiration time are
specified.
|
boolean |
set(String key,
Object value,
Integer hashCode)
Stores data on the server; only the key and the value are specified.
|
void |
setClassLoader(ClassLoader classLoader)
Deprecated.
will be removed in next release.
Please use customized transcoder AbstractTransCoder to achieve
the same objective. |
void |
setCompressEnable(boolean compressEnable)
Deprecated.
will be removed in next release.
|
void |
setCompressThreshold(long compressThreshold)
Deprecated.
will be removed in next release.
|
void |
setDefaultEncoding(String defaultEncoding)
Sets default String encoding when storing primitives as Strings.
|
void |
setErrorHandler(ErrorHandler errorHandler)
Deprecated.
will be removed in next release. The purpose of adding this
support was to make it compatible with previous releases.
|
void |
setPrimitiveAsString(boolean primitiveAsString)
Enables storing primitive types as their String values.
|
void |
setSanitizeKeys(boolean sanitizeKeys)
Enables/disables sanitizing keys by URLEncoding.
|
void |
setTransCoder(TransCoder transCoder) |
Map<String,Map<String,String>> |
stats()
Retrieves stats for all servers.
|
Map<String,Map<String,String>> |
stats(String[] servers)
Retrieves stats for passed in servers (or all servers).
|
Map<String,Map<String,String>> |
statsCacheDump(int slabNumber,
int limit)
Retrieves items cachedump for all servers.
|
Map<String,Map<String,String>> |
statsCacheDump(String[] servers,
int slabNumber,
int limit)
Retrieves stats for passed in servers (or all servers).
|
Map<String,Map<String,String>> |
statsItems()
Retrieves stats items for all servers.
|
Map<String,Map<String,String>> |
statsItems(String[] servers)
Retrieves stats for passed in servers (or all servers).
|
Map<String,Map<String,String>> |
statsSlabs()
Retrieves stats items for all servers.
|
Map<String,Map<String,String>> |
statsSlabs(String[] servers)
Retrieves stats for passed in servers (or all servers).
|
boolean |
storeCounter(String key,
Long counter)
Store a counter to memcached given a key
|
boolean |
storeCounter(String key,
Long counter,
Date date)
Store a counter to memcached given a key
|
boolean |
storeCounter(String key,
Long counter,
Date date,
Integer hashCode)
Store a counter to memcached given a key
|
boolean |
storeCounter(String key,
Long counter,
Integer hashCode)
Store a counter to memcached given a key
|
boolean |
sync(String key) |
boolean |
sync(String key,
Integer hashCode) |
boolean |
syncAll() |
boolean |
syncAll(String[] servers) |
protected ClassLoader classLoader
protected ErrorHandler errorHandler
public static org.slf4j.Logger log
public static final String VALUE
public static final String STATS
public static final String ITEM
public static final String DELETED
public static final String SYNCED
public static final String NOTFOUND
public static final String STORED
public static final String OK
public static final String END
public static final String ERROR
public static final String CLIENT_ERROR
public static final int COMPRESS_THRESH
public static final String SERVER_ERROR
public static final byte[] B_RETURN
public static final byte[] B_END
public static final byte[] B_NOTFOUND
public static final byte[] B_DELETED
public static final byte[] B_STORED
public static final byte MAGIC_REQ
public static final byte MAGIC_RESP
public static final int F_COMPRESSED
public static final int F_SERIALIZED
public static final int STAT_NO_ERROR
public static final int STAT_KEY_NOT_FOUND
public static final int STAT_KEY_EXISTS
public static final int STAT_VALUE_TOO_BIG
public static final int STAT_INVALID_ARGUMENTS
public static final int STAT_ITEM_NOT_STORED
public static final int STAT_UNKNOWN_COMMAND
public static final int STAT_OUT_OF_MEMORY
public static final byte OPCODE_GET
public static final byte OPCODE_SET
public static final byte OPCODE_ADD
public static final byte OPCODE_REPLACE
public static final byte OPCODE_DELETE
public static final byte OPCODE_INCREMENT
public static final byte OPCODE_DECREMENT
public static final byte OPCODE_QUIT
public static final byte OPCODE_FLUSH
public static final byte OPCODE_GETQ
public static final byte OPCODE_NOOP
public static final byte OPCODE_VERSION
public static final byte OPCODE_GETK
public static final byte OPCODE_GETKQ
public static final byte OPCODE_APPEND
public static final byte OPCODE_PREPEND
public static final byte OPCODE_STAT
public static final byte OPCODE_AUTH_LIST
public static final byte OPCODE_START_AUTH
public static final byte OPCODE_AUTH_STEPS
public static final byte AUTH_FAILED
public static final byte FURTHER_AUTH
public final byte[] BLAND_DATA_SIZE
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 MARKER_OTHERS
protected MemCachedClient(MemCachedClient client)
public MemCachedClient()
public MemCachedClient(boolean binaryProtocal)
binaryProtocal - whether use binary protocol.public MemCachedClient(String poolName)
poolName - name of SockIOPoolpublic MemCachedClient(String poolName, boolean binaryProtocal)
poolName - name of SockIOPoolbinaryProtocal - whether use binary protocol.public MemCachedClient(boolean isTCP,
boolean binaryProtocal)
isTCP - true - use tcp protocol binaryProtocal - use binary protocol.public MemCachedClient(String poolName, boolean isTcp, boolean binaryProtocal)
poolName - name of SockIOPoolisTCP - use tcp protocolbinaryProtocal - use binary protocol.public MemCachedClient(String poolName, boolean isTcp, boolean binaryProtocol, ClassLoader cl, ErrorHandler eh)
AbstractTransCoder to achieve
the same objective.poolName - pool nameisTcp - is tcp protocol?binaryProtocol - is binary protocol?cl - classloader.eh - errorhandler.public MemCachedClient(ClassLoader classLoader)
AbstractTransCoder to achieve
the same objective.classLoader - ClassLoader object.public MemCachedClient(ClassLoader classLoader, ErrorHandler errorHandler)
AbstractTransCoder to achieve
the same objective.classLoader - ClassLoader object.errorHandler - ErrorHandler object.public MemCachedClient(ClassLoader classLoader, ErrorHandler errorHandler, String poolName)
AbstractTransCoder to achieve
the same objective.classLoader - ClassLoader object.errorHandler - ErrorHandler object.poolName - SockIOPool namepublic boolean isUseBinaryProtocol()
public void setClassLoader(ClassLoader classLoader)
AbstractTransCoder to achieve
the same objective.classLoader - public void setErrorHandler(ErrorHandler errorHandler)
errorHandler - 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 void setDefaultEncoding(String defaultEncoding)
defaultEncoding - public void setPrimitiveAsString(boolean primitiveAsString)
primitiveAsString - if true, then store all primitives as their string value.public void setSanitizeKeys(boolean sanitizeKeys)
sanitizeKeys - if true, then URLEncode all keyspublic boolean keyExists(String key)
key - the key to look forpublic boolean delete(String key)
key - the key to be removedtrue, if the data was deleted successfullypublic boolean delete(String key, Date expiry)
key - the key to be removedexpiry - when to expire the record.true, if the data was deleted successfullypublic boolean delete(String key, Integer hashCode, Date expiry)
add and
replaceset 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(String key, Object value)
key - key to store data undervalue - value to storepublic boolean set(String key, Object value, Integer hashCode)
key - key to store data undervalue - value to storehashCode - if not null, then the int hashcode to usepublic boolean set(String key, Object value, Date expiry)
key - key to store data undervalue - value to storeexpiry - when to expire the recordpublic boolean set(String key, Object value, Date expiry, 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(String key, Object value)
key - key to store data undervalue - value to storepublic boolean add(String key, Object value, Integer hashCode)
key - key to store data undervalue - value to storehashCode - if not null, then the int hashcode to usepublic boolean add(String key, Object value, Date expiry)
key - key to store data undervalue - value to storeexpiry - when to expire the recordpublic boolean add(String key, Object value, Date expiry, 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(String key, Object value)
key - key to store data undervalue - value to storepublic boolean replace(String key, Object value, Integer hashCode)
key - key to store data undervalue - value to storehashCode - if not null, then the int hashcode to usepublic boolean replace(String key, Object value, Date expiry)
key - key to store data undervalue - value to storeexpiry - when to expire the recordpublic boolean replace(String key, Object value, Date expiry, 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 storeCounter(String key, Long counter)
key - cache keycounter - number to storepublic boolean storeCounter(String key, Long counter, Date date)
key - cache keycounter - number to storedate - when to expire the recordpublic boolean storeCounter(String key, Long counter, Date date, Integer hashCode)
key - cache keycounter - number to storedate - when to expire the recordhashCode - if not null, then the int hashcode to usepublic boolean storeCounter(String key, Long counter, Integer hashCode)
key - cache keycounter - number to storehashCode - if not null, then the int hashcode to usepublic long getCounter(String key)
key - cache ketpublic long getCounter(String key, Integer hashCode)
key - cache kethashCode - if not null, then the int hashcode to usepublic long addOrIncr(String key)
key - key where the data is storedpublic long addOrIncr(String key, long inc)
key - key where the data is storedinc - value to set or increment bypublic long addOrIncr(String key, long inc, 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(String key)
key - key where the data is storedpublic long addOrDecr(String key, long inc)
key - key where the data is storedinc - value to set or increment bypublic long addOrDecr(String key, long inc, 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(String key)
key - key where the data is storedpublic long incr(String key, long inc)
key - key where the data is storedinc - how much to increment bypublic long incr(String key, long inc, 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(String key)
key - key where the data is storedpublic long decr(String key, long inc)
key - key where the data is storedinc - how much to increment bypublic long decr(String key, long inc, Integer hashCode)
key - key where the data is storedinc - how much to increment byhashCode - if not null, then the int hashcode to usepublic Object get(String key)
key - key where data is storedpublic Object get(String key, Integer hashCode)
key - key where data is storedhashCode - if not null, then the int hashcode to usepublic MemcachedItem gets(String key)
public MemcachedItem gets(String key, Integer hashCode)
public void setTransCoder(TransCoder transCoder)
public Object get(String key, 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 Object[] getMultiArray(String[] keys)
get(),
since itkeys - String array of keys to retrievepublic Object[] getMultiArray(String[] keys, Integer[] hashCodes)
get(),
since itkeys - String array of keys to retrievehashCodes - if not null, then the Integer array of hashCodespublic Object[] getMultiArray(String[] keys, 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 Map<String,Object> getMulti(String[] keys)
get(),
since itkeys - String array of keys to retrievepublic Map<String,Object> getMulti(String[] keys, Integer[] hashCodes)
get(),
since itkeys - keys to retrievehashCodes - if not null, then the Integer array of hashCodespublic Map<String,Object> getMulti(String[] keys, 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 valpublic boolean flushAll()
public boolean flushAll(String[] servers)
servers - optional array of host(s) to flush (host:port)public Map<String,Map<String,String>> stats()
public Map<String,Map<String,String>> stats(String[] servers)
servers - string array of servers to retrieve stats from, or all if this
is nullpublic Map<String,Map<String,String>> statsItems()
public Map<String,Map<String,String>> statsItems(String[] servers)
servers - string array of servers to retrieve stats from, or all if this
is nullpublic Map<String,Map<String,String>> statsSlabs()
public Map<String,Map<String,String>> statsSlabs(String[] servers)
servers - string array of servers to retrieve stats from, or all if this
is nullpublic Map<String,Map<String,String>> statsCacheDump(int slabNumber, int limit)
slabNumber - the item number of the cache dumppublic Map<String,Map<String,String>> statsCacheDump(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 dumppublic boolean sync(String key)
public boolean syncAll()
public boolean syncAll(String[] servers)
Copyright © 2013 Schooner Information Technology. All Rights Reserved.