Skip to content

Commit 07f528b

Browse files
committed
node-cache - updating readme for NodeCacheStore
1 parent 30318e3 commit 07f528b

File tree

2 files changed

+35
-43
lines changed

2 files changed

+35
-43
lines changed

packages/node-cache/README.md

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ The `NodeCacheStore` is a class that extends the `NodeCache` and adds the abilit
8686
import {NodeCacheStore} from '@cacheable/node-cache';
8787

8888
const cache = new NodeCacheStore();
89-
cache.set('foo', 'bar');
90-
cache.get('foo'); // 'bar'
89+
await cache.set('foo', 'bar');
90+
await cache.get('foo'); // 'bar'
9191
```
9292

9393
## NodeCacheStoreOptions
@@ -108,8 +108,8 @@ Note: the `ttl` is now in milliseconds and not seconds like `stdTTL` in `NodeCac
108108

109109
```javascript
110110
const cache = new NodeCacheStore({ttl: 60000 }); // 1 minute as it defaults to milliseconds
111-
cache.set('foo', 'bar', '1h'); // 1 hour
112-
cache.set('longfoo', 'bar', '1d'); // 1 day
111+
await cache.set('foo', 'bar', '1h'); // 1 hour
112+
await cache.set('longfoo', 'bar', '1d'); // 1 day
113113
```
114114

115115
## Node Cache Store API
@@ -161,20 +161,20 @@ cache.on('expired', (key, value) => {
161161
});
162162
```
163163

164-
## `.set(key: string | number, value: any, ttl?: number): boolean`
164+
## `.set(key: string | number, value: any, ttl?: number): Promise<boolean>`
165165

166166
Set a key value pair with an optional ttl (in seconds). Will return true on success. If the ttl is not set it will default to 0 (no ttl).
167167

168168
```javascript
169-
cache.set('foo', 'bar', 10); // true
169+
await cache.set('foo', 'bar', 10); // true
170170
```
171171

172-
## `.mset(data: Array<NodeCacheItem>): boolean`
172+
## `.mset(data: Array<NodeCacheItem>): Promise<boolean>`
173173

174174
Set multiple key value pairs at once. This will take an array of objects with the key, value, and optional ttl.
175175

176176
```javascript
177-
cache.mset([{key: 'foo', value: 'bar', ttl: 10}, {key: 'bar', value: 'baz'}]); // true
177+
await cache.mset([{key: 'foo', value: 'bar', ttl: 10}, {key: 'bar', value: 'baz'}]); // true
178178
```
179179

180180
the `NodeCacheItem` is defined as:
@@ -187,89 +187,89 @@ export type NodeCacheItem = {
187187
};
188188
```
189189

190-
## `.get(key: string | number): any`
190+
## `.get(key: string | number): Promise<any>`
191191

192192
Get a value from the cache by key. If the key does not exist it will return `undefined`.
193193

194194
```javascript
195195
cache.get('foo'); // 'bar'
196196
```
197197

198-
## `mget(keys: Array<string | number>): Record<string, unknown>`
198+
## `mget(keys: Array<string | number>): Promise<Record<string, unknown>>`
199199

200200
Get multiple values from the cache by keys. This will return an object with the keys and values.
201201

202202
```javascript
203203
const obj = { my: 'value', my2: 'value2' };
204204
const obj2 = { special: 'value3', life: 'value4' };
205-
cache.set('my', obj);
206-
cache.set('my2', obj2);
207-
cache.mget(['my', 'my2']); // { my: { my: 'value', my2: 'value2' }, my2: { special: 'value3', life: 'value4' } }
205+
await cache.set('my', obj);
206+
await cache.set('my2', obj2);
207+
await cache.mget(['my', 'my2']); // { my: { my: 'value', my2: 'value2' }, my2: { special: 'value3', life: 'value4' } }
208208
```
209209

210-
## `take(key: string | number): any`
210+
## `take(key: string | number): Promise<T>`
211211

212212
Get a value from the cache by key and delete it. If the key does not exist it will return `undefined`.
213213

214214
```javascript
215-
cache.set('foo', 'bar');
216-
cache.take('foo'); // 'bar'
217-
cache.get('foo'); // undefined
215+
await cache.set('foo', 'bar');
216+
await cache.take('foo'); // 'bar'
217+
await cache.get('foo'); // undefined
218218
```
219219

