Memcached Java Client with sample program--reference
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
- xmemcached
- spymemcached
- 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的更多相关文章
- Memcached Java Client API详解
针对Memcached官方网站提供的java_memcached-release_2.0.1版本进行阅读分析,Memcached Java客户端lib库主要提供的调用类是SockIOPool和MemC ...
- Memcached Java Client比较
JAVA客户端调用memcached比较 Memcached 客户端程序三种API的比较 Java开发中的Memcache原理及实现(五)Memcached客户端
- memcached Java Client
下载: Step1: Step2 Step3: Step4:
- Memcached学习笔记 — 第四部分:Memcached Java 客户端-gwhalin(1)-介绍及使用
介绍 Memcached java client是官方推荐的最早的memcached java客户端.最新版本:java_memcached-release_2.6.1. 官方下载地址:http ...
- 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 ...
- ES系列十五、ES常用Java Client API
一.简介 1.先看ES的架构图 二.ES支持的客户端连接方式 1.REST API http请求,例如,浏览器请求get方法:利用Postman等工具发起REST请求:java 发起httpClien ...
- 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 ...
- 【Tech】CAS多机部署Server和Java Client端
昨天尝试把cas的java client端部署到另外一台机器,结果就有问题了.(localhost部署cas server和java client端参见:http://www.cnblogs.com/ ...
- [Kerberos] Java client访问kerberos-secured cluster
使用java client访问kerberos-secured cluster,最重要的是先从admin那里拿到可用的keytab文件,用来作认证.接下来就是调整连接的配置.以下先用连接hdfs为例进 ...
随机推荐
- JVM 学习笔记(二)
JVM 堆中几乎存放着java中所有的对象实例,在在垃圾回收前先要判断对象是否已死,这里对对象的判断主要有: 1. 引用计数法 给对象中添加一个引用计数器,每当有一个地方引用他时,计数器就加1:当引 ...
- 10款无限滚动自动翻页jquery插件
2012年3月29日 无限滚动自动翻页可以说是web2.0时代的一项堪称伟大的技术,它让我们在浏览页面的时候只需要把滚动条拉到网页底部就能自动显示下一页的 结果,改变了一直以来只能通过点击下一页来翻页 ...
- [原博客] POJ 2425 A Chess Game
题目链接题意:给定一个有向无环图(DAG),上面放有一些旗子,旗子可以重合,两个人轮流操作,每次可以把一个旗子从一个位置移动到相邻的位置,无法移动时输,询问先手是否必胜. 这道题可以把每个旗子看作单独 ...
- ReactEurope Conf 参会感想
React 带来的革命性创新是前端世界过去几年最激动人心的变化.自从接触 React 以来,我深信 React 会改变客户端开发者(包括前端.iOS 和 Android)的开发体验.这次在巴黎举办的 ...
- backbone csdn
http://blog.csdn.net/the_fire/article/details/7444067 blog.csdn.net/the_fire/article/details/7445448 ...
- linux使用su切换用户提示 Authentication failure的解决方法& 复制文件时,报cp: omitting directory `XXX'
linux使用su切换用户提示 Authentication failure的解决方法:这个问题产生的原因是由于ubtun系统默认是没有激活root用户的,需要我们手工进行操作,在命令行界面下,或者在 ...
- 【转】eclipse android 设置及修改生成apk的签名文件 -- custom debug keystore
原文网址:http://hold-on.iteye.com/blog/2064642 android eclipse 设置及修改生成apk的签名文件 1. 问题: 平时在使用eclipse进行andr ...
- Visual Studio 2015 企业版 官方中文版.iso
Visual Studio 2015 企业版 官方中文版.iso 附上链接 不要积分 谢谢: http://download.csdn.net/detail/onebelowzero2012/9561 ...
- Devexpress 之gridControl双击行事件
MouseDown事件 protected internal void gridControl1_MouseDown(object sender, MouseEventArgs e) { DevExp ...
- $(function(){})里面不能声明定义函数
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...