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
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.