Skip to content

Commit

Permalink
✨ Add key calculation parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
zswang committed Sep 26, 2018
1 parent 7bad653 commit 453806b
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 58 deletions.
34 changes: 22 additions & 12 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,24 @@ export interface ICacheOptions<T> {
*/
expire?: number;
/**
* 获取数据的方法
* 获取数据
* @param query 查询条件
*/
fetch: {
(key?: string | number): Promise<T>;
};
fetch(query?: any): Promise<T>;
/**
* 计算 hash 值
* @param query 查询条件
*/
hash?(query?: any): string;
}
/**
* @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
*/
export declare class Cache<T> {
/**
Expand All @@ -54,10 +58,16 @@ export declare class Cache<T> {
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)
Expand Down Expand Up @@ -189,11 +199,11 @@ export declare class Cache<T> {
```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 => {
Expand Down Expand Up @@ -244,7 +254,7 @@ export declare class Cache<T> {
}, 100)
```
*/
fetch(key?: string | number): Promise<T>;
fetch(query?: any): Promise<T>;
/**
* 移除缓存 Remove cached data
* @param key 缓存标志,默认: ''
Expand Down
41 changes: 28 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -235,17 +249,18 @@ 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 + ")")
: '';
// 数据正在获取中
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] || [];
Expand All @@ -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)
Expand Down
51 changes: 35 additions & 16 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ICacheStore } from 'jfetchs-util'
import { MemoryStore } from 'jfetchs-memory'
import { createHash } from 'crypto'
export { MemoryStore }
export interface ICacheOptions<T> {
/**
Expand All @@ -15,20 +16,24 @@ export interface ICacheOptions<T> {
*/
expire?: number
/**
* 获取数据的方法
* 获取数据
* @param query 查询条件
*/
fetch: {
(key?: string | number): Promise<T>
}
fetch(query?: any): Promise<T>
/**
* 计算 hash 值
* @param query 查询条件
*/
hash?(query?: any): string
}
/**
* @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
*/
export class Cache<T> {
/**
Expand Down Expand Up @@ -57,6 +62,13 @@ export class Cache<T> {
if (!this.options.store) {
this.options.store = new MemoryStore<T>()
}
if (!this.options.hash) {
this.options.hash = query => {
return createHash('sha1')
.update(JSON.stringify(query))
.digest('hex')
}
}
}
/**
* 获取数据 Fetch cached data
Expand All @@ -68,10 +80,16 @@ export class Cache<T> {
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)
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -258,7 +276,8 @@ setTimeout(() => {
}, 100)
```
*/
fetch(key: string | number = ''): Promise<T> {
fetch(query: any = ''): Promise<T> {
let key = this.options.hash(query)
// 日志前缀
const prefix =
typeof this.options.debug === 'string'
Expand All @@ -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] || []
Expand All @@ -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)
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": "0.1.27",
"version": "1.0.0",
"description": "Cache of fetch data",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
Loading

0 comments on commit 453806b

Please sign in to comment.