├── .gitignore ├── README.md ├── index.js ├── package-lock.json ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Cache - A simple caching object with TTL 3 | 4 | Copyright 2017 Sleepless Software Inc. All rights reserved. 5 | 6 | 7 | ## Install 8 | 9 | npm install cache 10 | 11 | 12 | ## Usage 13 | 14 | Cache = require("cache"); 15 | 16 | c = new Cache(10 * 1000); // Create a cache with 10 second TTL 17 | 18 | key = "foo"; 19 | val = "something"; // any object 20 | 21 | c.put(key, val); // put it in the cache. 22 | // Optional 3rd arg is TTL for just this 23 | // key, e.g.; c.put(key, val, 5 * 1000); 24 | 25 | c.get(key); // "something" (less than 10 secs have passed) 26 | 27 | // 11 seconds later ... 28 | setTimeout(function() { 29 | 30 | c.get(key); // expired out of cache 31 | 32 | }, 11 * 1000); 33 | 34 | 35 | ## More 36 | 37 | You can also do some other stuff. 38 | 39 | c.del(key) // delete key/val before expiry 40 | 41 | You can create a cache that will write its contents to a file in 42 | JSON form (not recommended for production purposes): 43 | 44 | c = new Cache(10 * 1000, "data.json"); 45 | 46 | Now, when do anything that changes the contents of the cache, 47 | it will write it to the file "data.json": 48 | 49 | c.put(key, val); // data.json appears 50 | 51 | Also, the cache will be preloaded from the file when you 52 | instantiate it, if the file is present. 53 | 54 | 55 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | // Copyright 2017 Sleepless Software Inc. All rights reserved. 3 | 4 | DS = require("ds").DS; 5 | 6 | 7 | function Cache( ttl = 0, save_file = null ) { 8 | 9 | let me = this; 10 | 11 | me.now = function() { return (new Date()).getTime() } 12 | me.ttl = ttl || 0; 13 | me.data = new DS(); 14 | 15 | let save = function() { 16 | if( save_file ) 17 | me.data.save( save_file ); 18 | return me; 19 | } 20 | 21 | let nuke = function( key ) { 22 | delete me.data[ key ] 23 | save(); 24 | return me; 25 | } 26 | 27 | me.get = function( key, cb ) { 28 | let val = null 29 | let obj = me.data[key] 30 | if(obj) { 31 | if( obj.expires == 0 || me.now() < obj.expires ) { 32 | val = obj.val; 33 | } else { 34 | val = null; 35 | nuke( key ); 36 | } 37 | } 38 | if(cb) 39 | cb(val); 40 | return val; 41 | } 42 | 43 | me.del = function(key, cb) { 44 | let oldval = me.get(key); 45 | nuke( key ); 46 | if(cb) 47 | cb(oldval); 48 | return oldval; 49 | } 50 | 51 | me.put = function(key, val = null, ttl = 0, cb) { 52 | if(ttl == 0) 53 | ttl = me.ttl; 54 | let expires = ( ttl == 0 ) ? 0 : ( me.now() + ttl ); 55 | var oldval = me.del(key); 56 | if(val !== null) { 57 | me.data[ key ] = { 58 | expires, 59 | val, 60 | } 61 | save(); 62 | } 63 | if(cb) 64 | cb(oldval); 65 | return oldval; 66 | } 67 | 68 | } 69 | 70 | module.exports = Cache; 71 | 72 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cache", 3 | "version": "3.0.0", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "cache", 9 | "version": "3.0.0", 10 | "license": "ISC", 11 | "dependencies": { 12 | "ds": "^1.4.2" 13 | } 14 | }, 15 | "node_modules/ds": { 16 | "version": "1.4.2", 17 | "resolved": "https://registry.npmjs.org/ds/-/ds-1.4.2.tgz", 18 | "integrity": "sha512-d5nMCjfod+srvE/1Bnt/u+L++6N8KJx3ZAi95AGp0g6RtfuGDNlGciWL/iiwKHsFVBVnA3/HEFUq5SW1NgTQ3Q==" 19 | } 20 | }, 21 | "dependencies": { 22 | "ds": { 23 | "version": "1.4.2", 24 | "resolved": "https://registry.npmjs.org/ds/-/ds-1.4.2.tgz", 25 | "integrity": "sha512-d5nMCjfod+srvE/1Bnt/u+L++6N8KJx3ZAi95AGp0g6RtfuGDNlGciWL/iiwKHsFVBVnA3/HEFUq5SW1NgTQ3Q==" 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cache", 3 | "version": "3.0.0", 4 | "description": "Simple caching object with optional TTL and file system persistence.", 5 | "main": "index.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "git+https://github.com/sleeplessinc/cache.git" 9 | }, 10 | "author": "Joe Hitchens ", 11 | "license": "ISC", 12 | "homepage": "https://github.com/sleeplessinc/cache#readme", 13 | "dependencies": { 14 | "ds": "^1.4.2" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 2 | function throwIf(c, s) { 3 | if(c) { 4 | throw new Error("FAIL: "+s); 5 | } 6 | }; 7 | 8 | function log(s) { 9 | console.log(s); 10 | } 11 | 12 | // --------------- 13 | 14 | Cache = require("./index.js"); 15 | 16 | 17 | c = new Cache(1 * 1000); // defaults to 1 sec TTL 18 | 19 | c.put("1", "one"); // use default 1 sec TTL 20 | c.put("6", "six", 6 * 1000); // specify 6 sec TTL for this key 21 | 22 | throwIf(c.get("1") !== "one"); 23 | 24 | setTimeout(()=> { 25 | log("2 secs ..."); 26 | 27 | throwIf(c.get("1") !== null); 28 | 29 | throwIf(c.get("6") !== "six"); 30 | 31 | setTimeout(()=> { 32 | log("4 secs ..."); 33 | 34 | // should still be there 35 | throwIf(c.get("6") !== "six"); 36 | 37 | setTimeout(()=> { 38 | log("6 secs ..."); 39 | 40 | // should be gone now 41 | throwIf(c.get("6") !== null); 42 | 43 | console.log("All tests passed."); 44 | 45 | }, 2 * 1000); 46 | 47 | }, 2 * 1000); 48 | 49 | }, 2 * 1000); 50 | 51 | 52 | --------------------------------------------------------------------------------