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. delphi xe5 android 开发数据访问server端(一)

    第一篇我们破解并安装了xe5 第二篇我们搭建了开发环境 接下来我们开发一个三层的android程序 建立一个webservices  stand-alone vcl application 作为手机访 ...

  2. 50个实用的jQuery代码段让你成为更好的Web前端工程师

    本文会给你们展示50个jquery代码片段,这些代码能够给你的javascript项目提供帮助.其中的一些代码段是从jQuery1.4.2才开始支持的做法,另一些则是真正有用的函数或方法,他们能够帮助 ...

  3. keychain 多应用共享数据

    地址:http://blog.csdn.net/jerryvon/article/details/16843065 补充: 若plist跟项目不在同一级目录下,可通过XXX/xxx.plist的方式设 ...

  4. 教你如何监控 Apache?

    什么是 Apache? Apache 是一款 HTTP 服务器软件,现在更名为 "http",而 Apache 则成了一个(包含httpd的项目)巨大的基金组织,根据习惯后文都用 ...

  5. PYTHON---FILE IO

    import pickle shoplistfile = 'shoplist.data' shoplist = ['apple', 'mango', 'carrot'] f = open(shopli ...

  6. Eclipse下设置github开发环境

    1.按照github上的指南配置(http://help.github.com/win-set-up-git/)基础的git环境. 2.在github上创建一个Repository. 3.在Eclip ...

  7. [置顶] linux内核启动1-启动参数(启动参数的获取和处理,分析setup_arch)

    最近公司要求调试一个内核,启动时有问题,所以就花了一点时间看看内核启动. 看的过程中总结了一点东西,希望可以帮助大家调试内核. 当我开始看的时候,第一件事是从网上搜集资料,不看不知道,一看吓一跳!牛人 ...

  8. Https 原理

    HTTPS其实是有两部分组成:HTTP + SSL / TLS, 也就是在HTTP上又加了一层处理加密信息的模块.服务端和客户端的信息传输都会通过TLS进行加密,所以传输的数据都是加密后的数据 1. ...

  9. 远程ubuntu虚拟机Tensorflow搭建 - 1 SSH连接

    感谢英才计划,我们每个人收获了一台清华的虚拟机. 4 core CPU 16GB Memory 80GB Disk 配置不错了... 用ssh密钥登录.赠送hadoop-key.pem一把. 先用su ...

  10. ImageSwitcher 右向左滑动的实现方式

    ImageSwitcher is;...is.setInAnimation(this, android.R.anim.slide_in_left);is.setOutAnimation(this, a ...