Friday 2 January 2015

Using Memcached with commandline for caching in Java

Background

There are many caching frameworks in Java like EHCache and OSCache. These are essentially in-memory caches. Meaning it uses the same java process memory to cache data. You can imagine them as HashMap or HashTable in your own java program. This frameworks work just fine for caching data in size of few GBs. In this post we will see Memcached which is another caching framework that uses distributed architectural approach as a result of which large amount of data can be stored.



 Memcached Basics

As mentioned in background section memcached uses distributed architectural approach. It is essentially different process (server). Then you have memcached client (essentially jars) that you import in your java program which talks with memcached server. You can have multiple memcached servers . No server is aware of other servers. It is the client that is aware of which server has a particular key stored.

Starting/Stopping Memcached server

You can use following commands in Linux

  • Start Server :  /etc/init.d/memcached start
  • Stop Server : /etc/init.d/memcached stop
  • Restart Server : /etc/init.d/memcachedrestart
  • Check status : /etc/init.d/memcached status 
For windows simply do

  • memcached.exe start
  • memcached.exe stop .... etc
 In windows to install memcached as a service you can do -
  • memcached.exe -d install
Then you can start it from services panel -



To see list of all commands type
  • memcached.exe -help


Go ahead . Play around with the command line. Also if the default port i.e 11211 is used by some other process on your machine change the port of your memcached server.

Note : By default server will start up on port 11211 with 64 MB of memory.

Connecting to Memcached server

You can do telnet to your localhost and port to connect to memcached server. Eg.

C:\Users\athakur\Softwares\memcached>telnet localhost 11211

This will connect to your memcahcahed server. You can type "stats" to get the server statistics like PID of the process, uptime etc

You can type quit to exit from the telnet.



Using command line to get Memcahched details

Memcached puts your data into different "slabs" depending on the data size. This optimized memory allocation and reduces fragmentation. You can see these slabs with command -

  • stats slabs
Another useful command is to find slabs with some data in it. For that you can use
  • stats items
After getting slabs you can get the keys in the slab with following command -
  • stats cachedump [slabID] [number of items, 0 for all items]
For Eg. you can do

stats cachedump 2 0
ITEM TEST_DATA_KEY [16 b; 1420207128 s]
END


This will list down all data items in slab 2. Text following the ITEM keyword is the key of the data. To get the value of the key you can simply do

  • get keyName
Eg. Yo get value for key  TEST_DATA_KEY you have to do

get TEST_DATA_KEY
VALUE TEST_DATA_VALUE 0 68
test_value

END

To delete data by it's key you can simply do
  • delete keyName
 For eg. to delete above data you have to do

delete TEST_DATA_KEY
 DELETED
get
TEST_DATA_KEY
 END

Note the subsequent get call gives nothing as the data is deleted.

You can clear the entire cache with following command
  • flush_all 

Eg.
telnet localhost 11211
flush_all
quit 

Related Links

t> UA-39527780-1 back to top