Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nq-cache

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nq-cache - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

dist/cache.js.map

33

CHANGELOG.md

@@ -1,3 +0,2 @@

<a name="0.0.10"></a>
## [0.0.10](https://git.sdp.nd//compare/v0.0.9...v0.0.10) (2018-05-17)
## 0.0.2 (2019-03-21)

@@ -7,31 +6,1 @@

<a name="0.0.9"></a>
## [0.0.9](https://git.sdp.nd//compare/v0.0.8...v0.0.9) (2018-05-17)
<a name="0.0.8"></a>
## [0.0.8](https://git.sdp.nd//compare/v0.0.7...v0.0.8) (2018-05-17)
<a name="0.0.7"></a>
## [0.0.7](https://git.sdp.nd//compare/v0.0.6...v0.0.7) (2018-05-17)
<a name="0.0.6"></a>
## [0.0.6](https://git.sdp.nd//compare/v0.0.5...v0.0.6) (2018-05-17)
<a name="0.0.5"></a>
## 0.0.5 (2018-05-17)
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.cache = {})));
}(this, (function (exports) { 'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.cache = {}));
}(this, function (exports) { 'use strict';
/**
* 把参数转换为key
* @param {...} arg - 可以有多个参数
* @returns {string} 用作缓存数据的key
*/
function argToKey() {
for (var _len = arguments.length, arg = new Array(_len), _key = 0; _key < _len; _key++) {
arg[_key] = arguments[_key];
function argToKey() {
for (var _len = arguments.length, arg = new Array(_len), _key = 0; _key < _len; _key++) {
arg[_key] = arguments[_key];
}
return JSON.stringify(arg);
}
return JSON.stringify(arg);
}
/**
* 缓存函数数组
* @type {Array}
*/
var cacheFunList = [];
/**
* 把缓存函数加入到数组中
* @param {Function} func - 函数
*/
function push(func) {
cacheFunList.push(func);
}
/**
* 清空在数组中函数的缓存数据
*/
function clearCache() {
cacheFunList.forEach(function (func) {
func.clearCache();
});
}
var store = {
push: push,
clearCache: clearCache
};
/**
* 异步的结果缓存在内存中,下次调用直接使用缓存数据
* @param {Function} fn - 纯函数
* @returns {Function} 和原函数的调用方式是一样的,第一次调用数据缓存后,之后的调用不会调用原函数,而是会直接使用缓存后的数据,另外该函数还带有一个静态的clearCache方法,可以用来清空缓存数据
* @property {Function} clearCache 清空缓存数据
*/
function promiseCache(fn) {
var cache = {};
var cachedFn = function cachedFn() {
var key = argToKey.apply(void 0, arguments);
return cache.hasOwnProperty(key) ? Promise.resolve(cache[key]) : fn.apply(void 0, arguments).then(function (res) {
cache[key] = res;
return Promise.resolve(res);
var cacheFunList = [];
function push(func) {
cacheFunList.push(func);
}
function clearCache() {
cacheFunList.forEach(function (func) {
func.clearCache();
});
}
var store = {
push: push,
clearCache: clearCache
};
cachedFn.clearCache = function (key) {
if (key) {
delete cache[key];
} else {
cache = {};
}
};
function promiseCache(fn) {
var cache = {};
cachedFn.funType = 'promise';
cachedFn.cacheType = 'memory';
cachedFn.originFunName = fn.name;
store.push(cachedFn);
return cachedFn;
}
var cachedFn = function cachedFn() {
var key = argToKey.apply(void 0, arguments);
return cache.hasOwnProperty(key) ? Promise.resolve(cache[key]) : fn.apply(void 0, arguments).then(function (res) {
cache[key] = res;
return Promise.resolve(res);
});
};
/**
* @module Storage
* @author crossjs <liwenfu@crossjs.com>
*/
/**
* @param {string} prefix key 前缀
* @param {number} expire 默认过期秒数
*/
cachedFn.clearCache = function (key) {
if (key) {
delete cache[key];
} else {
cache = {};
}
};
function Storage(prefix, expire) {
this.prefix = prefix || '';
if (expire === -1) {
this.driver = window.sessionStorage;
} else {
this.driver = window.localStorage;
this.expire = expire || 0;
cachedFn.funType = 'promise';
cachedFn.cacheType = 'memory';
cachedFn.originFunName = fn.name;
store.push(cachedFn);
return cachedFn;
}
}
Storage.prototype = {
constructor: Storage,
_key: function _key(key) {
return this.prefix + key;
},
/**
* 获取所有的本地存储数据对应的 key
* @module Storage
* @author crossjs <liwenfu@crossjs.com>
*/
keys: function keys() {
var keys = Object.keys(this.driver);
/**
* @param {string} prefix key 前缀
* @param {number} expire 默认过期秒数
*/
if (this.prefix) {
var index = this.prefix.length;
return keys.map(function (key) {
return key.substring(index);
});
function Storage(prefix, expire) {
this.prefix = prefix || '';
if (expire === -1) {
this.driver = window.sessionStorage;
} else {
this.driver = window.localStorage;
this.expire = expire || 0;
}
}
return keys;
},
Storage.prototype = {
constructor: Storage,
_key: function (key) {
return this.prefix + key;
},
/**
* 移除某一项本地存储的数据
*/
remove: function remove(key) {
this.driver.removeItem(this._key(key));
},
/**
* 获取所有的本地存储数据对应的 key
*/
keys: function () {
var keys = Object.keys(this.driver);
/**
* 清除所有本地存储的数据
*/
clear: function clear() {
this.driver.clear();
},
if (this.prefix) {
var index = this.prefix.length;
return keys.map(function (key) {
return key.substring(index);
});
}
/**
* 将数据进行本地存储
*/
set: function set(key, value, expire) {
var data = {
value: value
};
return keys;
},
if (typeof expire === 'undefined') {
expire = this.expire;
}
/**
* 移除某一项本地存储的数据
*/
remove: function (key) {
this.driver.removeItem(this._key(key));
},
if (expire) {
data.expire = Date.now() + expire * 1000;
}
/**
* 清除所有本地存储的数据
*/
clear: function () {
this.driver.clear();
},
this.driver.setItem(this._key(key), JSON.stringify(data));
},
/**
* 将数据进行本地存储
*/
set: function (key, value, expire) {
var data = {
value: value
};
/**
* 提取本地存储的数据
*/
get: function get(key) {
var data = this.driver.getItem(this._key(key));
if (typeof expire === 'undefined') {
expire = this.expire;
}
if (data) {
data = JSON.parse(data);
if (data.expire) {
if (data.expire < Date.now()) {
this.remove(key);
data = null;
}
if (expire) {
data.expire = Date.now() + expire * 1000;
}
}
return data && data.value;
}
};
var ndStorage = Storage;
this.driver.setItem(this._key(key), JSON.stringify(data));
},
/**
* session storage
* @type {Storage}
*/
/**
* 提取本地存储的数据
*/
get: function (key) {
var data = this.driver.getItem(this._key(key));
var SessionStorage = new ndStorage('S-CACHE', -1);
if (data) {
data = JSON.parse(data);
/**
* 异步的结果缓存在session storage中,下次调用直接使用缓存数据
* @param {Function} fn - 返回值是Promise的函数
* @param {Function} storageKey - 缓存在session storage中的key前缀
* @returns {Function} 和原函数的调用方式是一样的,第一次调用数据缓存后,之后的调用不会调用原函数,而是会直接使用缓存后的数据,另外该函数还带有一个静态的clearCache方法,可以用来清空缓存数据
* @property {Function} clearCache 清空缓存数据
*/
if (data.expire) {
if (data.expire < Date.now()) {
this.remove(key);
data = null;
}
}
}
function promiseSessionStorageCache(fn, storageKey) {
var cache = SessionStorage.get(storageKey) || {};
var cachedFn = function cachedFn() {
var key = argToKey.apply(void 0, arguments);
return cache.hasOwnProperty(key) ? Promise.resolve(cache[key]) : fn.apply(void 0, arguments).then(function (res) {
cache[key] = res;
SessionStorage.set(storageKey, cache);
return Promise.resolve(res);
});
};
cachedFn.clearCache = function (key) {
if (key) {
delete cache[key];
SessionStorage.set(storageKey, cache);
} else {
cache = {};
SessionStorage.remove(storageKey);
return data && data.value;
}
};
var ndStorage = Storage;
cachedFn.funType = 'promise';
cachedFn.cacheType = 'sessionStorage';
cachedFn.cacheKey = storageKey;
cachedFn.originFunName = fn.name;
store.push(cachedFn);
return cachedFn;
}
var SessionStorage = new ndStorage('S-CACHE-', -1);
/**
* 纯函数执行的结果缓存在内存中,下次调用直接使用缓存数据
* @param {Function} fn - 返回值是Promise的函数
* @returns {Function} 和原函数的调用方式是一样的,第一次调用数据缓存后,之后的调用不会调用原函数,而是会直接使用缓存后的数据,另外该函数还带有一个静态的clearCache方法,可以用来清空缓存数据
* @property {Function} clearCache 清空缓存数据
*/
function promiseSessionStorageCache(fn, storageKey) {
var cache = SessionStorage.get(storageKey) || {};
function pureFuncCache(fn) {
var cache = {};
var cachedFn = function cachedFn() {
var key = argToKey.apply(void 0, arguments);
return cache.hasOwnProperty(key) ? Promise.resolve(cache[key]) : fn.apply(void 0, arguments).then(function (res) {
cache[key] = res;
SessionStorage.set(storageKey, cache);
return Promise.resolve(res);
});
};
var cachedFn = function cachedFn() {
var cacheKey = argToKey.apply(void 0, arguments);
return cache.hasOwnProperty(cacheKey) ? cache[cacheKey] : cache[cacheKey] = fn.apply(void 0, arguments);
};
cachedFn.clearCache = function (key) {
if (key) {
delete cache[key];
SessionStorage.set(storageKey, cache);
} else {
cache = {};
SessionStorage.remove(storageKey);
}
};
cachedFn.clearCache = function (key) {
if (key) {
delete cache[key];
} else {
cache = {};
}
};
cachedFn.funType = 'promise';
cachedFn.cacheType = 'sessionStorage';
cachedFn.cacheKey = storageKey;
cachedFn.originFunName = fn.name;
store.push(cachedFn);
return cachedFn;
}
cachedFn.funType = 'pureFunc';
cachedFn.cacheType = 'memory';
cachedFn.originFunName = fn.name;
store.push(cachedFn);
return cachedFn;
}
function pureFuncCache(fn) {
var cache = {};
exports.promiseMemoryCache = promiseCache;
exports.promiseSessionStorageCache = promiseSessionStorageCache;
exports.pureFuncMemoryCache = pureFuncCache;
exports.argToKey = argToKey;
exports.clearCache = clearCache;
var cachedFn = function cachedFn() {
var cacheKey = argToKey.apply(void 0, arguments);
return cache.hasOwnProperty(cacheKey) ? cache[cacheKey] : cache[cacheKey] = fn.apply(void 0, arguments);
};
Object.defineProperty(exports, '__esModule', { value: true });
cachedFn.clearCache = function (key) {
if (key) {
delete cache[key];
} else {
cache = {};
}
};
})));
cachedFn.funType = 'pureFunc';
cachedFn.cacheType = 'memory';
cachedFn.originFunName = fn.name;
store.push(cachedFn);
return cachedFn;
}
exports.argToKey = argToKey;
exports.clearCache = clearCache;
exports.promiseMemoryCache = promiseCache;
exports.promiseSessionStorageCache = promiseSessionStorageCache;
exports.pureFuncMemoryCache = pureFuncCache;
}));
//# sourceMappingURL=cache.js.map

@@ -1,1 +0,2 @@

var cache=function(e){"use strict";function r(){for(var e=arguments.length,r=new Array(e),n=0;n<e;n++)r[n]=arguments[n];return JSON.stringify(r)}var n=[];function t(){n.forEach(function(e){e.clearCache()})}var i={push:function(e){n.push(e)},clearCache:t};function o(e,r){this.prefix=e||"",-1===r?this.driver=window.sessionStorage:(this.driver=window.localStorage,this.expire=r||0)}o.prototype={constructor:o,_key:function(e){return this.prefix+e},keys:function(){var e=Object.keys(this.driver);if(this.prefix){var r=this.prefix.length;return e.map(function(e){return e.substring(r)})}return e},remove:function(e){this.driver.removeItem(this._key(e))},clear:function(){this.driver.clear()},set:function(e,r,n){var t={value:r};void 0===n&&(n=this.expire),n&&(t.expire=Date.now()+1e3*n),this.driver.setItem(this._key(e),JSON.stringify(t))},get:function(e){var r=this.driver.getItem(this._key(e));return r&&(r=JSON.parse(r)).expire&&r.expire<Date.now()&&(this.remove(e),r=null),r&&r.value}};var a=new o("S-CACHE",-1);return e.promiseMemoryCache=function(e){var n={},t=function(){var t=r.apply(void 0,arguments);return n.hasOwnProperty(t)?Promise.resolve(n[t]):e.apply(void 0,arguments).then(function(e){return n[t]=e,Promise.resolve(e)})};return t.clearCache=function(e){e?delete n[e]:n={}},t.funType="promise",t.cacheType="memory",t.originFunName=e.name,i.push(t),t},e.promiseSessionStorageCache=function(e,n){var t=a.get(n)||{},o=function(){var i=r.apply(void 0,arguments);return t.hasOwnProperty(i)?Promise.resolve(t[i]):e.apply(void 0,arguments).then(function(e){return t[i]=e,a.set(n,t),Promise.resolve(e)})};return o.clearCache=function(e){e?(delete t[e],a.set(n,t)):(t={},a.remove(n))},o.funType="promise",o.cacheType="sessionStorage",o.cacheKey=n,o.originFunName=e.name,i.push(o),o},e.pureFuncMemoryCache=function(e){var n={},t=function(){var t=r.apply(void 0,arguments);return n.hasOwnProperty(t)?n[t]:n[t]=e.apply(void 0,arguments)};return t.clearCache=function(e){e?delete n[e]:n={}},t.funType="pureFunc",t.cacheType="memory",t.originFunName=e.name,i.push(t),t},e.argToKey=r,e.clearCache=t,e}({});
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((e=e||self).cache={})}(this,function(e){"use strict";function i(){for(var e=arguments.length,r=new Array(e),n=0;n<e;n++)r[n]=arguments[n];return JSON.stringify(r)}var r=[];function n(){r.forEach(function(e){e.clearCache()})}var o={push:function(e){r.push(e)},clearCache:n};function t(e,r){this.prefix=e||"",-1===r?this.driver=window.sessionStorage:(this.driver=window.localStorage,this.expire=r||0)}t.prototype={constructor:t,_key:function(e){return this.prefix+e},keys:function(){var e=Object.keys(this.driver);if(this.prefix){var r=this.prefix.length;return e.map(function(e){return e.substring(r)})}return e},remove:function(e){this.driver.removeItem(this._key(e))},clear:function(){this.driver.clear()},set:function(e,r,n){var t={value:r};void 0===n&&(n=this.expire),n&&(t.expire=Date.now()+1e3*n),this.driver.setItem(this._key(e),JSON.stringify(t))},get:function(e){var r=this.driver.getItem(this._key(e));return r&&(r=JSON.parse(r)).expire&&r.expire<Date.now()&&(this.remove(e),r=null),r&&r.value}};var a=new t("S-CACHE-",-1);e.argToKey=i,e.clearCache=n,e.promiseMemoryCache=function(e){var n={},r=function(){var r=i.apply(void 0,arguments);return n.hasOwnProperty(r)?Promise.resolve(n[r]):e.apply(void 0,arguments).then(function(e){return n[r]=e,Promise.resolve(e)})};return r.clearCache=function(e){e?delete n[e]:n={}},r.funType="promise",r.cacheType="memory",r.originFunName=e.name,o.push(r),r},e.promiseSessionStorageCache=function(e,n){var t=a.get(n)||{},r=function(){var r=i.apply(void 0,arguments);return t.hasOwnProperty(r)?Promise.resolve(t[r]):e.apply(void 0,arguments).then(function(e){return t[r]=e,a.set(n,t),Promise.resolve(e)})};return r.clearCache=function(e){e?(delete t[e],a.set(n,t)):(t={},a.remove(n))},r.funType="promise",r.cacheType="sessionStorage",r.cacheKey=n,r.originFunName=e.name,o.push(r),r},e.pureFuncMemoryCache=function(r){var n={},e=function(){var e=i.apply(void 0,arguments);return n.hasOwnProperty(e)?n[e]:n[e]=r.apply(void 0,arguments)};return e.clearCache=function(e){e?delete n[e]:n={}},e.funType="pureFunc",e.cacheType="memory",e.originFunName=r.name,o.push(e),e}});
//# sourceMappingURL=cache.min.js.map
export { argToKey } from './utils/assist';
import _promiseMemoryCache from './cache/promise-mm';
export { _promiseMemoryCache as promiseMemoryCache };
import _promiseSessionStorageCache from './cache/promise-ss';
export { _promiseSessionStorageCache as promiseSessionStorageCache };
import _pureFuncMemoryCache from './cache/pure-func-mm';
export { _pureFuncMemoryCache as pureFuncMemoryCache };
export { default as promiseMemoryCache } from './cache/promise-mm';
export { default as promiseSessionStorageCache } from './cache/promise-ss';
export { default as pureFuncMemoryCache } from './cache/pure-func-mm';
export { clearCache } from './cache/store';

@@ -7,2 +7,2 @@ import Storage from 'nd-storage';

export const SessionStorage = new Storage('S-CACHE', -1);
export const SessionStorage = new Storage('S-CACHE-', -1);

@@ -16,3 +16,3 @@ "use strict";

*/
var SessionStorage = new _ndStorage.default('S-CACHE', -1);
var SessionStorage = new _ndStorage.default('S-CACHE-', -1);
exports.SessionStorage = SessionStorage;
{
"name": "nq-cache",
"version": "0.0.1",
"description": "cache",
"version": "0.0.2",
"description": "function cache",
"main": "lib/index.js",
"types": "src/index.d.ts",
"web": "dist/cache.js",
"module": "es/index.js",
"jsnext:main": "es/index.js",
"files": [
"lib",
"dist",
"es"
],
"author": "nianqin <nianqin.vip@gmail.com> (https://github.com/nqdy666/)",
"contributors": [
{
"name": "nianqin",
"url": "https://github.com/nqdy666"
}
],
"bugs": {
"url": "https://github.com/nqdy666/nq-cache/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/nqdy666/nq-cache.git"
},
"license": "MIT",
"scripts": {
"test": "jest --no-cache",
"test:unit": "jest --no-cache",
"test:e2e": "npm run build && node test/e2e/runner.js",
"test:e2e:sauce": "npm run test:e2e -- --env chrome,firefox,ie11,ie10,ie9,ie8,edge,safari,iphone6,android4.4",
"test": "npm run test:unit && npm run test:e2e -- --env phantomjs",
"example": "serve ./examples",
"flow": "flow",

@@ -17,26 +44,31 @@ "prelint": "npm run flow",

"log": "conventional-changelog -p eslint -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md && git commit -m \"Update CHANGELOG.md\"",
"compile:cjs": "cross-env NODE-ENV=production && babel src -d lib --no-babelrc --presets=@babel/preset-env,@babel/preset-stage-0,@babel/preset-flow",
"compile:es": "cross-env NODE-ENV=production && babel src -d es --no-babelrc --presets=@babel/preset-stage-0,@babel/preset-flow",
"postversion": "npm run log"
"compile:cjs": "cross-env NODE-ENV=production && babel src -d lib --no-babelrc --presets=@babel/preset-env,@babel/preset-flow",
"compile:es": "cross-env NODE-ENV=production && babel src -d es --no-babelrc --presets=@babel/preset-flow",
"postversion": "npm run log",
"codecov": "codecov"
},
"repository": {
"type": "git",
"url": "https://github.com/nqdy666/nq-cache"
"dependencies": {
"nd-storage": "^3.0.0"
},
"keywords": [
"cache"
],
"files": [
"lib",
"dist",
"es"
],
"author": "nianqin <nianqin.vip@gmail.com> (https://qjzd.net/)",
"license": "MIT",
"devDependencies": {
"@babel/cli": "^7.0.0-beta.42",
"@babel/core": "^7.0.0-beta.42",
"@babel/preset-env": "^7.0.0-beta.42",
"@babel/preset-flow": "^7.0.0-beta.42",
"@babel/preset-stage-0": "^7.0.0-beta.42",
"@babel/cli": "^7.2.3",
"@babel/core": "^7.3.4",
"@babel/plugin-proposal-class-properties": "^7.0.0",
"@babel/plugin-proposal-decorators": "^7.0.0",
"@babel/plugin-proposal-do-expressions": "^7.0.0",
"@babel/plugin-proposal-export-default-from": "^7.0.0",
"@babel/plugin-proposal-export-namespace-from": "^7.0.0",
"@babel/plugin-proposal-function-bind": "^7.0.0",
"@babel/plugin-proposal-function-sent": "^7.0.0",
"@babel/plugin-proposal-json-strings": "^7.0.0",
"@babel/plugin-proposal-logical-assignment-operators": "^7.0.0",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.0.0",
"@babel/plugin-proposal-numeric-separator": "^7.0.0",
"@babel/plugin-proposal-optional-chaining": "^7.0.0",
"@babel/plugin-proposal-pipeline-operator": "^7.0.0",
"@babel/plugin-proposal-throw-expressions": "^7.0.0",
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/plugin-syntax-import-meta": "^7.0.0",
"@babel/preset-env": "^7.3.4",
"@babel/preset-flow": "^7.0.0",
"babel-core": "^7.0.0-bridge.0",

@@ -47,5 +79,8 @@ "babel-eslint": "^8.2.2",

"babel-preset-env": "^1.7.0",
"conventional-changelog-cli": "^1.3.17",
"chromedriver": "^2.30.1",
"codecov": "^3.0.2",
"conventional-changelog-cli": "^2.0.12",
"cross-env": "^5.1.4",
"esdoc": "^1.0.4",
"cross-spawn": "^6.0.5",
"esdoc": "^1.1.0",
"esdoc-custom-theme": "^1.4.2",

@@ -55,4 +90,4 @@ "esdoc-ecmascript-proposal-plugin": "^1.0.0",

"esdoc-inject-style-plugin": "^1.0.0",
"esdoc-publish-html-plugin": "^1.1.0",
"esdoc-publish-markdown-plugin": "^1.0.0",
"esdoc-publish-html-plugin": "^1.1.2",
"esdoc-publish-markdown-plugin": "^1.1.0",
"esdoc-standard-plugin": "^1.0.0",

@@ -69,18 +104,29 @@ "eslint": "^4.19.1",

"flow-remove-types": "^1.2.3",
"jest": "^22.4.3",
"http-server": "^0.11.1",
"jest": "^24.5.0",
"jest-localstorage-mock": "^2.2.0",
"rollup": "^0.56.5",
"nightwatch": "^1.0.19",
"nightwatch-helpers": "^1.2.0",
"phantomjs-prebuilt": "^2.1.16",
"rollup": "^1.7.0",
"rollup-plugin-alias": "^1.4.0",
"rollup-plugin-babel": "^4.0.0-beta.3",
"rollup-plugin-babel": "^4.3.2",
"rollup-plugin-commonjs": "^9.1.0",
"rollup-plugin-flow": "^1.1.1",
"rollup-plugin-node-resolve": "^3.3.0",
"rollup-plugin-node-resolve": "^4.0.1",
"rollup-plugin-prettier": "^0.4.0",
"rollup-plugin-replace": "^2.0.0",
"rollup-plugin-uglify": "^3.0.0"
"rollup-plugin-uglify": "^6.0.2",
"selenium-server": "^2.53.1",
"serve": "^10.1.2"
},
"browserslist": "last 2 versions, Android >= 4, iOS 10, not IE <= 8",
"dependencies": {
"nd-storage": "^3.0.0"
}
"browserslist": "last 2 versions, Android >= 4, iOS 10, not IE < 8",
"keywords": [
"cache",
"nq-cache",
"function",
"pure-function",
"memory",
"storage"
]
}
NQ-CACHE
=
> 函数缓存
[![build status](https://api.travis-ci.org/nqdy666/nq-cache.svg?branch=master)](https://travis-ci.org/nqdy666/nq-cache)
[![codecov](https://codecov.io/gh/nqdy666/nq-cache/branch/master/graph/badge.svg)](https://codecov.io/gh/nqdy666/nq-cache)
[![node version](https://img.shields.io/badge/node.js-%3E=_8.0-green.svg?style=flat-square)](http://nodejs.org/download/)
[![David deps](https://img.shields.io/david/nqdy666/nq-cache.svg?style=flat-square)](https://david-dm.org/nqdy666/nq-cache)
[![license](https://img.shields.io/npm/l/nq-cache.svg)](https://www.npmjs.com/package/nq-cache)
[![Build Status](https://saucelabs.com/browser-matrix/nqdy666.svg)](https://saucelabs.com/beta/builds/1997be72b34e41228522a3a3e065d993)
## 特性
- IE8+
- 支持Typescript
- 支持纯函数缓存
- 支持返回值Promise的函数缓存
- 支持缓存到sessionStorage
## 文档
- [Example on JSBin](https://jsbin.com/baluray/edit?html,js,output)
## 安装
安装npm包
```bash

@@ -10,31 +31,105 @@ npm install nq-cache

## 使用
使用 `pureFuncMemoryCache`
- `ES Module`
add.js
```javascript
import { pureFuncMemoryCache } from 'nq-cache'
export function add (a, b) {
return a + b
}
export const addCache = pureFuncMemoryCache(add)
```
app.js
```javascript
import { promiseMemoryCache, promiseSessionStorageCache, pureFuncMemoryCache } from 'nq-cache'
import { addCache as add } from './add'
add(1, 2) // 执行,并把结果缓存
add(1, 2) // 直接从缓存中获取结果
```
- `commonjs`
使用 `promiseMemoryCache`
request.js
```javascript
const { promiseMemoryCache, promiseSessionStorageCache, pureFuncMemoryCache } = require('nq-cache')
import { promiseMemoryCache } from 'nq-cache'
export function request (data) {
return new Promise(resolve => {
setTimeout(() => {
resolve(data)
}, 2 * 1000)
})
}
export const requestCache = promiseMemoryCache(request)
```
- `UMD`(未压缩且保留注释,需自行引入 `uglify` 处理)
app.js
```javascript
import { requestCache as request } from './request'
// 执行,并把结果缓存
request({ name: 'bowl' }).then(res => {
// 直接从缓存中获取结果
return request({ name: 'bowl' })
})
```
使用 `promiseSessionStorageCache`
request.js
```javascript
const { promiseMemoryCache, promiseSessionStorageCache, pureFuncMemoryCache } = require('nq-cache/dist/nq-cache')
import { promiseSessionStorageCache } from 'nq-cache'
export function request (data) {
return new Promise(resolve => {
setTimeout(() => {
resolve(data)
}, 2 * 1000)
})
}
export const requestCache = promiseSessionStorageCache(request, 'request')
```
- `iife`
app.js
```javascript
import { requestCache as request } from './request'
// 执行,并把结果缓存
request({ name: 'bowl' }).then(res => {
// 直接从缓存中获取结果
return request({ name: 'bowl' })
})
```
#### CDN
仅包含 `nq-cache`
```html
<script type="text/javascript" href="nq-cache/dist/nq-cache.min.js"></script>
<!-- 使用最新版本 -->
<script src="https://unpkg.com/nq-cache@latest"></script>
<!-- 或指定某一个版本 -->
<script src="https://unpkg.com/nq-cache@0.0.2"></script>
<script>
var cache = window.cache
function add (a, b) {
return a + b
}
addCache = cache.pureFuncMemoryCache(add)
addCache(1, 2) // 执行,并把结果缓存
addCache(1, 2) // 直接从缓存中获取结果
</script>
```
其他更多的方法,可以查看[例子](https://jsbin.com/baluray/edit?html,js,output)
提示,如果浏览器不支持 Promise 或者 JSON,你应该进行 polyfill
```html
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"></script>
<!--[if lt IE 8]>
<script type="text/javascript" src="https://cdn.bootcss.com/json2/20160511/json2.min.js"></script>
<![endif]-->
```
## 本地开发

@@ -41,0 +136,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc