Memcached 简明教程

Memcached - Clear Data

Memcached flush_all 命令用于从 Memcached 服务器删除所有数据(键值对)。它接受一个名为 time 的可选参数,该参数设置一个时间,在此时间之后将清除 Memcached 数据。

Syntax

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

flush_all [time] [noreply]

以上命令始终返回 OK。

Example

在下面的示例中,我们将一些数据存储到 Memcached 服务器,然后清除所有数据。

set tutorialspoint 0 900 9
memcached
STORED
get tutorialspoint
VALUE tutorialspoint 0 9
memcached
END
flush_all
OK
get tutorialspoint
END

Clear Data Using Java Application

要从 Memcached 服务器清除数据,你需要使用 Memcached flush 方法。

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("count", 900, "5").isDone());

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

      // now increase the stored value
      System.out.println("Increment value:"+mcc.incr("count", 2));

      // now decrease the stored value
      System.out.println("Decrement value:"+mcc.decr("count", 1));

      // now get the final stored value
      System.out.println("Get from Cache:"+mcc.get("count"));

      // now clear all this data
      System.out.println("Clear data:"+mcc.flush().isDone());
   }
}

Output

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

Connection to server successfully
set status:true
Get from Cache:5
Increment value:7
Decrement value:6
Get from Cache:6
Clear data:true