@arkstack/cache 0.5.3
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 +71 -0
- package/dist/CacheManager-Do82q7KO.js +709 -0
- package/dist/commands/CacheClearCommand.d.ts +13 -0
- package/dist/commands/CacheClearCommand.js +19 -0
- package/dist/index.d.ts +526 -0
- package/dist/index.js +2 -0
- package/dist/setup.d.ts +1 -0
- package/dist/setup.js +21 -0
- package/package.json +57 -0
- package/stubs/migrations/20260601000000_create_cache_table.ts.stub +21 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Toneflix Technologies Limited
|
|
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,71 @@
|
|
|
1
|
+
# @arkstack/cache
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@arkstack/cache)
|
|
4
|
+
|
|
5
|
+
Cache module for Arkstack, providing a unified, driver based caching layer for the framework.
|
|
6
|
+
|
|
7
|
+
## Stores
|
|
8
|
+
|
|
9
|
+
`@arkstack/cache` ships with four stores out of the box:
|
|
10
|
+
|
|
11
|
+
| Driver | Backing store | Notes |
|
|
12
|
+
| ---------- | ---------------------------- | ---------------------------------------- |
|
|
13
|
+
| `memory` | in-process `Map` | default; great for dev and tests |
|
|
14
|
+
| `file` | JSON files on disk | no external service required |
|
|
15
|
+
| `redis` | Redis (via `ioredis`) | distributed; atomic counters |
|
|
16
|
+
| `database` | a table via `@arkstack/database` | reuses your database connection |
|
|
17
|
+
|
|
18
|
+
## Configuration
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
// src/config/cache.ts
|
|
22
|
+
import { Arkstack } from '@arkstack/contract'
|
|
23
|
+
import type { CacheConfig } from '@arkstack/cache'
|
|
24
|
+
import path from 'node:path'
|
|
25
|
+
|
|
26
|
+
export default (): CacheConfig => ({
|
|
27
|
+
default: env('CACHE_STORE', 'memory'),
|
|
28
|
+
prefix: env('CACHE_PREFIX', 'arkstack_cache_'),
|
|
29
|
+
stores: {
|
|
30
|
+
memory: { driver: 'memory' },
|
|
31
|
+
file: { driver: 'file', path: path.join(Arkstack.rootDir(), './storage/framework/cache') },
|
|
32
|
+
redis: { driver: 'redis', host: env('REDIS_HOST', '127.0.0.1'), port: env('REDIS_PORT', 6379) },
|
|
33
|
+
database: { driver: 'database', table: 'cache' },
|
|
34
|
+
},
|
|
35
|
+
})
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Usage
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { Cache } from '@arkstack/cache'
|
|
42
|
+
|
|
43
|
+
// default store
|
|
44
|
+
await Cache.put('user:1', user, 60) // ttl in seconds
|
|
45
|
+
await Cache.get('user:1')
|
|
46
|
+
await Cache.forget('user:1')
|
|
47
|
+
|
|
48
|
+
// remember: read-through caching
|
|
49
|
+
const stats = await Cache.remember('stats', 300, () => computeStats())
|
|
50
|
+
|
|
51
|
+
// counters
|
|
52
|
+
await Cache.increment('hits')
|
|
53
|
+
await Cache.decrement('hits', 2)
|
|
54
|
+
|
|
55
|
+
// a specific store
|
|
56
|
+
await Cache.store('redis').forever('flag', true)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
The ttl accepts a number of seconds, a `Date`, or `null` (forever).
|
|
60
|
+
|
|
61
|
+
## Custom drivers
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { Cache, Store } from '@arkstack/cache'
|
|
65
|
+
|
|
66
|
+
class TagStore extends Store { /* ... implement the Store contract ... */ }
|
|
67
|
+
|
|
68
|
+
Cache.extend('tags', (config) => new TagStore(config))
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Then reference `{ driver: 'tags' }` in a store config.
|