Testing – Distributed Memcache using SpyMemcached In Java

Datetime:2016-08-23 01:27:35          Topic: Test Engineer           Share

1.What is Memcached ?

2.How to perform basic operations on Memcached like insert, retrieve and delete using Java client SpyMemcached.

3.Sample Java client to perform the operations on Memcached.

1.What is Memcached?

Memcached is an open source, high performance, distributed memory object caching system. intended to speed up dynamic web applications by reducing the database load. It is a key-value dictionary of strings, objects, etc, stored in the memory, resulting from database calls, API calls, or page rendering.

2.How to insert data into Memcached?

Insert a data/record into the Memcached using below method

memcachedClient.set(String key, String value);

2.1 How to retrieve data from Memcached ?

Retrieve a data/record from the Memcached using below method

getContentFromMemcache(String key);

2.2 How to delete data from Memcached ?

Delete a data/record from Memcached

memcachedClient.delete(String key);

3 Maven dependency :

<dependency>

<groupId>net.spy</groupId>

<artifactId>spymemcached</artifactId>

</dependency>

Below is the sample Java code to test one Memcached

package <memcache_path>; // example - com.test.memcache_test
importjava.io.IOException;
importjava.net.InetSocketAddress;
importjunit.runner.Version;
importnet.spy.memcached.AddrUtil;
importnet.spy.memcached.MemcachedClient;
importorg.springframework.stereotype.Component;
 
@Component
public class MemcacheTest {
private String memcachedHost = "<memcache endpoint>";
private int memcachedPort = <memcachePort>; //by default - 11211
private MemcachedClientmemcachedClient;
 
MemcacheTest() throws IOException {
memcachedClient = new MemcachedClient(new InetSocketAddress(
memcachedHost, memcachedPort));
}
 
public void putContentInMemCache(String key, String value) {
memcachedClient.set(key, 3600, value); // (3600 - expiry time in seconds)
}
 
public Object getContentInMemCache(String key) {
return memcachedClient.get(key);
}
 
public void deleteContentFromMemCache(String key) {
memcachedClient.delete(key);
}
 
public static void main(String[] args) throws IOException {
MemcacheTesttest = new MemcacheTest();
test.putContentInMemCache("key", "abc");
System.out.println(test.getContentInMemCache("key"));
test.deleteContentFromMemCache("key");
System.out.println(test.getContentInMemCache("key"));
/* To insert multiple records into memcache
for (int i = 0; i< 10; i++) {
test.putContentInMemCache("key" + "_" + i , "value" + "_" + i);
}
To retrieve multiple records from memcache
for (int i = 0; i < 10; i++) {
System.out.println("key _" +i + " - " + test.getContentInMemCache("key" + "_" + i));
} */
}

Output -1 :

abc

null

Output -2 : for loop

key _0 – value_0

key _1 – value_1

key _2 – value_2

key _3 – value_3

key _4 – value_4

key _5 – value_5

key _6 – value_6

key _7 – value_7

key _8 – value_8

key _9 – value_9

Ref : Memcached.org





About List