Search code examples
pythonrediscouchdbkey-value-store

Does CouchDB have an equivalent to Redis' expire?


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

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.