从内存缓存中查询 ID 列表的值

Query value for list of Ids from memcache

我正在浏览内存缓存文档,我确实看到了 get 可用于获取给定键的值的方法。

有没有办法在到内存缓存服务器的单次往返中获取给定键集的值?

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

Clients should implement multi-get (still important for reducing network roundtrips!) as n pipelined requests, the first n-1 being getq/getkq, the last being a regular get/getk.

根据SpyMemcached,您可以使用getBulkgetBulk 的执行是 fire n 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包的数据并在一个响应中获取所有数据。我跳过处理多节点的过程,因为你可能不关心。