mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-24 05:06:02 +08:00
adding typings for node-cache-manager lib
This commit is contained in:
51
cache-manager/cache-manager-tests.ts
Normal file
51
cache-manager/cache-manager-tests.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
/// <reference path="cache-manager.d.ts" />
|
||||
|
||||
import * as cacheManager from 'cache-manager'
|
||||
|
||||
const memoryCache = cacheManager.caching({store: 'memory', max: 100, ttl: 10/*seconds*/});
|
||||
const ttl = 5;
|
||||
|
||||
memoryCache.set('foo', 'bar', {ttl: ttl}, (err) => {
|
||||
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
memoryCache.get('foo', (err, result) => {
|
||||
|
||||
// console.log(result);
|
||||
|
||||
memoryCache.del('foo', (err) => {
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
function getUser(id:number, cb:Function) {
|
||||
|
||||
cb(null, {id: id, name: 'Bob'});
|
||||
}
|
||||
|
||||
const userId = 123;
|
||||
const key = 'user_' + userId;
|
||||
|
||||
// Note: ttl is optional in wrap()
|
||||
memoryCache.wrap<{id: number, name: string}>(key, (cb) => {
|
||||
|
||||
getUser(userId, cb);
|
||||
|
||||
}, {ttl: ttl}, (err, user) => {
|
||||
|
||||
//console.log(user);
|
||||
|
||||
// Second time fetches user from memoryCache
|
||||
memoryCache.wrap<{id: number, name: string}>(key, (cb)=> {
|
||||
|
||||
getUser(userId, cb);
|
||||
|
||||
}, (err, user) => {
|
||||
|
||||
//console.log(user);
|
||||
|
||||
});
|
||||
});
|
||||
36
cache-manager/cache-manager.d.ts
vendored
Normal file
36
cache-manager/cache-manager.d.ts
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
// Type definitions for cache-manager v1.2.0
|
||||
// Project: https://github.com/BryanDonovan/node-cache-manager
|
||||
// Definitions by: Simon Gausmann www.gausmann-media.de
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
declare module 'cache-manager' {
|
||||
|
||||
|
||||
interface ICachingConfig {
|
||||
ttl: number;
|
||||
}
|
||||
interface IStoreConfig extends ICachingConfig {
|
||||
store:string;
|
||||
max?: number;
|
||||
isCacheableValue?:(value:any)=>boolean;
|
||||
}
|
||||
interface ICache {
|
||||
set<T>(key:string, value:T, options:ICachingConfig, callback?:(error:any)=>void):void;
|
||||
set<T>(key:string, value:T, ttl:number, callback?:(error:any)=>void):void;
|
||||
|
||||
wrap<T>(key:string, wrapper:(callback:(error:any, result:T)=>void)=>void, options:ICachingConfig, callback:(error:any, result:T)=>void):void;
|
||||
wrap<T>(key:string, wrapper:(callback:(error:any, result:T)=>void)=>void, callback:(error:any, result:T)=>void):void;
|
||||
|
||||
get<T>(key:string, callback:(error:any, result:T)=>void):void;
|
||||
|
||||
del(key:string, callback?:(error:any)=>void):void;
|
||||
}
|
||||
|
||||
|
||||
module cacheManager {
|
||||
function caching(ICongig:IStoreConfig):ICache;
|
||||
|
||||
function multiCaching(Caches:ICache[]):ICache;
|
||||
}
|
||||
|
||||
export = cacheManager;
|
||||
}
|
||||
Reference in New Issue
Block a user