Question

Does CouchDB have an equivalent to expire like in Redis?

Example for Redis expire:

#!/usr/bin/env python
import redis
redis_server = redis.Redis(host='localhost',port=5477,db=0)
r.set('cat','meow')
r.expire('cat',10)
# do some work and ten seconds later...
r.get('cat') # returns None
Was it helpful?

Solution

No. CouchDB does not have this.

Redis uses a lazy approach and deletes keys when they are inspected even though they may have expired much earlier. Also, as @antirez pointed out Redis will remove a random set of expired keys every second or so to keep the database size under control.

If CouchDB does not natively support this, you could add a tiny layer on top of your objects to do this work. Add an expiry field and when trying to retrieve objects, make sure expiry is in the future. If not, delete the expired objects. Furthermore, since deleted objects must persist (so the delete action can be replicated), you will also need to periodically find deleted documents and purge them.

OTHER TIPS

No. This is memcache/redis feature. CouchDB is data-persistent db.

Good question! The simple answer is "no" but another answer is mu.

The idiomatic CouchDB approach would be to have expires_at timestamps in the records (documents). Next have a view, indexed by expiration timestamp. Clients would query the view keyed on timestamp, with the timestamp value greater or equal to now. The result will be a list of all valid documents.

This requires clients' clocks to be synchronized. If you have one central, authoritative server (a very common situation), an easy way to synchronize is for clients to ping couch and check its HTTP Date header.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top