Outline
Install/Command
Linux
Billing Team Manual
memcached FAQ
install
install libevent
libevent Download
libevent-1.4.12.tar.gz 2009-07-24
$ tar -zxvf libevent-1.x.x.tar.gz
$ ./configure --prefix=/usr/
$ make
$ make test
$ make install
install memcached
memcached Download
memcached-1.4.0.tar.gz - July 9th, 2009
$ tar -zxvf memcached-1.x.x.tar.gz
$ ./configure --prefix=/usr/local/memcached --with-libevent=/usr/ --enable-threads (you probably want threads)
$ make
$ make test
$ sudo make install
Commands
Start/Stop
text protocol
//start
cd /usr/local/memcached/bin
./memcached -d -m 100 -u root -p 11211 -c 256 -P /tmp/memcached.pid
-m memory allocated for memcached server, unit is MB
-u the user run memcached.
-l listen host ip address.
-p listen port number.
-c concurrent thread number.
-P the file saving memcached pid.
//monitor
telnet 172.16.100.117 11211
//shutdown
kill `cat /tmp/memcached.pid`
Storage
$ set key001 0 0 5
$ hello
$ STORED
$ get key001
$ VALUE key001 0 5
$ hello
$ END
$ delete key001
$ DELETED
$ get key001
$ END
$ set key001 0 0 5
$ hello
$ STORED
$ flush_all
$ OK
$ get key001
$ END
- set <key> <flags> <exptime> <bytes>
- flush_all : Its effect is to invalidate all existing items immediately (by default) or after the expiration specified.
Windows
- Download : version: 1.2.1 - Dec 23, 2006
- unzip : Unzip the binaries in your desired directory (eg. c:/memcached)
- setup : c:/memcached/memcached.exe -d install
- start : c:/memcached/memcached.exe -d start
Client Usage/API
Java - spymemcached
spymemcached
spymemcached javadoc
//connect
MemcachedClient c=new MemcachedClient(
new InetSocketAddress("hostname", portNum));
// Store a value (async) for one hour
c.set("someKey", 3600, someObject);
// Retrieve a value (synchronously).
Object myObject=c.get("someKey");
// Try to get a value, for up to 5 seconds, and cancel if it doesn't return
Object myObj=null;
Future<Object> f=c.asyncGet("someKey");
try {
myObj=f.get(5, TimeUnit.SECONDS);
} catch(TimeoutException e) {
// Since we don't need this, go ahead and cancel the operation. This
// is not strictly necessary, but it'll save some work on the server.
f.cancel(false);
// Do other timeout related stuff
}
Resources
Inside into memcached, Chinese