Memcached 简明教程
Memcached - Add Data
Memcached add 命令用于为新键设置值。如果键已存在,则它给出输出 NOT_STORED。
Memcached add command is used to set a value to a new key. If the key already exists, then it gives the output NOT_STORED.
Syntax
Memcached add 的基本语法如下所示 −
The basic syntax of Memcached add command is as shown below −
add key flags exptime bytes [noreply]
value
语法中的关键字的说明如下:
The keywords in the syntax are as described below −
-
key − It is the name of the key by which data is stored and retrieved from Memcached.
-
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.
-
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.
-
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.
-
noreply (optional) − It is a parameter that informs the server not to send any reply.
-
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
-
STORED indicates success.
-
NOT_STORED indicates the data is not stored in Memcached.
Example
在以下示例中,我们使用“key”作为键,并以 900 秒的过期时间在其内添加值 Memcached。
In the following example, we use ‘key’ as the key and add the value Memcached in it with an expiration time of 900 seconds.
add key 0 900 9
memcached
STORED
get key
VALUE key 0 9
Memcached
END
Add Data Using Java Application
若要将数据添加到 Memcached 服务器,您需要使用 Memcached add 方法。
To add data in a Memcached server, you need to use the Memcached add 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("add status:"+mcc.add("tutorialspoint", 900, "redis").done);
System.out.println("add status:"+mcc.add("tp", 900, "redis").done);
// Get value from cache
System.out.println("Get from Cache tp:"+mcc.get("tp"));
}
}