Memcached Set Data - Memcached

What is Memcached set data command?

Memcached set command is used to set a value to key; if the key does not appear then another key generated and value is assigned to that key.

Syntax

Here’s the basic syntax of Memcached set command :
The keywords in the syntax are as described below −
  • key - It is the name of the unique key by which data is accessed.
  • 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 (seconds) of data stored in cache. A 0 value means "never expire", i.e. it should not be removed from the cache unless required. If the exptime is more than 30 days then Memcached interprets it as UNIX timestamp for expiration.
  • bytes - This is the length of the data in bytes that needs to be stored in Memcached.
  • noreply (optional) - This parameter informs the server not to send any reply.
  • value - It is the data that needs to be stored. The data needs to be given on the new line after executing the command with the above options.

Example

In the following example, we use −
key → wisdomjobs
flag → 0
exptime → 900 (expiry time in seconds)
bytes → 9 (size of data balue in bytes)
value → memcached
Output
The output of the command is as shown below –
  • STORED indicates success.
  • ERROR indicates incorrect syntax or error while saving data.

Set Data Using Java Application

Use use Memcached set method to set the data using java application. Then set method returns Future Object. Now, we need to include java.util.concurrent.Future interface in order to store output of set method.
Example
This program should set the value memcached with key wisdomjobs. If the key-value pair is set successfully, the get method of Future object should return true.

Output

Once you compel and execute the program, you will get the below mentioned output –

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Memcached Topics