对memcache的数据(key-value)进行遍历操作

本文介绍了使用PHP、Java及Ruby等不同编程语言实现Memcached客户端的方法,包括连接配置、获取键值等操作,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. PHP实现:

参考资料:http://www.cnblogs.com/sunli/archive/2008/11/01/1324153.html

$port = 11211;
$host = '192.168.11.128';
$mem  = new Memcache();
$mem->connect($host, $port);
$items = $mem->getExtendedStats('items');
$items = $items["$host:$port"]['items'];
foreach($items as $key => $values){
	$number = $key;
	$str = $mem->getExtendedStats ("cachedump",$number,0);
	$line = $str["$host:$port"];
	if(is_array($line) && count($line) > 0){
		foreach($line as $key => $value) {
			echo $key . '=>';
			print_r($mem->get($key));
			echo "\r\n";
		}
	}
}

2. Java实现:

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeoutException;

import net.rubyeye.xmemcached.KeyIterator;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.XMemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;

public class XMemcache {

	public static void main(String[] args) throws IOException, TimeoutException, InterruptedException, MemcachedException {

		InetAddress addr = InetAddress.getByName("192.168.11.128");
		InetSocketAddress ia = new InetSocketAddress(addr, 11211);
		MemcachedClient client = new XMemcachedClient(ia);
		
		KeyIterator it = client.getKeyIterator(ia);
		while(it.hasNext()) {
			String key = it.next();
			System.out.println(client.get(key));
		}
		
		client.shutdown();
	}
}

3. Ruby实现:

http://robbin.iteye.com/blog/252345

4. telnet:

http://iamcaihuafeng.blog.sohu.com/159082880.html

特别提醒:http://fnil.net/docs/xmemcached/net/rubyeye/xmemcached/class-use/KeyIterator.html

memcached 1.6.x will remove cachedump stats command,so this method will be removed in the future


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值