Created
December 11, 2019 02:03
-
-
Save AlexanderAllen/1b561a4a47eb09cef5ef250cc542cf58 to your computer and use it in GitHub Desktop.
levelup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file: cache.ts | |
*/ | |
// Import levelup dependencies manually. | |
var levelup = require('levelup') | |
var leveljs = require('level-js') | |
var ttl = require('level-ttl') | |
// Instantiate store. | |
var db = levelup(leveljs('/tmp/mydb.db')) | |
// Try setting up global ttl (also supports per-item ttl) | |
db = ttl(db, { checkFrequency: 1000 }) | |
// Set cache. | |
export const cache_get = async (cid: string, $bin = 'cache') => { | |
// https://github.com/Level/level-js#type-support | |
return await db.get(cid, { asBuffer: false }) | |
} | |
// Get cache. | |
export const cache_set = (cid:string, data:string, expire:number = CACHE_PERMANENT) => { | |
console.log('Setting cache cid "' + cid + '" to expire in ' + expire + ' milliseconds') | |
// todo automatically serialize data if required. | |
db.put(cid, data, { ttl: expire }, err => { | |
if(err) console.log(err) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment