Memcached 简明教程

Memcached - Replace Data

Memcached replace 命令用于替换现有键的值。如果键不存在,则会输出 NOT_STORED。

Memcached replace command is used to replace the value of an existing key. If the key does not exist, then it gives the output NOT_STORED.

Syntax

Memcached replace 的基本语法如下所示 −

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

replace key flags exptime bytes [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 the Memcached.

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

  6. value − It is the data that needs to be stored. The data needs to be passed on the 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. NOT_STORED indicates the data is not stored in Memcached.

Example

在以下示例中,我们使用“key”作为键,并以 900 秒的过期时间在其内存储 memcached。此后,将使用值“redis”替换相同的键。

In the following example, we use ‘key’ as the key and store memcached in it with an expiration time of 900 seconds. After this, the same key is replaced with the value ‘redis’.

add key 0 900 9
memcached
STORED
get key
VALUE key 0 9
memcached
END
replace key 0 900 5
redis
get key
VALUE key 0 5
redis
END

Replace Data Using Java Application

若要替换 Memcached 服务器中的数据,您需要使用 Memcached replace 方法。

To replace data in a Memcached server, you need to use the Memcached replace 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 sucessfully");
      System.out.println("set status:"+mcc.set("tutorialspoint", 900, "memcached").done);

      // Get value from cache
      System.out.println("Get from Cache:"+mcc.get("tutorialspoint"));

      // now replace the existing data
      System.out.println("Replace cache:"+mcc.replace("tutorialspoint", 900, "redis").done);

      // get the updated data
      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 successfully
set status:true
Get from Cache:memcached
Replace cache:true
Get from Cache:redis