Memcached教程

Memcached 预装数据

Memcached预装命令类似于添加命令,该命令用于在现有键中添加一些数据。但是,append命令在现有数据之后添加新数据,而prepend命令在现有数据之前添加新数据。

语法

prepend key flags exptime bytes [noreply]
value
在这里
key: 这是存储和从memcached检索数据的密钥。
flags: 标志是服务器与数据一起存储的32位无符号整数(由用户提供),并在检索到项目时随数据一起返回。
exptime: exptime是以秒为单位的到期时间。 0表示没有延迟。如果超过30天,则memcached会将其用作UNIX时间戳记过期。
bytes: bytes是数据块中需要存储的字节数。这是存储在memcached中的数据的长度。
noreply: 。这是一个可选参数。用来通知服务器不要发送任何答复。
value: 值是必须存储的数据。使用上述选项执行命令后,需要在新行中传递数据。

返回值

此命令将返回以下值:
STORED: 存储意味着成功 NOT_STORED: : NOT_STORED表示数据未存储在内存缓存中。 CLIENT_ERROR: 表示错误。

Ubuntu中的示例

让我们看一下prepend命令的示例。在这里,我们尝试添加一些不存在的数据。因此,它返回NOT_STORED。之后,我们设置一个密钥并将数据附加到其中。
prepend town 0 900 5
delhi
NOT_STORED
set town 0 900 9
bangalore
STORED
get town
VALUE town 0 14
bangalore
END
prepend town 0 900 5
delhi
STORED
get town
VALUE town 0 14
delhibangalore
END
如何在Memcached中添加数据

Windows中的示例

prepend town 0 900 5
delhi
NOT_STORED
set town 0 900 9
bangalore
STORED
get town
VALUE town 0 14
bangalore
END
append town 0 900 5
delhi
STORED
get town
VALUE town 0 14
delhibangalore
END
如何在Memcached中添加数据

使用Java添加数据应用程序

考虑到内存缓存服务器正在主机127.0.0.1和端口11211上运行。在这里,我们将使用prepend()方法在内存缓存服务器中添加数据。

示例:

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("town", 900, "bangalore").isDone());
      
      // Get value from cache
      System.out.println("Get from Cache:"+mcc.get("town"));
      
      // now append some data into existing key
      System.out.println("Prepend to cache:"+mcc.prepend("town", "delhi").isDone());
      
      // get the updated key
      System.out.println("Get from Cache:"+mcc.get("town"));
   }
}
输出:
Connection to server successfully
set status:true
Get from Cache:bangalore
Prepend to cache:true
Get from Cache:delhibangalore

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4