@gravito/stasis 1.0.0-beta.2
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/README.md +86 -0
- package/dist/index.cjs +11107 -0
- package/dist/index.cjs.map +74 -0
- package/dist/index.mjs +11076 -0
- package/dist/index.mjs.map +73 -0
- package/package.json +55 -0
package/README.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# @gravito/stasis
|
|
2
|
+
|
|
3
|
+
> The Standard Cache Orbit for Galaxy Architecture.
|
|
4
|
+
|
|
5
|
+
Provides a Laravel-like cache layer with:
|
|
6
|
+
|
|
7
|
+
- `CacheManager` + `CacheRepository` API (`get/put/add/remember/forever/increment/decrement/pull`)
|
|
8
|
+
- Multiple stores (`memory`, `file`, `null`, `custom`)
|
|
9
|
+
- Tags (memory store) and Locks (`lock().block()`)
|
|
10
|
+
- Hook events (`cache:hit`, `cache:miss`, `cache:write`, `cache:forget`, `cache:flush`, `cache:init`)
|
|
11
|
+
|
|
12
|
+
## 📦 Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
bun add @gravito/stasis
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## 🚀 Usage
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
import { PlanetCore } from 'gravito-core';
|
|
22
|
+
import orbitCache from '@gravito/stasis';
|
|
23
|
+
|
|
24
|
+
const core = new PlanetCore();
|
|
25
|
+
|
|
26
|
+
// Initialize Cache Orbit (Laravel-like config)
|
|
27
|
+
const cache = orbitCache(core, {
|
|
28
|
+
exposeAs: 'cache',
|
|
29
|
+
default: 'memory',
|
|
30
|
+
prefix: 'app_cache:',
|
|
31
|
+
defaultTtl: 60, // seconds
|
|
32
|
+
// Low-overhead hook dispatch (default: 'async')
|
|
33
|
+
// Use 'sync' + throwOnEventError for development/debug.
|
|
34
|
+
eventsMode: 'async',
|
|
35
|
+
stores: {
|
|
36
|
+
memory: { driver: 'memory', maxItems: 10_000 },
|
|
37
|
+
file: { driver: 'file', directory: './tmp/cache' },
|
|
38
|
+
null: { driver: 'null' },
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// Use in routes
|
|
43
|
+
core.app.get('/heavy-data', async (c) => {
|
|
44
|
+
// Either use the manager returned from orbitCache(...) or request-scoped access:
|
|
45
|
+
// const cache = c.get('cache');
|
|
46
|
+
|
|
47
|
+
const data = await cache.remember('heavy_key', 300, async () => {
|
|
48
|
+
// Expensive computation...
|
|
49
|
+
return { result: 42 };
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
return c.json(data);
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Tags (memory store)
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
const cache = c.get('cache');
|
|
60
|
+
await cache.tags(['users']).set('user:1', { id: 1 }, 60);
|
|
61
|
+
await cache.tags(['users']).clear(); // flush only tagged keys
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Locks
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
const cache = c.get('cache');
|
|
68
|
+
await cache.lock('jobs:rebuild', 10).block(5, async () => {
|
|
69
|
+
// exclusive section
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## 🪝 Hooks
|
|
74
|
+
|
|
75
|
+
- `cache:miss` - Fired when data is not found in cache.
|
|
76
|
+
- `cache:hit` - Fired when data is retrieved from cache.
|
|
77
|
+
- `cache:write` - Fired when writing cache.
|
|
78
|
+
- `cache:forget` - Fired when forgetting cache.
|
|
79
|
+
- `cache:flush` - Fired when flushing cache.
|
|
80
|
+
- `cache:init` - Fired when the orbit is installed.
|
|
81
|
+
|
|
82
|
+
By default, hook events run in `async` mode and never block cache reads/writes. For debug, set:
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
orbitCache(core, { eventsMode: 'sync', throwOnEventError: true });
|
|
86
|
+
```
|