Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added ttl() method

  • Loading branch information...
commit 05da3708bdcc7ae44f7808c7c48958408d7c841c 1 parent 305a885
@rvagg rvagg authored
Showing with 54 additions and 0 deletions.
  1. +7 −0 README.md
  2. +6 −0 level-ttl.js
  3. +41 −0 test.js
View
7 README.md
@@ -29,6 +29,13 @@ levelup('/tmp/foo.db', function (err, db) {
If you put the same entry twice, you **refresh** the TTL to the *last* put operation. In this way you can build utilities like [session managers](https://github.com/rvagg/node-level-session/) for your web application where the user's session is refreshed with each visit but expires after a set period of time since their last visit.
+Alternatively, for a lower write-footprint you can use the `ttl()` method that is added to your LevelUP instance which can serve to insert or update a ttl for any given key in the database (even if that key doesn't exist but may in the future! Crazy!).
+
+```js
+db.put('foo', 'bar', function (err) { /* .. */ })
+db.ttl(1000 * 60 * 60, function (err) { /* .. */ })
+```
+
**Level TTL** uses an internal scan every 10 seconds by default, this limits the available resolution of your TTL values, possibly delaying a delete for up to 10 seconds. The resolution can be tuned by passing the `'checkFrequency'` option to the `ttl()` initialiser.
```js
View
6 level-ttl.js
@@ -149,6 +149,11 @@ var startTtl = function (db, checkFrequency) {
db._ttl.put.call(db, key, value, options, callback)
}
+ , ttl = function (db, key, ttl, callback) {
+ if (ttl > 0 && key !== null && key !== undefined)
+ ttlon(db, key, ttl, callback)
+ }
+
, del = function (db, key, options, callback) {
var done
, _callback = callback
@@ -226,6 +231,7 @@ var startTtl = function (db, checkFrequency) {
db[options.methodPrefix + 'put'] = put.bind(null, db)
db[options.methodPrefix + 'del'] = del.bind(null, db)
db[options.methodPrefix + 'batch'] = batch.bind(null, db)
+ db[options.methodPrefix + 'ttl'] = ttl.bind(null, db)
// we must intercept close()
db['close'] = close.bind(null, db)
View
41 test.js
@@ -231,6 +231,47 @@ ltest('test prolong entry life with additional put', function (db, t, createRead
setTimeout(t.end.bind(t), 300)
})
+ltest('test prolong entry life with ttl(key, ttl)', function (db, t, createReadStream) {
+ var ttlBar = function () {
+ db.ttl('bar', 40)
+ return Date.now()
+ }
+ , verify = function (base, delay) {
+ setTimeout(function () {
+ db2arr(createReadStream, t, function (err, arr) {
+ t.notOk(err, 'no error')
+ var ts = base + 37
+ , i = 0
+ // allow +/- 3ms leeway, allow for processing speed and Node timer inaccuracy
+ for (; i < 6 && arr[3] && arr[3].value; i++) {
+ if (arr[3] && arr[3].value == String(ts))
+ break
+ ts++
+ }
+ t.deepEqual(arr, [
+ { key: 'bar', value: 'barvalue' }
+ , { key: 'foo', value: 'foovalue' }
+ , { key: 'ÿttlÿ' + ts + 'ÿbar', value: 'bar' }
+ , { key: 'ÿttlÿbar', value: String(ts) }
+ ])
+ })
+ }, delay)
+ }
+ , retest = function (delay) {
+ setTimeout(function () {
+ var base = ttlBar()
+ verify(base, 10)
+ }, delay)
+ }
+ , i
+
+ db.put('foo', 'foovalue')
+ db.put('bar', 'barvalue')
+ for (i = 0; i < 200; i += 20)
+ retest(i)
+ setTimeout(t.end.bind(t), 300)
+})
+
ltest('test del', function (db, t, createReadStream) {
var verify = function (base, delay) {
setTimeout(function () {
Please sign in to comment.
Something went wrong with that request. Please try again.