微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;@p<a href="/tag/ara/" target="_blank" class="keywords">ara</a>m</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.setFai<a href="/tag/lov/" target="_blank" class="keywords">lov</a>er( </span><span style="color: #0000ff;"&gt;true</span><span style="color: #000000;"&gt; );
    pool.se<a href="/tag/tini/" target="_blank" class="keywords">tini</a>tConn( </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>
    Sy<a href="/tag/stem/" target="_blank" class="keywords">stem</a>.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>
    Sy<a href="/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("Get from Cache:"+mcc.get("1"<span style="color: #000000;"&gt;));

    Sy<a href="/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(</span>"add status:"+mcc.add("1","Modified"<span style="color: #000000;"&gt;));
    Sy<a href="/tag/stem/" target="_blank" class="keywords">stem</a>.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>
    Sy<a href="/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("set status:"+mcc.set("1","Modified"<span style="color: #000000;"&gt;));
    Sy<a href="/tag/stem/" target="_blank" class="keywords">stem</a>.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>
    Sy<a href="/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("remove status:"+mcc.delete("1"<span style="color: #000000;"&gt;));
    Sy<a href="/tag/stem/" target="_blank" class="keywords">stem</a>.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()){
        Sy<a href="/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(</span>"KEY:"+key+" VALUE:"+<span style="color: #000000;"&gt;hm.get(key));
    }
}

}

Output of the above program is:

rush: bash; title: ; notranslate">add status:true
Get from Cache:Original
add status:false
Get from Cache:Original
set status:true
Get from Cache after set:Modified
remove status:true
Get from Cache after delete:null
KEY:3 VALUE:3
KEY:2 VALUE:2
KEY:1 VALUE:null
KEY:INVALID VALUE:null
KEY:5 VALUE:5

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