diff --git a/lib/index.d.ts b/lib/index.d.ts index 47e0627..c210805 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -15,11 +15,15 @@ export interface ICacheOptions { */ expire?: number; /** - * 获取数据的方法 + * 获取数据 + * @param query 查询条件 */ - fetch: { - (key?: string | number): Promise; - }; + fetch(query?: any): Promise; + /** + * 计算 hash 值 + * @param query 查询条件 + */ + hash?(query?: any): string; } /** * @file jfetchs @@ -27,8 +31,8 @@ export interface ICacheOptions { * Cache of fetch data * @author * zswang (http://weibo.com/zswang) - * @version 0.1.27 - * @date 2018-09-16 + * @version 1.0.0 + * @date 2018-09-26 */ export declare class Cache { /** @@ -54,10 +58,16 @@ export declare class Cache { expire: 1, fetch: (() => { let count = 0 - return key => { - return Promise.resolve(`cache1 ${key}${count++}`) + return query => { + return Promise.resolve(`cache1 ${query}${count++}`) } })(), + hash: query => { + if (['string', 'number', 'boolean'].includes(typeof query)) { + return String(query) + } + return JSON.stringify(query) + }, }) cache1.fetch('c').then(data => { console.log(data) @@ -189,11 +199,11 @@ export declare class Cache { ```js let cache6 = new jfetchs.Cache({ debug: true, - fetch: key => { - if (key === 6) { + fetch: query => { + if (query === 6) { return Promise.resolve(666) } - return Promise.reject(`cache6 ${key} error`) + return Promise.reject(`cache6 ${query} error`) }, }) cache6.fetch('ok').catch(err => { @@ -244,7 +254,7 @@ export declare class Cache { }, 100) ``` */ - fetch(key?: string | number): Promise; + fetch(query?: any): Promise; /** * 移除缓存 Remove cached data * @param key 缓存标志,默认: '' diff --git a/lib/index.js b/lib/index.js index 78be3a3..a9b8feb 100644 --- a/lib/index.js +++ b/lib/index.js @@ -11,14 +11,15 @@ var __assign = (this && this.__assign) || Object.assign || function(t) { Object.defineProperty(exports, "__esModule", { value: true }); var jfetchs_memory_1 = require("jfetchs-memory"); exports.MemoryStore = jfetchs_memory_1.MemoryStore; +var crypto_1 = require("crypto"); /** * @file jfetchs * * Cache of fetch data * @author * zswang (http://weibo.com/zswang) - * @version 0.1.27 - * @date 2018-09-16 + * @version 1.0.0 + * @date 2018-09-26 */ var Cache = /** @class */ (function () { function Cache(options) { @@ -34,6 +35,13 @@ var Cache = /** @class */ (function () { if (!this.options.store) { this.options.store = new jfetchs_memory_1.MemoryStore(); } + if (!this.options.hash) { + this.options.hash = function (query) { + return crypto_1.createHash('sha1') + .update(JSON.stringify(query)) + .digest('hex'); + }; + } } /** * 获取数据 Fetch cached data @@ -45,10 +53,16 @@ var Cache = /** @class */ (function () { expire: 1, fetch: (() => { let count = 0 - return key => { - return Promise.resolve(`cache1 ${key}${count++}`) + return query => { + return Promise.resolve(`cache1 ${query}${count++}`) } })(), + hash: query => { + if (['string', 'number', 'boolean'].includes(typeof query)) { + return String(query) + } + return JSON.stringify(query) + }, }) cache1.fetch('c').then(data => { console.log(data) @@ -180,11 +194,11 @@ var Cache = /** @class */ (function () { ```js let cache6 = new jfetchs.Cache({ debug: true, - fetch: key => { - if (key === 6) { + fetch: query => { + if (query === 6) { return Promise.resolve(666) } - return Promise.reject(`cache6 ${key} error`) + return Promise.reject(`cache6 ${query} error`) }, }) cache6.fetch('ok').catch(err => { @@ -235,9 +249,10 @@ var Cache = /** @class */ (function () { }, 100) ``` */ - Cache.prototype.fetch = function (key) { + Cache.prototype.fetch = function (query) { var _this = this; - if (key === void 0) { key = ''; } + if (query === void 0) { query = ''; } + var key = this.options.hash(query); // 日志前缀 var prefix = typeof this.options.debug === 'string' ? " " + JSON.stringify(this.options.debug) + (key === '' ? '' : "(" + key + ")") @@ -245,7 +260,7 @@ var Cache = /** @class */ (function () { // 数据正在获取中 if (this.fetching[key]) { if (this.options.debug) { - console.log("jfetchs/src/index.ts:115" + prefix + " fetching in queue"); + console.log("jfetchs/src/index.ts:129" + prefix + " fetching in queue"); } return new Promise(function (resolve, reject) { _this.queue[key] = _this.queue[key] || []; @@ -260,17 +275,17 @@ var Cache = /** @class */ (function () { return new Promise(function (resolve, reject) { if (data !== undefined) { if (_this.options.debug) { - console.log("jfetchs/src/index.ts:131" + prefix + " hitting cache"); + console.log("jfetchs/src/index.ts:145" + prefix + " hitting cache"); } _this.fetching[key] = false; return resolve(data); } if (_this.options.debug) { - console.log("jfetchs/src/index.ts:138" + prefix + " missing cache"); + console.log("jfetchs/src/index.ts:152" + prefix + " missing cache"); } _this.flush(key); _this.options - .fetch(key) + .fetch(query) .then(function (data) { return _this.options.store .save(key, data, _this.options.expire) diff --git a/lib/index.ts b/lib/index.ts index fdd4fd2..f9a28e2 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,5 +1,6 @@ import { ICacheStore } from 'jfetchs-util' import { MemoryStore } from 'jfetchs-memory' +import { createHash } from 'crypto' export { MemoryStore } export interface ICacheOptions { /** @@ -15,11 +16,15 @@ export interface ICacheOptions { */ expire?: number /** - * 获取数据的方法 + * 获取数据 + * @param query 查询条件 */ - fetch: { - (key?: string | number): Promise - } + fetch(query?: any): Promise + /** + * 计算 hash 值 + * @param query 查询条件 + */ + hash?(query?: any): string } /** * @file jfetchs @@ -27,8 +32,8 @@ export interface ICacheOptions { * Cache of fetch data * @author * zswang (http://weibo.com/zswang) - * @version 0.1.27 - * @date 2018-09-16 + * @version 1.0.0 + * @date 2018-09-26 */ export class Cache { /** @@ -57,6 +62,13 @@ export class Cache { if (!this.options.store) { this.options.store = new MemoryStore() } + if (!this.options.hash) { + this.options.hash = query => { + return createHash('sha1') + .update(JSON.stringify(query)) + .digest('hex') + } + } } /** * 获取数据 Fetch cached data @@ -68,10 +80,16 @@ export class Cache { expire: 1, fetch: (() => { let count = 0 - return key => { - return Promise.resolve(`cache1 ${key}${count++}`) + return query => { + return Promise.resolve(`cache1 ${query}${count++}`) } })(), + hash: query => { + if (['string', 'number', 'boolean'].includes(typeof query)) { + return String(query) + } + return JSON.stringify(query) + }, }) cache1.fetch('c').then(data => { console.log(data) @@ -203,11 +221,11 @@ cache5.fetch(8).catch(err => { ```js let cache6 = new jfetchs.Cache({ debug: true, - fetch: key => { - if (key === 6) { + fetch: query => { + if (query === 6) { return Promise.resolve(666) } - return Promise.reject(`cache6 ${key} error`) + return Promise.reject(`cache6 ${query} error`) }, }) cache6.fetch('ok').catch(err => { @@ -258,7 +276,8 @@ setTimeout(() => { }, 100) ``` */ - fetch(key: string | number = ''): Promise { + fetch(query: any = ''): Promise { + let key = this.options.hash(query) // 日志前缀 const prefix = typeof this.options.debug === 'string' @@ -269,7 +288,7 @@ setTimeout(() => { // 数据正在获取中 if (this.fetching[key]) { if (this.options.debug) { - console.log(`jfetchs/src/index.ts:115${prefix} fetching in queue`) + console.log(`jfetchs/src/index.ts:129${prefix} fetching in queue`) } return new Promise((resolve, reject) => { this.queue[key] = this.queue[key] || [] @@ -284,17 +303,17 @@ setTimeout(() => { return new Promise((resolve, reject) => { if (data !== undefined) { if (this.options.debug) { - console.log(`jfetchs/src/index.ts:131${prefix} hitting cache`) + console.log(`jfetchs/src/index.ts:145${prefix} hitting cache`) } this.fetching[key] = false return resolve(data) } if (this.options.debug) { - console.log(`jfetchs/src/index.ts:138${prefix} missing cache`) + console.log(`jfetchs/src/index.ts:152${prefix} missing cache`) } this.flush(key) this.options - .fetch(key) + .fetch(query) .then(data => { return this.options.store .save(key, data, this.options.expire) diff --git a/package.json b/package.json index fcd1e08..c84181d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jfetchs", - "version": "0.1.27", + "version": "1.0.0", "description": "Cache of fetch data", "main": "lib/index.js", "typings": "lib/index.d.ts", diff --git a/src/index.ts b/src/index.ts index 8c534fc..23eb0c8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import { ICacheStore } from 'jfetchs-util' import { MemoryStore } from 'jfetchs-memory' +import { createHash } from 'crypto' export { MemoryStore } export interface ICacheOptions { @@ -16,11 +17,15 @@ export interface ICacheOptions { */ expire?: number /** - * 获取数据的方法 + * 获取数据 + * @param query 查询条件 */ - fetch: { - (key?: string | number): Promise - } + fetch(query?: any): Promise + /** + * 计算 hash 值 + * @param query 查询条件 + */ + hash?(query?: any): string } /**/ @@ -70,6 +75,13 @@ export class Cache { if (!this.options.store) { this.options.store = new MemoryStore() } + if (!this.options.hash) { + this.options.hash = query => { + return createHash('sha1') + .update(JSON.stringify(query)) + .digest('hex') + } + } } /** @@ -100,7 +112,9 @@ export class Cache { (**) ``` */ - fetch(key: string | number = ''): Promise { + fetch(query: any = ''): Promise { + let key = this.options.hash(query) + // 日志前缀 const prefix = typeof this.options.debug === 'string' @@ -140,7 +154,7 @@ export class Cache { this.flush(key) this.options - .fetch(key) + .fetch(query) .then(data => { return this.options.store .save(key, data, this.options.expire) @@ -190,10 +204,16 @@ let cache1 = new jfetchs.Cache({ expire: 1, fetch: (() => { let count = 0 - return key => { - return Promise.resolve(`cache1 ${key}${count++}`) + return query => { + return Promise.resolve(`cache1 ${query}${count++}`) } })(), + hash: query => { + if (['string', 'number', 'boolean'].includes(typeof query)) { + return String(query) + } + return JSON.stringify(query) + }, }) cache1.fetch('c').then(data => { @@ -343,11 +363,11 @@ cache5.fetch(8).catch(err => { /**/ let cache6 = new jfetchs.Cache({ debug: true, - fetch: key => { - if (key === 6) { + fetch: query => { + if (query === 6) { return Promise.resolve(666) } - return Promise.reject(`cache6 ${key} error`) + return Promise.reject(`cache6 ${query} error`) }, }) diff --git a/test/index.js b/test/index.js index c07b4d9..b4b7d3d 100644 --- a/test/index.js +++ b/test/index.js @@ -19,10 +19,16 @@ describe("src/index.ts", function () { expire: 1, fetch: (() => { let count = 0 - return key => { - return Promise.resolve(`cache1 ${key}${count++}`) + return query => { + return Promise.resolve(`cache1 ${query}${count++}`) } })(), + hash: query => { + if (['string', 'number', 'boolean'].includes(typeof query)) { + return String(query) + } + return JSON.stringify(query) + }, }) cache1.fetch('c').then(data => { examplejs_print(data) @@ -158,11 +164,11 @@ cache5.fetch(8).catch(err => { examplejs_printLines = []; let cache6 = new jfetchs.Cache({ debug: true, - fetch: key => { - if (key === 6) { + fetch: query => { + if (query === 6) { return Promise.resolve(666) } - return Promise.reject(`cache6 ${key} error`) + return Promise.reject(`cache6 ${query} error`) }, }) cache6.fetch('ok').catch(err => {