Skip to content

Commit

Permalink
✨ Add "key" parameter to fetch()
Browse files Browse the repository at this point in the history
  • Loading branch information
zswang committed Oct 13, 2018
1 parent eb2d396 commit da3b3d8
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 29 deletions.
15 changes: 10 additions & 5 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ export interface ICacheOptions<T> {
/**
* 获取数据
* @param query 查询条件
* @param key hash 键值
*/
fetch(query?: any): Promise<T>;
fetch(query?: any, key?: string): Promise<T>;
/**
* 计算 hash 值
* @param query 查询条件
Expand All @@ -31,8 +32,8 @@ export interface ICacheOptions<T> {
* Cache of fetch data
* @author
* zswang (http://weibo.com/zswang)
* @version 1.0.3
* @date 2018-09-28
* @version 1.0.4
* @date 2018-10-12
*/
export declare class Cache<T> {
/**
Expand Down Expand Up @@ -63,7 +64,7 @@ export declare class Cache<T> {
}
})(),
hash: query => {
if (['string', 'number', 'boolean'].includes(typeof query)) {
if (['string', 'number', 'boolean'].indexOf(typeof query) >= 0) {
return String(query)
}
return JSON.stringify(query)
Expand Down Expand Up @@ -164,7 +165,11 @@ export declare class Cache<T> {
```js
let cache4 = new jfetchs.Cache({
debug: true,
fetch: () => {
fetch: (query, key) => {
console.log(query)
// >
console.log(key)
// > dd29ecf524b030a65261e3059c48ab9e1ecb2585
return Promise.reject('cache4 error')
},
})
Expand Down
20 changes: 12 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ var crypto_1 = require("crypto");
* Cache of fetch data
* @author
* zswang (http://weibo.com/zswang)
* @version 1.0.3
* @date 2018-09-28
* @version 1.0.4
* @date 2018-10-12
*/
var Cache = /** @class */ (function () {
function Cache(options) {
Expand Down Expand Up @@ -58,7 +58,7 @@ var Cache = /** @class */ (function () {
}
})(),
hash: query => {
if (['string', 'number', 'boolean'].includes(typeof query)) {
if (['string', 'number', 'boolean'].indexOf(typeof query) >= 0) {
return String(query)
}
return JSON.stringify(query)
Expand Down Expand Up @@ -159,7 +159,11 @@ var Cache = /** @class */ (function () {
```js
let cache4 = new jfetchs.Cache({
debug: true,
fetch: () => {
fetch: (query, key) => {
console.log(query)
// >
console.log(key)
// > dd29ecf524b030a65261e3059c48ab9e1ecb2585
return Promise.reject('cache4 error')
},
})
Expand Down Expand Up @@ -260,7 +264,7 @@ var Cache = /** @class */ (function () {
// 数据正在获取中
if (this.fetching[key]) {
if (this.options.debug) {
console.log("jfetchs/src/index.ts:129" + prefix + " fetching in queue");
console.log("jfetchs/src/index.ts:130" + prefix + " fetching in queue");
}
return new Promise(function (resolve, reject) {
_this.queue[key] = _this.queue[key] || [];
Expand All @@ -275,17 +279,17 @@ var Cache = /** @class */ (function () {
return new Promise(function (resolve, reject) {
if (data !== undefined) {
if (_this.options.debug) {
console.log("jfetchs/src/index.ts:145" + prefix + " hitting cache");
console.log("jfetchs/src/index.ts:146" + prefix + " hitting cache");
}
_this.fetching[key] = false;
return resolve(data);
}
if (_this.options.debug) {
console.log("jfetchs/src/index.ts:152" + prefix + " missing cache");
console.log("jfetchs/src/index.ts:153" + prefix + " missing cache");
}
_this.flush(key);
_this.options
.fetch(query)
.fetch(query, key)
.then(function (data) {
return _this.options.store
.save(key, data, _this.options.expire)
Expand Down
23 changes: 14 additions & 9 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export interface ICacheOptions<T> {
/**
* 获取数据
* @param query 查询条件
* @param key hash 键值
*/
fetch(query?: any): Promise<T>
fetch(query?: any, key?: string): Promise<T>
/**
* 计算 hash 值
* @param query 查询条件
Expand All @@ -32,8 +33,8 @@ export interface ICacheOptions<T> {
* Cache of fetch data
* @author
* zswang (http://weibo.com/zswang)
* @version 1.0.3
* @date 2018-09-28
* @version 1.0.4
* @date 2018-10-12
*/
export class Cache<T> {
/**
Expand Down Expand Up @@ -85,7 +86,7 @@ export class Cache<T> {
}
})(),
hash: query => {
if (['string', 'number', 'boolean'].includes(typeof query)) {
if (['string', 'number', 'boolean'].indexOf(typeof query) >= 0) {
return String(query)
}
return JSON.stringify(query)
Expand Down Expand Up @@ -186,7 +187,11 @@ setTimeout(() => {
```js
let cache4 = new jfetchs.Cache({
debug: true,
fetch: () => {
fetch: (query, key) => {
console.log(query)
// >
console.log(key)
// > dd29ecf524b030a65261e3059c48ab9e1ecb2585
return Promise.reject('cache4 error')
},
})
Expand Down Expand Up @@ -288,7 +293,7 @@ setTimeout(() => {
// 数据正在获取中
if (this.fetching[key]) {
if (this.options.debug) {
console.log(`jfetchs/src/index.ts:129${prefix} fetching in queue`)
console.log(`jfetchs/src/index.ts:130${prefix} fetching in queue`)
}
return new Promise((resolve, reject) => {
this.queue[key] = this.queue[key] || []
Expand All @@ -303,17 +308,17 @@ setTimeout(() => {
return new Promise((resolve, reject) => {
if (data !== undefined) {
if (this.options.debug) {
console.log(`jfetchs/src/index.ts:145${prefix} hitting cache`)
console.log(`jfetchs/src/index.ts:146${prefix} hitting cache`)
}
this.fetching[key] = false
return resolve(data)
}
if (this.options.debug) {
console.log(`jfetchs/src/index.ts:152${prefix} missing cache`)
console.log(`jfetchs/src/index.ts:153${prefix} missing cache`)
}
this.flush(key)
this.options
.fetch(query)
.fetch(query, key)
.then(data => {
return this.options.store
.save(key, data, this.options.expire)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jfetchs",
"version": "1.0.3",
"version": "1.0.4",
"description": "Cache of fetch data",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
13 changes: 9 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ export interface ICacheOptions<T> {
/**
* 获取数据
* @param query 查询条件
* @param key hash 键值
*/
fetch(query?: any): Promise<T>
fetch(query?: any, key?: string): Promise<T>
/**
* 计算 hash 值
* @param query 查询条件
Expand Down Expand Up @@ -154,7 +155,7 @@ export class Cache<T> {

this.flush(key)
this.options
.fetch(query)
.fetch(query, key)
.then(data => {
return this.options.store
.save(key, data, this.options.expire)
Expand Down Expand Up @@ -210,7 +211,7 @@ let cache1 = new jfetchs.Cache({
}
})(),
hash: query => {
if (['string', 'number', 'boolean'].includes(typeof query)) {
if (['string', 'number', 'boolean'].indexOf(typeof query) >= 0) {
return String(query)
}
return JSON.stringify(query)
Expand Down Expand Up @@ -323,7 +324,11 @@ setTimeout(() => {
/*<debug desc="reject">*/
let cache4 = new jfetchs.Cache({
debug: true,
fetch: () => {
fetch: (query, key) => {
console.log(query)
// >
console.log(key)
// > dd29ecf524b030a65261e3059c48ab9e1ecb2585
return Promise.reject('cache4 error')
},
})
Expand Down
8 changes: 6 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe("src/index.ts", function () {
}
})(),
hash: query => {
if (['string', 'number', 'boolean'].includes(typeof query)) {
if (['string', 'number', 'boolean'].indexOf(typeof query) >= 0) {
return String(query)
}
return JSON.stringify(query)
Expand Down Expand Up @@ -128,7 +128,11 @@ setTimeout(() => {
examplejs_printLines = [];
let cache4 = new jfetchs.Cache({
debug: true,
fetch: () => {
fetch: (query, key) => {
examplejs_print(query)
// >
examplejs_print(key)
assert.equal(examplejs_printLines.join("\n"), "dd29ecf524b030a65261e3059c48ab9e1ecb2585"); examplejs_printLines = [];
return Promise.reject('cache4 error')
},
})
Expand Down

0 comments on commit da3b3d8

Please sign in to comment.