Memcached expiration time – Here is how we set it
Curious to know about Memcached expiration time?
Memcached is a caching system that helps to speed up websites by loading cached data.
The expiration time is the duration specified in seconds until which Memcached stores the data.
At Bobcares, users often approach us on queries regarding Memcached parameters, as a part of our Server Management Services.
Today, let’s see have a look at Memcached and a guide by our Support Engineers on the expiration time.
A quick look at Memcached
Memcached is an open-source, high-performance, distributed memory caching system. The purpose of this is to speed up dynamic web applications. Usually, it does this by reducing the database load.
Above all, the Memcached server is a big hash table suitable for websites with high database load.
Now let’s have a look at Memcached expiration time.
What is the expiration time in Memcached?
The expiration time is a parameter defined during data storage. Memcached allows users to use storage commands like the set, add, replace, append, prepend and CAS (Check-And-Set or Compare-And-Swap).
Let’s see what is expiration time by having a look at the set command. One way to connect to a Memcached server is by using telnet command. And the command usage is,
telnet <HOST> <PORT>
The default port for Memcached is 11211. Here, we connected to the Memcached server running on the localhost.
The Memcached add command adds a new value to a new or else an existing key. And the usage of this command is,
add key flags exptime bytes [noreply] <value>
The other parameters in the command are,
- key − This is the key name by which we can store and retrieve data from Memcached.
- flags − This is a 32-bit unsigned integer. The server stores this flag with the data set by the user. Also, it returns the flag when we retrieve the data.
- exptime − It is the expiration time in seconds.
- bytes − The number of bytes in the data block. Usually, this is the length of the data that we store in Memcached.
- noreply – This optional parameter informs the server not to send any reply.
- <value> − It is the data we want to store. Always enter the data on a new line after executing the set command.
A sample output of adding a new key using add command appears as,
Here the name of the key is key itself, the flag is 0, the expiration time is 900 seconds, and bytes is 9. And we added value ‘memcached‘. After using the get command we obtained the added values.
Now let’s have a closer look at exptime or expiration time.
A detailed look at exptime
The expiration time in Memcached is in seconds. For instance, the default value is 10800 seconds. But, it can have a maximum value of 2592000 seconds that is, 30 days.
A value greater than 2592000 is a Unix time in Memcached. Because Memcached counts Unix time from the number of seconds since January 1, 1970.
Similarly, we can also have no automatic expiration by specifying expiration time as zero. Memcached has a lazy caching. That is, it deletes values only when requested, or during a new entry when the cache is full.
More specifically, Memcached deletes items before there expiration time due to the unavailability of memory.
Manually clearing cache
Even though Memcached speeds up a website by loading cache, this has a drawback. Memcached still loads the cache after the website update. This is not desirable.
So, there is a need to clear the cache before the expiration time. And this is possible in Memcached. For this, our Support Engineers use the command,
This removes all the stored cache.
[Still, having trouble in setting up the Memcached server? – We can help.]
So far, we saw a brief guide on Memcached expiration time. This parameter determines the duration of how long Memcached stores data. We also saw how our Support Engineers set the expiration time for a new key.