220-
## `del(key: string | number | Array<string | number>): number`
220+
## `del(key: string | number | Array<string | number>): Promise<number>`
221221

222222
Delete a key from the cache. Will return the number of deleted entries and never fail. You can also pass in an array of keys to delete multiple keys. All examples assume that you have initialized the cache like `const cache = new NodeCache();`.
223223

224224
```javascript
225-
cache.del('foo'); // true
225+
await cache.del('foo'); // true
226226
```
227227

228228
passing in an array of keys:
229229

230230
```javascript
231-
cache.del(['foo', 'bar']); // true
231+
await cache.del(['foo', 'bar']); // true
232232
```
233233

234-
## `.mdel(keys: Array<string | number>): number`
234+
## `.mdel(keys: Array<string | number>): Promise<number>`
235235

236236
Delete multiple keys from the cache. Will return the number of deleted entries and never fail.
237237

238238
```javascript
239-
cache.mdel(['foo', 'bar']); // true
239+
await cache.mdel(['foo', 'bar']); // true
240240
```
241241

242-
## `.ttl(key: string | number, ttl?: number): boolean`
242+
## `.ttl(key: string | number, ttl?: number): Promise<boolean>`
243243

244244
Redefine the ttl of a key. Returns true if the key has been found and changed. Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
245245

246246
```javascript
247-
cache.ttl('foo', 10); // true
247+
await cache.ttl('foo', 10); // true
248248
```
249249

250-
## `getTtl(key: string | number): number | undefined`
250+
## `getTtl(key: string | number): Promise<number | undefined>`
251251

252252
Get the ttl expiration from `Date.now()` of a key. If the key does not exist it will return `undefined`.
253253

254254
```javascript
255-
cache.getTtl('foo'); // 1725993344859
255+
await cache.getTtl('foo'); // 1725993344859
256256
```
257257

258-
## `has(key: string | number): boolean`
258+
## `has(key: string | number): Promise<boolean>`
259259

260260
Check if a key exists in the cache.
261261

262262
```javascript
263-
cache.set('foo', 'bar');
264-
cache.has('foo'); // true
263+
await cache.set('foo', 'bar');
264+
await cache.has('foo'); // true
265265
```
266266

267-
## `keys(): Array<string>`
267+
## `keys(): Promise<Array<string>>`
268268

269269
Get all keys from the cache.
270270

271271
```javascript
272-
cache.keys(); // ['foo', 'bar']
272+
await cache.keys(); // ['foo', 'bar']
273273
```
274274

275275
## `getStats(): NodeCacheStats`
@@ -286,7 +286,7 @@ Flush the cache. Will remove all keys and reset the stats.
286286

287287
```javascript
288288
cache.flushAll();
289-
cache.keys(); // []
289+
await cache.keys(); // []
290290
cache.getStats(); // {hits: 0, misses: 0, keys: 0, ksize: 0, vsize: 0}
291291
```
292292

@@ -295,18 +295,10 @@ cache.getStats(); // {hits: 0, misses: 0, keys: 0, ksize: 0, vsize: 0}
295295
Flush the stats. Will reset the stats but keep the keys.
296296

297297
```javascript
298-
cache.set('foo', 'bar');
298+
await cache.set('foo', 'bar');
299299
cache.flushStats();
300300
cache.getStats(); // {hits: 0, misses: 0, keys: 0, ksize: 0, vsize: 0}
301-
cache.keys(); // ['foo']
302-
```
303-
304-
## `close(): void`
305-
306-
this will stop the interval that is running for the `checkperiod` and `deleteOnExpire` options.
307-
308-
```javascript
309-
cache.close();
301+
await cache.keys(); // ['foo']
310302
```
311303

312304
## `on(event: string, callback: Function): void`

packages/node-cache/src/store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export class NodeCacheStore extends Hookified {
239239
/**
240240
* Check if a key exists in the cache. If it does exist it will get the value and delete the item from the cache.
241241
* @param {string | number} key
242-
* @returns {any | undefined}
242+
* @returns {T | undefined}
243243
*/
244244
public async take<T>(key: string | number): Promise<T | undefined> {
245245
return this._cache.take<T>(key.toString());

0 commit comments

Comments
 (0)