In my previous post,I listed down most common??with sample execution terminal logs. Today I want to discuss about the Memcached Client program available in Java language.
I have used Greg Whalin memcached client and found it easy to understand and use. It provides all the basic functionalities with thread pooling. Its available under BSD license and you can download it from below URL:
Once you have downloaded the source code you can create a java project and copy all the java classes and then use it.
To help you get started quickly,I am providing a sample program to showcase the usage of basic functions that can be performed with memcached server.
<span style="color: #0000ff;">import<span style="color: #000000;"> java.util.HashMap;
<span style="color: #0000ff;">import<span style="color: #000000;"> com.meetup.memcached.MemcachedClient;
<span style="color: #0000ff;">import<span style="color: #000000;"> com.meetup.memcached.SockIOPool;
<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> MemcachedJavaClient {
</span><span style="color: #008000;">/**</span><span style="color: #008000;">
* MemcachedJavaClient program to show the usage of different functions
* that can be performed on Memcached server with Java Client
* </span><span style="color: #808080;">@param</span><span style="color: #008000;"> args
</span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) {
</span><span style="color: #008000;">//</span><span style="color: #008000;">initialize the SockIOPool that maintains the Memcached Server Connection Pool</span>
String[] servers = {"localhost:22222"<span style="color: #000000;">};
SockIOPool pool </span>= SockIOPool.getInstance("Test1"<span style="color: #000000;">);
pool.setServers( servers );
pool.setFailover( </span><span style="color: #0000ff;">true</span><span style="color: #000000;"> );
pool.setInitConn( </span>10<span style="color: #000000;"> );
pool.setMinConn( </span>5<span style="color: #000000;"> );
pool.setMaxConn( </span>250<span style="color: #000000;"> );
pool.setMaintSleep( </span>30<span style="color: #000000;"> );
pool.setNagle( </span><span style="color: #0000ff;">false</span><span style="color: #000000;"> );
pool.setSocketTO( </span>3000<span style="color: #000000;"> );
pool.setAliveCheck( </span><span style="color: #0000ff;">true</span><span style="color: #000000;"> );
pool.initialize();
</span><span style="color: #008000;">//</span><span style="color: #008000;">Get the Memcached Client from SockIOPool named Test1</span>
MemcachedClient mcc = <span style="color: #0000ff;">new</span> MemcachedClient("Test1"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">add some value in cache</span>
System.out.println("add status:"+mcc.add("1","Original"<span style="color: #000000;">));
</span><span style="color: #008000;">//</span><span style="color: #008000;">Get value from cache</span>
System.out.println("Get from Cache:"+mcc.get("1"<span style="color: #000000;">));
System.out.println(</span>"add status:"+mcc.add("1","Modified"<span style="color: #000000;">));
System.out.println(</span>"Get from Cache:"+mcc.get("1"<span style="color: #000000;">));
</span><span style="color: #008000;">//</span><span style="color: #008000;">use set function to add/update value,use replace to update and not add</span>
System.out.println("set status:"+mcc.set("1","Modified"<span style="color: #000000;">));
System.out.println(</span>"Get from Cache after set:"+mcc.get("1"<span style="color: #000000;">));
</span><span style="color: #008000;">//</span><span style="color: #008000;">use delete function to delete key from cache</span>
System.out.println("remove status:"+mcc.delete("1"<span style="color: #000000;">));
System.out.println(</span>"Get from Cache after delete:"+mcc.get("1"<span style="color: #000000;">));
</span><span style="color: #008000;">//</span><span style="color: #008000;">Use getMulti function to retrieve multiple keys values in one function
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Its helpful in reducing network calls to 1</span>
mcc.set("2","2"<span style="color: #000000;">);
mcc.set(</span>"3","3"<span style="color: #000000;">);
mcc.set(</span>"4","4"<span style="color: #000000;">);
mcc.set(</span>"5","5"<span style="color: #000000;">);
String [] keys </span>= {"1","2","3","INVALID","5"<span style="color: #000000;">};
HashMap</span><String,Object> hm = (HashMap<String,Object><span style="color: #000000;">) mcc.getMulti(keys);
</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(String key : hm.keySet()){
System.out.println(</span>"KEY:"+key+" VALUE:"+<span style="color: #000000;">hm.get(key));
}
}
}
If you want to connect to multiple memcached servers then you will have to create multiple SockIOPool instances and then use the same name while getting the MemcacheClient instance.?
reference:http://www.journaldev.com/24/memcached-java-client-with-sample-program
补充:memcachedclient获取方式有3中,上面是一种:
MemcachedClientBuilder builder="127.0.0.1:11211"=------------------------------------------------------------<span style="color: #000000;">
MemcachedClient memcacheClient=<span style="color: #0000ff;">new<span style="color: #000000;"> MemcachedClient(
<span style="color: #0000ff;">new InetSocketAddress("127.0.0.1:11211",11211));
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!