Memcached Java Client with sample program--reference

发布时间:2019-02-25 发布网站:脚本之家
脚本之家收集整理的这篇文章主要介绍了Memcached Java Client with sample program--reference脚本之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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.

There are three most widely used memcached client programs in java

  1. xmemcached
  2. spymemcached
  3. gwhalin memcached client

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;"&gt;/**</span><span style="color: #008000;"&gt;
 * MemcachedJavaClient program to show the usage of different functions
 * that can be performed on Memcached server with Java Client
 * </span><span style="color: #808080;"&gt;@param</span><span style="color: #008000;"&gt; args
 </span><span style="color: #008000;"&gt;*/</span>
<span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;static</span> <span style="color: #0000ff;"&gt;void</span><span style="color: #000000;"&gt; main(String[] args) {
    </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;initialize the SockIOPool that maintains the Memcached Server Connection Pool</span>
    String[] servers = {"localhost:11111"<span style="color: #000000;"&gt;};
    SockIOPool pool </span>= SockIOPool.getInstance("Test1"<span style="color: #000000;"&gt;);
    pool.setServers( servers );
    pool.setFailover( </span><span style="color: #0000ff;"&gt;true</span><span style="color: #000000;"&gt; );
    pool.setInitConn( </span>10<span style="color: #000000;"&gt; );
    pool.setMinConn( </span>5<span style="color: #000000;"&gt; );
    pool.setMaxConn( </span>250<span style="color: #000000;"&gt; );
    pool.setMaintSleep( </span>30<span style="color: #000000;"&gt; );
    pool.setNagle( </span><span style="color: #0000ff;"&gt;false</span><span style="color: #000000;"&gt; );
    pool.setSocketTO( </span>3000<span style="color: #000000;"&gt; );
    pool.setAliveCheck( </span><span style="color: #0000ff;"&gt;true</span><span style="color: #000000;"&gt; );
    pool.initialize();
    </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;Get the Memcached Client from SockIOPool named Test1</span>
    MemcachedClient mcc = <span style="color: #0000ff;"&gt;new</span> MemcachedClient("Test1"<span style="color: #000000;"&gt;);
    </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;add some value in cache</span>
    System.out.println("add status:"+mcc.add("1","Original"<span style="color: #000000;"&gt;));
    </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;Get value from cache</span>
    System.out.println("Get from Cache:"+mcc.get("1"<span style="color: #000000;"&gt;));

    System.out.println(</span>"add status:"+mcc.add("1","Modified"<span style="color: #000000;"&gt;));
    System.out.println(</span>"Get from Cache:"+mcc.get("1"<span style="color: #000000;"&gt;));

    </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;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;"&gt;));
    System.out.println(</span>"Get from Cache after set:"+mcc.get("1"<span style="color: #000000;"&gt;));

    </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;use delete function to delete key from cache</span>
    System.out.println("remove status:"+mcc.delete("1"<span style="color: #000000;"&gt;));
    System.out.println(</span>"Get from Cache after delete:"+mcc.get("1"<span style="color: #000000;"&gt;));

    </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;Use getMulti function to retrieve multiple keys values in one function
    </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; Its helpful in reducing network calls to 1</span>
    mcc.set("2","2"<span style="color: #000000;"&gt;);
    mcc.set(</span>"3","3"<span style="color: #000000;"&gt;);
    mcc.set(</span>"4","4"<span style="color: #000000;"&gt;);
    mcc.set(</span>"5","5"<span style="color: #000000;"&gt;);
    String [] keys </span>= {"1","2","3","INVALID","5"<span style="color: #000000;"&gt;};
    HashMap</span><String,Object> hm = (HashMap<String,Object><span style="color: #000000;"&gt;) mcc.getMulti(keys);

    </span><span style="color: #0000ff;"&gt;for</span><span style="color: #000000;"&gt;(String key : hm.keySet()){
        System.out.println(</span>"KEY:"+key+" VALUE:"+<span style="color: #000000;"&gt;hm.get(key));
    }
}

}

Output of the above program is:

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));

总结

以上是脚本之家为你收集整理的Memcached Java Client with sample program--reference全部内容,希望文章能够帮你解决Memcached Java Client with sample program--reference所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:76874919,请注明来意。
标签: