In my previous post, I listed down most common telnet commands for memcached 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:

https://github.com/gwhalin/Memcached-Java-Client

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.

package com.journaldev.memcached.test;

import java.util.HashMap;

import com.meetup.memcached.MemcachedClient;
import com.meetup.memcached.SockIOPool; public class MemcachedJavaClient { /**
* MemcachedJavaClient program to show the usage of different functions
* that can be performed on Memcached server with Java Client
* @param args
*/
public static void main(String[] args) {
//initialize the SockIOPool that maintains the Memcached Server Connection Pool
String[] servers = {"localhost:11111"};
SockIOPool pool = SockIOPool.getInstance("Test1");
pool.setServers( servers );
pool.setFailover( true );
pool.setInitConn( 10 );
pool.setMinConn( 5 );
pool.setMaxConn( 250 );
pool.setMaintSleep( 30 );
pool.setNagle( false );
pool.setSocketTO( 3000 );
pool.setAliveCheck( true );
pool.initialize();
//Get the Memcached Client from SockIOPool named Test1
MemcachedClient mcc = new MemcachedClient("Test1");
//add some value in cache
System.out.println("add status:"+mcc.add("1", "Original"));
//Get value from cache
System.out.println("Get from Cache:"+mcc.get("1")); System.out.println("add status:"+mcc.add("1", "Modified"));
System.out.println("Get from Cache:"+mcc.get("1")); //use set function to add/update value, use replace to update and not add
System.out.println("set status:"+mcc.set("1","Modified"));
System.out.println("Get from Cache after set:"+mcc.get("1")); //use delete function to delete key from cache
System.out.println("remove status:"+mcc.delete("1"));
System.out.println("Get from Cache after delete:"+mcc.get("1")); //Use getMulti function to retrieve multiple keys values in one function
// Its helpful in reducing network calls to 1
mcc.set("2", "2");
mcc.set("3", "3");
mcc.set("4", "4");
mcc.set("5", "5");
String [] keys = {"1", "2","3","INVALID","5"};
HashMap<String,Object> hm = (HashMap<String, Object>) mcc.getMulti(keys); for(String key : hm.keySet()){
System.out.println("KEY:"+key+" VALUE:"+hm.get(key));
}
} }

Output of the above program is:

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=new XMemcachedClientBuilder(
AddrUtil.getAddresses("127.0.0.1:11211"));
MemcachedClient memCacheClient=builder.build(); ------------------------------------------------------------
MemcachedClient memcacheClient=new MemcachedClient(
new InetSocketAddress("127.0.0.1:11211", 11211));

Memcached Java Client with sample program--reference的更多相关文章

  1. Memcached Java Client API详解

    针对Memcached官方网站提供的java_memcached-release_2.0.1版本进行阅读分析,Memcached Java客户端lib库主要提供的调用类是SockIOPool和MemC ...

  2. Memcached Java Client比较

    JAVA客户端调用memcached比较 Memcached 客户端程序三种API的比较 Java开发中的Memcache原理及实现(五)Memcached客户端

  3. memcached Java Client

    下载: Step1: Step2 Step3: Step4:

  4. Memcached学习笔记 — 第四部分:Memcached Java 客户端-gwhalin(1)-介绍及使用

     介绍 Memcached java client是官方推荐的最早的memcached java客户端.最新版本:java_memcached-release_2.6.1. 官方下载地址:http ...

  5. elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)

    一.ES Client 简介 1. ES是一个服务,采用C/S结构 2. 回顾 ES的架构 3. ES支持的客户端连接方式 3.1 REST API ,端口 9200 这种连接方式对应于架构图中的RE ...

  6. ES系列十五、ES常用Java Client API

    一.简介 1.先看ES的架构图 二.ES支持的客户端连接方式 1.REST API http请求,例如,浏览器请求get方法:利用Postman等工具发起REST请求:java 发起httpClien ...

  7. Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)

    elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介.Java REST Client.Java Client.Spri ...

  8. 【Tech】CAS多机部署Server和Java Client端

    昨天尝试把cas的java client端部署到另外一台机器,结果就有问题了.(localhost部署cas server和java client端参见:http://www.cnblogs.com/ ...

  9. [Kerberos] Java client访问kerberos-secured cluster

    使用java client访问kerberos-secured cluster,最重要的是先从admin那里拿到可用的keytab文件,用来作认证.接下来就是调整连接的配置.以下先用连接hdfs为例进 ...

随机推荐

  1. 李洪强漫谈iOS开发[C语言-032]-三目运算符

  2. RTP

    RTP学习(三)RTP/RTCP/RTSP数据包格式 h264RTP打包描述的较为详细(含SDP中sps等信息的描述) UDP.TCP.RTP三种协议的总结 http://super-and-star ...

  3. [收藏转贴]构建RESTful风格的WCF服务

    RESTful Wcf是一种基于Http协议的服务架构风格. 相较 WCF.WebService 使用 SOAP.WSDL.WS-* 而言,几乎所有的语言和网络平台都支持 HTTP 请求. RESTf ...

  4. iOS -view横向变成竖向

    -------

  5. 转载:win7JDK环境配置

    [win7JDK环境配置] 网址:http://blog.sina.com.cn/s/blog_6a9df2330100ms9q.html 系统变量下: (1) 新建->变量名:JAVA_HOM ...

  6. 【HDOJ】3033 I love sneakers!

    分组背包. #include <stdio.h> #include <string.h> #define mymax(a, b) (a>b) ? a:b typedef ...

  7. Timer计时不准确的解决方案 每次都重新调整,修正误差

    http://stackoverflow.com/questions/29722838/system-timers-timer-steadily-increasing-the-interval 需要在 ...

  8. USACO3.31Riding the Fences(输出欧拉路径)

    都忘了欧拉路径是什么了.. 用dfs搜 标记边  刚开始直接从I-N搜 直接超时 2了 先找符合起点和终点的点搜 即度数是奇数 d单dfs也超了 后来换了个姿势.. /* ID: shangca2 L ...

  9. BZOJ1935: [Shoi2007]Tree 园丁的烦恼

    1935: [Shoi2007]Tree 园丁的烦恼 Time Limit: 15 Sec  Memory Limit: 357 MBSubmit: 552  Solved: 220[Submit][ ...

  10. B. Berland Bingo

    Lately, a national version of a bingo game has become very popular in Berland. There are n players p ...