Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 666
pokstad Avatar asked Jun 11 '11 19:06

pokstad


3 Answers

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.

like image 85
Anurag Avatar answered Nov 15 '22 04:11

Anurag


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.

like image 38
JasonSmith Avatar answered Nov 15 '22 03:11

JasonSmith


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

like image 45
Dmitrii Sorin Avatar answered Nov 15 '22 04:11

Dmitrii Sorin