Introducing Dragonfly Cloud! Learn More

Question: Does Memcached expire?


The default TTL for items in Memcached is 0, meaning they will never expire unless evicted due to memory constraints or being explicitly deleted. However, it's best practice to set a reasonable TTL based on the expected frequency of updates to the data being cached.

Here's an example of setting a TTL of 60 seconds for a cache item in Memcached using the Python library python-memcached:

import memcache

client = memcache.Client(['localhost:11211'])
key = 'my_key'
value = 'my_value'
ttl = 60
client.set(key, value, ttl=ttl)

In this example, the set method is used to store a key-value pair in the cache with a TTL of 60 seconds. When the TTL expires, the item will be automatically removed from the cache.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.