├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | 39 | .idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Trek10 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # lambda-local-cache for JS objects 2 | 3 | ## Installation : 4 | 5 | ``` 6 | npm install lambda-local-cache --save 7 | ``` 8 | 9 | 10 | ## How to use 11 | ```js 12 | const lambdaLocalCache = require( 'lambda-local-cache' ); 13 | 14 | let collectionName = 'COLLECTION_NAME' 15 | 16 | let options = { 17 | indexes: ['index1', 'index2'], // first index name is treated as primary index 18 | expire : 5 // in minutes 19 | }; 20 | 21 | 22 | let cache = new lambdaLocalCache(collectionName, options); 23 | 24 | 25 | // METHODS 26 | 27 | cache.set(value, expire); 28 | 29 | cache.get('key', 'indexName'); 30 | 31 | cache.remove('key', 'indexName'); 32 | 33 | cache.clear(); 34 | 35 | 36 | ## License 37 | 38 | MIT 39 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class CacheCollection { 4 | 5 | /** 6 | * 7 | * @param {String} collectionName (required) 8 | * @param {Object} options (required) { 9 | * expire: default expiry time span in seconds 10 | * indexes: [ array of index fields ] - first index name is treated as primary index 11 | * } 12 | */ 13 | constructor(collectionName, options) { 14 | //make sure we have global cache storage object 15 | if (!global['CACHE_STORAGE']) global['CACHE_STORAGE'] = {}; 16 | 17 | //make sure indexes are supplied with options 18 | if (!options.indexes) options.indexes = []; 19 | if (typeof options.indexes === 'string') options.indexes = [options.indexes]; 20 | if (!options.indexes.length) throw new Error('Indexes are not supplied'); 21 | 22 | //create cache collection if does not exists 23 | if (!global['CACHE_STORAGE'][collectionName]) { 24 | let container = { 25 | primaryIndexName: options.indexes.shift(), 26 | primaryStorage: new Map(), 27 | 28 | secondaryIndexNames: options.indexes, 29 | secondaryStorage: new Map() 30 | }; 31 | 32 | options.indexes.forEach(indexName => container.secondaryStorage.set(indexName, new Map())); 33 | global['CACHE_STORAGE'][collectionName] = container; 34 | } 35 | 36 | this._options = options; 37 | this._container = global['CACHE_STORAGE'][collectionName]; 38 | } 39 | 40 | /** 41 | * @param {Object} value (required) - item to save in cache 42 | * @param {Number} expire (optional) - expiration time in minutes, default : 1 min 43 | * */ 44 | set(value, expire) { 45 | if (Array.isArray(value)) { 46 | value.forEach(_ => this.set(_, expire)); 47 | return; 48 | } 49 | 50 | let primaryIndexValue = getKeyValue(value, this._container.primaryIndexName); 51 | expire = (this._options.expire || expire || 1) * 60000 + Date.now(); 52 | this._container.primaryStorage.set(primaryIndexValue, { value, expire }); 53 | 54 | this._container.secondaryIndexNames.forEach(_ => { 55 | let secondaryIndexValue = getKeyValue(value, _); 56 | this._container.secondaryStorage.get(_).set(secondaryIndexValue, primaryIndexValue); 57 | }); 58 | } 59 | 60 | /** 61 | * @param {String} key (required) 62 | * @param {String=} indexName (optional) - default is primary index 63 | * */ 64 | get(key, indexName) { 65 | if(!key) throw new Error('key is required!'); 66 | 67 | let record; 68 | 69 | if(!indexName || indexName == this._container.primaryIndexName) { 70 | record = this._container.primaryStorage.get(key); 71 | } else { 72 | if(!this._container.secondaryStorage.get(indexName) || !this._container.secondaryStorage.get(indexName).has(key)) return null; 73 | 74 | let primaryKey = this._container.secondaryStorage.get(indexName).get(key); 75 | record = this._container.primaryStorage.get(primaryKey); 76 | } 77 | 78 | if (!record) return null; 79 | 80 | if (!record.expire || record.expire > Date.now()) return record.value; 81 | else return this.remove(key, indexName); 82 | } 83 | 84 | /** 85 | * @param {String} key (required) 86 | * @param {String} indexName (required) 87 | * */ 88 | remove(key, indexName) { 89 | let primaryKey = !indexName ? key : 90 | this._container.secondaryStorage.get(indexName).get(key); 91 | 92 | let record = this._container.primaryStorage.get(primaryKey); 93 | if (!record) return null; 94 | 95 | this._container.secondaryIndexNames.forEach(_ => { 96 | let secondaryIndexValue = getKeyValue(record.value, _); 97 | this._container.secondaryStorage.get(_).delete(secondaryIndexValue); 98 | }); 99 | 100 | this._container.primaryStorage.delete(primaryKey); 101 | console.log('item removed...'); 102 | return null; 103 | } 104 | 105 | /** 106 | * clear cache storage 107 | * */ 108 | clear() { 109 | this._container.primaryStorage.clear(); 110 | 111 | this._container.secondaryIndexNames.forEach(_ => { 112 | this._container.secondaryStorage.get(_).clear(); 113 | }); 114 | } 115 | 116 | 117 | /** 118 | * get cached items count 119 | * */ 120 | get count() { 121 | return this._container.primaryStorage.size; 122 | } 123 | } 124 | 125 | function getKeyValue(obj, key) { 126 | return obj[key]; 127 | } 128 | 129 | module.exports = CacheCollection; 130 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "lambda-local-cache", 3 | "version": "1.0.2", 4 | "description": "local object caching in node", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/trek10inc/lambda-local-cache" 12 | }, 13 | "keywords": [ 14 | "lambda", 15 | "cache", 16 | "aws", 17 | "node", 18 | "node-cache", 19 | "global-cache", 20 | "node-global" 21 | ], 22 | "author": "trek10.com", 23 | "license": "MIT" 24 | } 25 | --------------------------------------------------------------------------------