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

相关文章

前言设计一个缓存系统,不得不要考虑的问题就是:缓存穿透、...
在192.168.80.100要联网关闭防火墙及SElinuxsystemctlstopfi...
Redis与Memcache对比:1.Memcache是一个分布式的内存对象缓存...
安装Mencache:关闭防火墙及SElinuxsystemctl  stop  fi...
#安装php的yaf模块,参考https://www.cnblogs.com/shifu204/...
win10下安装配置apache、php、mysql、redis、memcache资源官...