@node-ts-cache/lru-storage 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +153 -0
- package/dist/LRUStorage.d.ts +24 -0
- package/dist/LRUStorage.js +28 -0
- package/dist/LRUStorage.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/package.json +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Himmet Avsar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
# @node-ts-cache/lru-storage
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.org/package/@node-ts-cache/lru-storage)
|
|
4
|
+
|
|
5
|
+
LRU (Least Recently Used) cache storage adapter for [@node-ts-cache/core](https://www.npmjs.com/package/@node-ts-cache/core) using [lru-cache](https://www.npmjs.com/package/lru-cache).
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- Synchronous operations
|
|
10
|
+
- Automatic eviction of least recently used items
|
|
11
|
+
- Configurable maximum size (items or memory)
|
|
12
|
+
- Built-in TTL support
|
|
13
|
+
- Multi-get/set operations
|
|
14
|
+
- Memory-safe with bounded cache size
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @node-ts-cache/core @node-ts-cache/lru-storage
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### Basic Usage
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { SyncCache, ExpirationStrategy } from '@node-ts-cache/core';
|
|
28
|
+
import { LRUStorage } from '@node-ts-cache/lru-storage';
|
|
29
|
+
|
|
30
|
+
const storage = new LRUStorage({
|
|
31
|
+
max: 500 // Maximum 500 items
|
|
32
|
+
});
|
|
33
|
+
const strategy = new ExpirationStrategy(storage);
|
|
34
|
+
|
|
35
|
+
class DataService {
|
|
36
|
+
@SyncCache(strategy, { ttl: 60 })
|
|
37
|
+
getData(key: string): Data {
|
|
38
|
+
return computeExpensiveData(key);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### With TTL
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
const storage = new LRUStorage({
|
|
47
|
+
max: 1000,
|
|
48
|
+
ttl: 300 // 5 minutes in seconds
|
|
49
|
+
});
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Memory-Based Limit
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
const storage = new LRUStorage({
|
|
56
|
+
max: 500,
|
|
57
|
+
maxSize: 5000 // Maximum total "size" units
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Async Usage
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
import { Cache, ExpirationStrategy } from '@node-ts-cache/core';
|
|
65
|
+
import { LRUStorage } from '@node-ts-cache/lru-storage';
|
|
66
|
+
|
|
67
|
+
const storage = new LRUStorage({ max: 1000 });
|
|
68
|
+
const strategy = new ExpirationStrategy(storage);
|
|
69
|
+
|
|
70
|
+
class UserService {
|
|
71
|
+
@Cache(strategy, { ttl: 300 })
|
|
72
|
+
async getUser(id: string): Promise<User> {
|
|
73
|
+
return await db.users.findById(id);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Multi-Operations
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { MultiCache, ExpirationStrategy } from '@node-ts-cache/core';
|
|
82
|
+
import { LRUStorage } from '@node-ts-cache/lru-storage';
|
|
83
|
+
|
|
84
|
+
const storage = new LRUStorage({ max: 1000 });
|
|
85
|
+
const strategy = new ExpirationStrategy(storage);
|
|
86
|
+
|
|
87
|
+
class ProductService {
|
|
88
|
+
@MultiCache([strategy], 0, id => `product:${id}`)
|
|
89
|
+
async getProducts(ids: string[]): Promise<Product[]> {
|
|
90
|
+
return await db.products.findByIds(ids);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Direct API Usage
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
const storage = new LRUStorage({ max: 100 });
|
|
99
|
+
const strategy = new ExpirationStrategy(storage);
|
|
100
|
+
|
|
101
|
+
// Store
|
|
102
|
+
strategy.setItem('key', { data: 'value' }, { ttl: 60 });
|
|
103
|
+
|
|
104
|
+
// Retrieve (also marks as "recently used")
|
|
105
|
+
const value = strategy.getItem<{ data: string }>('key');
|
|
106
|
+
|
|
107
|
+
// Clear all
|
|
108
|
+
strategy.clear();
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Constructor Options
|
|
112
|
+
|
|
113
|
+
| Option | Type | Default | Description |
|
|
114
|
+
| --------- | -------- | -------- | ------------------------------------------- |
|
|
115
|
+
| `max` | `number` | Required | Maximum number of items |
|
|
116
|
+
| `ttl` | `number` | - | Time to live in **seconds** |
|
|
117
|
+
| `maxSize` | `number` | - | Maximum total size (for memory-based limit) |
|
|
118
|
+
|
|
119
|
+
## Interface
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
interface ISynchronousCacheType {
|
|
123
|
+
getItem<T>(key: string): T | undefined;
|
|
124
|
+
setItem(key: string, content: any, options?: any): void;
|
|
125
|
+
clear(): void;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
interface IMultiSynchronousCacheType {
|
|
129
|
+
getItems<T>(keys: string[]): { [key: string]: T | undefined };
|
|
130
|
+
setItems(values: { key: string; content: any }[], options?: any): void;
|
|
131
|
+
clear(): void;
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## LRU Eviction
|
|
136
|
+
|
|
137
|
+
When the cache reaches `max` items, the least recently accessed items are automatically evicted to make room for new ones. This makes LRU ideal for:
|
|
138
|
+
|
|
139
|
+
- Memory-constrained environments
|
|
140
|
+
- Hot-data caching (frequently accessed items stay cached)
|
|
141
|
+
- Preventing unbounded memory growth
|
|
142
|
+
|
|
143
|
+
## Dependencies
|
|
144
|
+
|
|
145
|
+
- `lru-cache` ^10.0.0
|
|
146
|
+
|
|
147
|
+
## Requirements
|
|
148
|
+
|
|
149
|
+
- Node.js >= 18.0.0
|
|
150
|
+
|
|
151
|
+
## License
|
|
152
|
+
|
|
153
|
+
MIT
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { IMultiSynchronousCacheType, ISynchronousCacheType } from '@node-ts-cache/core';
|
|
2
|
+
import { LRUCache } from 'lru-cache';
|
|
3
|
+
export interface LRUStorageOptions {
|
|
4
|
+
/** Maximum number of items in cache (required) */
|
|
5
|
+
max: number;
|
|
6
|
+
/** Time to live in seconds */
|
|
7
|
+
ttl?: number;
|
|
8
|
+
/** Maximum size (if using sizeCalculation) */
|
|
9
|
+
maxSize?: number;
|
|
10
|
+
}
|
|
11
|
+
export declare class LRUStorage implements ISynchronousCacheType, IMultiSynchronousCacheType {
|
|
12
|
+
myCache: LRUCache<string, any>;
|
|
13
|
+
constructor(/** ttl in seconds! */ options: LRUStorageOptions);
|
|
14
|
+
getItems<T>(keys: string[]): {
|
|
15
|
+
[key: string]: T | undefined;
|
|
16
|
+
};
|
|
17
|
+
setItems<T = unknown>(values: {
|
|
18
|
+
key: string;
|
|
19
|
+
content: T | undefined;
|
|
20
|
+
}[]): void;
|
|
21
|
+
getItem<T>(key: string): T | undefined;
|
|
22
|
+
setItem<T = unknown>(key: string, content: T | undefined): void;
|
|
23
|
+
clear(): void;
|
|
24
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { LRUCache } from 'lru-cache';
|
|
2
|
+
export class LRUStorage {
|
|
3
|
+
constructor(/** ttl in seconds! */ options) {
|
|
4
|
+
this.myCache = new LRUCache({
|
|
5
|
+
max: options.max,
|
|
6
|
+
ttl: options.ttl ? options.ttl * 1000 : undefined,
|
|
7
|
+
maxSize: options.maxSize
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
getItems(keys) {
|
|
11
|
+
return Object.fromEntries(keys.map(key => [key, this.myCache.get(key)]));
|
|
12
|
+
}
|
|
13
|
+
setItems(values) {
|
|
14
|
+
values.forEach(val => {
|
|
15
|
+
this.myCache.set(val.key, val.content);
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
getItem(key) {
|
|
19
|
+
return this.myCache.get(key);
|
|
20
|
+
}
|
|
21
|
+
setItem(key, content) {
|
|
22
|
+
this.myCache.set(key, content);
|
|
23
|
+
}
|
|
24
|
+
clear() {
|
|
25
|
+
this.myCache.clear();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=LRUStorage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LRUStorage.js","sourceRoot":"","sources":["../src/LRUStorage.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAWrC,MAAM,OAAO,UAAU;IAKtB,YAAY,sBAAsB,CAAC,OAA0B;QAC5D,IAAI,CAAC,OAAO,GAAG,IAAI,QAAQ,CAAC;YAC3B,GAAG,EAAE,OAAO,CAAC,GAAG;YAChB,GAAG,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,SAAS;YACjD,OAAO,EAAE,OAAO,CAAC,OAAO;SACxB,CAAC,CAAC;IACJ,CAAC;IAED,QAAQ,CAAI,IAAc;QACzB,OAAO,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,GAAG,EAAE,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAEtE,CAAC;IACH,CAAC;IAED,QAAQ,CAAc,MAAiD;QACtE,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;YACpB,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;QACxC,CAAC,CAAC,CAAC;IACJ,CAAC;IAEM,OAAO,CAAI,GAAW;QAC5B,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAkB,CAAC;IAC/C,CAAC;IAEM,OAAO,CAAc,GAAW,EAAE,OAAsB;QAC9D,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;IAChC,CAAC;IAEM,KAAK;QACX,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;IACtB,CAAC;CACD"}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C,eAAe,UAAU,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@node-ts-cache/lru-storage",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Simple and extensible caching module supporting decorators",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"node",
|
|
7
|
+
"nodejs",
|
|
8
|
+
"cache",
|
|
9
|
+
"typescript",
|
|
10
|
+
"ts",
|
|
11
|
+
"caching",
|
|
12
|
+
"memcache",
|
|
13
|
+
"memory-cache",
|
|
14
|
+
"redis-cache",
|
|
15
|
+
"redis",
|
|
16
|
+
"file-cache",
|
|
17
|
+
"node-cache",
|
|
18
|
+
"ts-cache"
|
|
19
|
+
],
|
|
20
|
+
"homepage": "https://github.com/simllll/node-ts-cache/tree/master/storages/lru#readme",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/simllll/node-ts-cache/issues"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/simllll/node-ts-cache.git"
|
|
27
|
+
},
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"author": "Simon Tretter <s.tretter@gmail.com>",
|
|
30
|
+
"type": "module",
|
|
31
|
+
"main": "dist/index.js",
|
|
32
|
+
"types": "dist/index.d.ts",
|
|
33
|
+
"files": [
|
|
34
|
+
"dist"
|
|
35
|
+
],
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"lru-cache": "^10.0.0",
|
|
38
|
+
"@node-ts-cache/core": "1.0.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {},
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": ">=18.0.0"
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "tsc -p .",
|
|
49
|
+
"clean": "git clean -fdx src",
|
|
50
|
+
"dev": "tsc -p . -w",
|
|
51
|
+
"test": "mocha --loader=ts-node/esm test/**/*.test.ts"
|
|
52
|
+
}
|
|
53
|
+
}
|