memcached客户端
Today we will look into Memcached Java client example. Earlier we learned about telnet commands for memcached with sample execution terminal logs. But most of the times we want to connect to the Memcached server through some other programming languages, such as Java and PHP.
今天,我们将研究Memcached Java客户端示例。 之前我们了解了带有示例执行终端日志的用于memcached的telnet命令 。 但是大多数时候,我们希望通过其他一些编程语言(例如Java和PHP)连接到Memcached服务器。
Memcached Java客户端 (Memcached Java Clients)
There are three most widely used memcached Java client API.
有三种使用最广泛的内存缓存Java客户端API。
- xmemcached xmemcached
- spymemcached spymemcached
- gwhalin memcached client gwhalin memcached客户端
Memcached Java示例 (Memcached Java Example)
I have used Greg Whalin Memcached client and found it easy to understand and use. It provides all the basic functionalities with thread pooling. Below is the maven dependency to include this into your project.
我使用了Greg Whalin Memcached客户端,发现它易于理解和使用。 它提供了线程池的所有基本功能。 下面是Maven依赖项,将其包括在您的项目中。
<dependency>
<groupId>com.whalin</groupId>
<artifactId>Memcached-Java-Client</artifactId>
<version>3.0.2</version>
</dependency>
To help you get started quickly, I am providing a sample program to showcase the usage of basic functions that can be performed with the Memcached server.
为了帮助您快速入门,我提供了一个示例程序来展示可以使用Memcached服务器执行的基本功能。
package com.journaldev.memcached.test;
import java.util.HashMap;
import com.whalin.MemCached.MemCachedClient;
import com.whalin.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 the 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));
}
}
}
The output of the above Memcache java client program is:
上面的Memcache Java客户端程序的输出为:
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.
如果要连接到多个Memcached服务器,则必须创建多个SockIOPool
实例,然后在获取MemcacheClient
实例时使用相同的名称。
翻译自: https://www.journaldev.com/24/memcached-java-client-example
memcached客户端