Memcached Java客户端示例

时间:2020-02-23 14:37:21  来源:igfitidea点击:

今天,我们将研究Memcached Java客户端示例。
前面我们了解了带有示例执行终端日志的用于memcached的telnet命令。
但是大多数时候,我们希望通过其他一些编程语言(例如Java和PHP)连接到Memcached服务器。

Memcached Java客户端

有三种使用最广泛的memcached Java客户端API。

  • xmemcached
  • spymemcached
  • gwhalin memcached客户端

Memcached Java示例

我使用了Greg Whalin Memcached客户端,发现它易于理解和使用。
它提供了线程池的所有基本功能。
下面是Maven依赖项,将其包括在您的项目中。

<dependency>
  <groupId>com.whalin</groupId>
  <artifactId>Memcached-Java-Client</artifactId>
  <version>3.0.2</version>
</dependency>

为了帮助您快速入门,我提供了一个示例程序来展示可以使用Memcached服务器执行的基本功能。

package com.theitroad.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));
		}
	}

}

上面的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

如果要连接到多个Memcached服务器,则必须创建多个SockIOPool实例,然后在获取MemcacheClient实例时使用相同的名称。