Home » How to Set Data in Memcached

How to Set Data in Memcached

by Online Tutorials Library

Memcached – Set Data

The memcached set command is the most common command. It is used to set a new value to a new or existing key. It means, it stores the data or possibly overwrites the existing data. New items are stored at the top of the LRU (Least Recently Used).

Syntax

Here,

key: It is the key to the data 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: exptime is expiration time in seconds. 0 means no delay. If it is more than 30 days, Memcached uses it as 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 is stored in Memcached.

noreply: It is an optional parameter. It is used to inform the server not to send any reply.

value: It is the data that have to be stored. The data needs to be passed on the new line after executing the command with the above options.

Return Value

This command will return the following values:

STORED: STORED means success

ERROR: ERROR means incorrect syntax or error while saving data.

Example in Ubuntu

Let’s see an example for simple set command. Here, we use the city as the key and set value Bangalore in it with an expiration time of 0 seconds.

How to Set Data in Memcached

Example in Windows

Let’s see an example for simple set command. Here, we use city as the key and set value Bangalore in it with an expiration time of 0 seconds.

How to Set Data in Memcached

Example in Java Application

Consider that Memcached server is running on host 127.0.0.1and port 11211. Here, we will use the set() method to set a key in the Memcached server.

Example:

Output:

Connection to server successfully  set status: true  Get from Cache: Bangalore  

You may also like