index.d.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // Type definitions for node-cache 4.1
  2. // Project: https://github.com/tcs-de/nodecache
  3. // Definitions by: Ilya Mochalov <https://github.com/chrootsu>
  4. // Daniel Thunell <https://github.com/dthunell>
  5. // Ulf Seltmann <https://github.com/useltmann>
  6. // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
  7. /// <reference types="node" />
  8. /**
  9. * Since 4.1.0: Key-validation: The keys can be given as either string or number,
  10. * but are casted to a string internally anyway.
  11. */
  12. type Key = string | number;
  13. declare namespace NodeCache {
  14. interface NodeCache {
  15. /** container for cached data */
  16. data: Data;
  17. /** module options */
  18. options: Options;
  19. /** statistics container */
  20. stats: Stats;
  21. /**
  22. * get a cached key and change the stats
  23. *
  24. * @param key cache key or an array of keys
  25. * @param cb Callback function
  26. */
  27. get<T>(
  28. key: Key,
  29. cb?: Callback<T>
  30. ): T | undefined;
  31. /**
  32. * get multiple cached keys at once and change the stats
  33. *
  34. * @param keys an array of keys
  35. * @param cb Callback function
  36. */
  37. mget<T>(
  38. keys: Key[],
  39. cb?: Callback<{ [key: string]: T }>
  40. ): { [key: string]: T };
  41. /**
  42. * set a cached key and change the stats
  43. *
  44. * @param key cache key
  45. * @param value A element to cache. If the option `option.forceString` is `true` the module trys to translate
  46. * it to a serialized JSON
  47. * @param ttl The time to live in seconds.
  48. * @param cb Callback function
  49. */
  50. set<T>(
  51. key: Key,
  52. value: T,
  53. ttl: number | string,
  54. cb?: Callback<boolean>
  55. ): boolean;
  56. set<T>(
  57. key: Key,
  58. value: T,
  59. cb?: Callback<boolean>
  60. ): boolean;
  61. /**
  62. * remove keys
  63. * @param keys cache key to delete or a array of cache keys
  64. * @param cb Callback function
  65. * @returns Number of deleted keys
  66. */
  67. del(
  68. keys: Key | Key[],
  69. cb?: Callback<number>
  70. ): number;
  71. /**
  72. * reset or redefine the ttl of a key. If `ttl` is not passed or set to 0 it's similar to `.del()`
  73. */
  74. ttl(
  75. key: Key,
  76. ttl: number,
  77. cb?: Callback<boolean>
  78. ): boolean;
  79. ttl(
  80. key: Key,
  81. cb?: Callback<boolean>
  82. ): boolean;
  83. getTtl(
  84. key: Key,
  85. ): number|undefined;
  86. getTtl(
  87. key: Key,
  88. cb?: Callback<boolean>
  89. ): boolean;
  90. /**
  91. * list all keys within this cache
  92. * @param cb Callback function
  93. * @returns An array of all keys
  94. */
  95. keys(cb?: Callback<string[]>): string[];
  96. /**
  97. * get the stats
  98. *
  99. * @returns Stats data
  100. */
  101. getStats(): Stats;
  102. /**
  103. * flush the whole data and reset the stats
  104. */
  105. flushAll(): void;
  106. /**
  107. * This will clear the interval timeout which is set on checkperiod option.
  108. */
  109. close(): void;
  110. }
  111. interface Data {
  112. [key: string]: WrappedValue<any>;
  113. }
  114. interface Options {
  115. forceString?: boolean;
  116. objectValueSize?: number;
  117. arrayValueSize?: number;
  118. stdTTL?: number;
  119. checkperiod?: number;
  120. useClones?: boolean;
  121. errorOnMissing?: boolean;
  122. deleteOnExpire?: boolean;
  123. }
  124. interface Stats {
  125. hits: number;
  126. misses: number;
  127. keys: number;
  128. ksize: number;
  129. vsize: number;
  130. }
  131. interface WrappedValue<T> {
  132. // ttl
  133. t: number;
  134. // value
  135. v: T;
  136. }
  137. type Callback<T> = (err: any, data: T | undefined) => void;
  138. }
  139. import events = require("events");
  140. import Data = NodeCache.Data;
  141. import Options = NodeCache.Options;
  142. import Stats = NodeCache.Stats;
  143. import Callback = NodeCache.Callback;
  144. declare class NodeCache extends events.EventEmitter implements NodeCache.NodeCache {
  145. /** container for cached data */
  146. data: Data;
  147. /** module options */
  148. options: Options;
  149. /** statistics container */
  150. stats: Stats;
  151. constructor(options?: Options);
  152. /**
  153. * get a cached key and change the stats
  154. *
  155. * @param key cache key or an array of keys
  156. * @param cb Callback function
  157. */
  158. get<T>(
  159. key: Key,
  160. cb?: Callback<T>
  161. ): T | undefined;
  162. /**
  163. * get multiple cached keys at once and change the stats
  164. *
  165. * @param keys an array of keys
  166. * @param cb Callback function
  167. */
  168. mget<T>(
  169. keys: Key[],
  170. cb?: Callback<{ [key: string]: T }>
  171. ): { [key: string]: T };
  172. /**
  173. * set a cached key and change the stats
  174. *
  175. * @param key cache key
  176. * @param value A element to cache. If the option `option.forceString` is `true` the module trys to translate
  177. * it to a serialized JSON
  178. * @param ttl The time to live in seconds.
  179. * @param cb Callback function
  180. */
  181. set<T>(
  182. key: Key,
  183. value: T,
  184. ttl: number | string,
  185. cb?: Callback<boolean>
  186. ): boolean;
  187. set<T>(
  188. key: Key,
  189. value: T,
  190. cb?: Callback<boolean>
  191. ): boolean;
  192. /**
  193. * remove keys
  194. * @param keys cache key to delete or a array of cache keys
  195. * @param cb Callback function
  196. * @returns Number of deleted keys
  197. */
  198. del(
  199. keys: Key | Key[],
  200. cb?: Callback<number>
  201. ): number;
  202. /**
  203. * reset or redefine the ttl of a key. If `ttl` is not passed or set to 0 `stdTtl` is used. if set lt 0 it's similar to `.del()`
  204. */
  205. ttl(
  206. key: Key,
  207. ttl: number,
  208. cb?: Callback<boolean>
  209. ): boolean;
  210. ttl(
  211. key: Key,
  212. cb?: Callback<boolean>
  213. ): boolean;
  214. getTtl(
  215. key: Key
  216. ): number|undefined;
  217. getTtl(
  218. key: Key,
  219. cb?: Callback<boolean>,
  220. ): boolean;
  221. /**
  222. * list all keys within this cache
  223. * @param cb Callback function
  224. * @returns An array of all keys
  225. */
  226. keys(cb?: Callback<string[]>): string[];
  227. /**
  228. * get the stats
  229. *
  230. * @returns Stats data
  231. */
  232. getStats(): Stats;
  233. /**
  234. * flush the hole data and reset the stats
  235. */
  236. flushAll(): void;
  237. /**
  238. * This will clear the interval timeout which is set on checkperiod option.
  239. */
  240. close(): void;
  241. }
  242. export = NodeCache;