Javatpoint Logo
Javatpoint Logo

Memcached - CAS Command

Full form of CAS is Check And Set or Compare And Swap. This command is used to set the data if it is not updated since last fetch. In other words, we can say that CAS is an operation that stores data, but only if no one else has updated the data since you read it last. This command is very useful for resolving race conditions on updating cache data.



key: It is the key of the data stored and retrieved from Memcached.

flags: flags is the 32-bit unsigned integer that the server stores with the data (which is 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: bytes is the number of bytes in the data block that requires to be stored. This is the length of the data that is stored in Memcached.

Unique_cas_key: This is a unique key and get from the gets command.

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

value: value 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: shows the error while saving data or wrong syntax.
  • EXISTS: It shows that someone has modified the CAS data since last fetch.
  • NOT_FOUND: It shows that the key does not exist in the Memcached server.


To execute a CAS command in Memcached, we have to get a CAS token from the Memcached gets command.

Let's see an example:

Memcached Server

Memcached CAS Command

Memcached Client

Memcached CAS Command

CAS using Java Application

Consider that Memcached server is running on host port 11211.v Here, we will use gets() method to prepend data in memcached server.



Connection to server successfully
set status:true
Cas token: 3
Prepend to cache:OK
Get from Cache:deep

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA