anga.com/memcached/.
Supports setting, adding, replacing, deleting compressed/uncompressed and
serialized (can be stored as string if object is native class) objects to memcached.
Now pulls SockIO objects from SockIOPool, which is a connection pool. The server failover
has also been moved into the SockIOPool class.
This pool needs to be initialized prior to the client working. See javadocs from SockIOPool.
Some examples of use follow.
To create cache client object and set params:
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);
To store an object:
MemcachedClient mc = new MemcachedClient(); String key = "cacheKey1"; Object value = SomeClass.getObject(); mc.set(key, value);
To store an object using a custom server hashCode:
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.
The add and replace methods do the same, but with a slight difference.
- add -- will store the object only if the server does not have an entry for this key
- replace -- will store the object only if the server already has an entry for this key
To delete a cache entry:
MemcachedClient mc = new MemcachedClient(); String key = "cacheKey1"; mc.delete(key);
To delete a cache entry using a custom hash code:
MemcachedClient mc = new MemcachedClient(); String key = "cacheKey1"; Integer hash = new Integer(45); mc.delete(key, hashCode);
To store a counter and then increment or decrement that counter:
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));
To store a counter and then increment or decrement that counter with custom hash:
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));
To retrieve an object from the cache:
MemcachedClient mc = new MemcachedClient(); String key = "key"; Object value = mc.get(key);
To retrieve an object from the cache with custom hash:
MemcachedClient mc = new MemcachedClient(); String key = "key"; Integer hash = new Integer(45); Object value = mc.get(key, hash);
To retrieve an multiple objects from the cache
MemcachedClient mc = new MemcachedClient(); String[] keys = { "key", "key1", "key2" }; Map<Object> values = mc.getMulti(keys);
To retrieve an multiple objects from the cache with custom hashing
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);
To flush all items in server(s)
MemcachedClient mc = new MemcachedClient(); mc.flushAll();
To get stats from server(s)
MemcachedClient mc = new MemcachedClient(); Map stats = mc.stats();
@author greg whalin
@author Richard 'toast' Russo
@author Kevin Burton
@author Robert Watts
@author Vin Chawla
@version 1.5