Memcached 简明教程

Memcached - CAS Command

CAS 代表 Check-And-Set 或 Compare-And-Swap。Memcached CAS 命令用于在数据自上次获取以来未更新时设置数据。如果密钥不存在于 Memcached 中,则它返回 NOT_FOUND

CAS stands for Check-And-Set or Compare-And-Swap. Memcached CAS command is used to set the data if it is not updated since last fetch. If the key does not exist in Memcached, then it returns NOT_FOUND.

Syntax

Memcached CAS 命令的基本语法如下所示 −

The basic syntax of Memcached CAS command is as shown below −

set key flags exptime bytes unique_cas_key [noreply]
value

语法中的关键字的说明如下−

The keywords in the syntax are as described below−

  1. key − It is the name of the key by which data is stored and retrieved from Memcached.

  2. flags − It is the 32-bit unsigned integer that the server stores with the data provided by the user, and returns along with the data when the item is retrieved.

  3. exptime − It is the expiration time in seconds. 0 means no delay. If exptime is more than 30 days, Memcached uses it as a UNIX timestamp for expiration.

  4. bytes − It is the number of bytes in the data block that needs to be stored. This is the length of the data that needs to be stored in Memcached.

  5. unique_cas_key − It is the unique key get from gets command.

  6. noreply (optional) − It is a parameter that informs the server not to send any reply.

  7. value − It is the data that needs to be stored. Data needs to be passed on new line after executing the command with the above options.

Output

命令的输出如下所示:

The output of the command is as shown below −

STORED
  1. STORED indicates success.

  2. ERROR indicates error while saving data or wrong syntax.

  3. EXISTS indicates that someone has modified the CAS data since last fetch.

  4. NOT_FOUND indicates that the key does not exist in the Memcached server.

Example

要在 Memcached 中执行 CAS 命令,你需要从 Memcached gets 命令中获取 CAS 令牌。

To execute a CAS command in Memcached, you need to get a CAS token from the Memcached gets command.

cas tp 0 900 9
ERROR
cas tp 0 900 9 2
memcached
set tp 0 900 9
memcached
STORED
gets tp
VALUE tp 0 9 1
memcached
END
cas tp 0 900 5 2
redis
EXISTS
cas tp 0 900 5 1
redis
STORED
get tp
VALUE tp 0 5
redis
END

CAS Using Java Application

要从 Memcached 服务器中获取 CAS 数据,你需要使用 Memcached gets 方法。

To get CAS data from a Memcached server, you need to use Memcached gets method.

Example

import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
   public static void main(String[] args) {

     // Connecting to Memcached server on localhost
      MemcachedClient mcc = new MemcachedClient(new
      InetSocketAddress("127.0.0.1", 11211));
      System.out.println("Connection to server successful");
      System.out.println("set status:"+mcc.set("tutorialspoint", 900, "memcached").isDone());

      // Get cas token from cache
      long castToken = mcc.gets("tutorialspoint").cas;
      System.out.println("Cas token:"+castToken);

      // now set new data in memcached server
      System.out.println("Now set new data:"+mcc.cas("tutorialspoint",
      castToken, 900, "redis"));
      System.out.println("Get from Cache:"+mcc.get("tutorialspoint"));
   }
}

Output

编译并执行该程序后,您可以看到以下输出:

On compiling and executing the program, you get to see the following output −

Connection to server successful
set status:true
Cas token:3
Now set new data:OK
Get from Cache:redis