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. eclipse安装CDT插件遇到的问题

    转自eclipse安装CDT插件遇到的问题 已经安装了集成java版本的eclipse,eclipse-java-indigo-SR1-win32.zip,在添加CDT插件时,遇到了问题. cdt-m ...

  2. FFMPEG之TimeBase成员理解

    http://blog.csdn.net/supermanwg/article/details/14521869

  3. VC提交网页表单(一共八篇)

    VC提交网页表单-自动评论留言(1)http://blog.csdn.net/wangningyu/article/details/4526357VC提交网页表单-自动评论留言(2)http://bl ...

  4. 【HDOJ】2149 Public Sale

    看Discuss说是博弈论,没学到这个分类.不过仔细想了想,发现.如果m<=n,那么可能结果为m,m+1...n.否则,如果m%(n+1) == 0,那么无论如何都会输,因为无论先报价什么数,如 ...

  5. What exactly is the difference between WndProc and DefaultWndProc?

    Control.DefWndProc Sends the specified message to the default window procedure. 参数说明:m:The Windows M ...

  6. 尚未在 Web 服务器上注册 ASP.NET 4.0” 的解决办法

    http://www.sowsoy.com/topics-537.html win7,vs2010创建.NetFramework 4框架下的Asp.Net空网站.系统提示 “尚未在 Web 服务器上注 ...

  7. android开发板

    element14-beaglebone-black http://www.embest-tech.cn/shop/star/element14-beaglebone-black-rev-c.html ...

  8. linux下包管理命令yum与apt-get以及开发环境配置

    一般来说市面上常见的Linux系统分为两大类: RH类:Redhat.centOS和Fedora等 Debian类:ubuntu.Debian等. 上述两类系统对应的包管理工具命令分别是yum和apt ...

  9. HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

    Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  10. HDU-1390 Binary Numbers

    http://acm.hdu.edu.cn/showproblem.php?pid=1390 Binary Numbers Time Limit: 2000/1000 MS (Java/Others) ...