从memcache查询ID列表的值

时间:2016-08-17 19:01:25

标签: caching memcached

我正在阅读memcache文档,我确实看到了get方法,可用于获取给定键的值。

有没有办法在一次往返memcache服务器的往返中获取给定密钥集的值?

1 个答案:

答案 0 :(得分:2)

回答你的问题:是,有。不像Redis那样直接支持,但是是的。
您可能想知道的协议级支持是quiet mode,您可以参考here。我引用如下:

  

客户端应该实现多次获取(对于减少网络往返仍然很重要!)作为n个流水线请求,第一个n-1是getq / getkq,最后一个是常规get / getk。

根据SpyMemcached,您可以使用getBulkgetBulk的实施是火灾getq操作,然后执行一次noop操作。以下是代码段:

// set up the initial header stuff
ByteBuffer bb = ByteBuffer.allocate(size);
for (Map.Entry<Integer, byte[]> me : bkeys.entrySet()) {
  final byte[] keyBytes = me.getValue();
  final String key = keys.get(me.getKey());

  // Custom header
  bb.put(REQ_MAGIC);
  bb.put(CMD_GETQ);
  bb.putShort((short) keyBytes.length);
  bb.put((byte) 0); // extralen
  bb.put((byte) 0); // data type
  bb.putShort(vbmap.get(key).shortValue()); // vbucket
  bb.putInt(keyBytes.length);
  bb.putInt(me.getKey());
  bb.putLong(0); // cas
  // the actual key
  bb.put(keyBytes);
}
// Add the noop
bb.put(REQ_MAGIC);
bb.put((byte) NoopOperationImpl.CMD);
bb.putShort((short) 0);
bb.put((byte) 0); // extralen
bb.put((byte) 0); // data type
bb.putShort((short) 0); // reserved
bb.putInt(0);
bb.putInt(terminalOpaque);
bb.putLong(0); // cas

bb.flip();
setBuffer(bb);

threadpool无关,让我们说,我们在一个网络包中发送n个memcached包的数据,并在一个响应中获取所有数据。我跳过这个过程来处理多节点你可能不在乎。