@gravito/stasis 3.1.0 → 3.2.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/README.md +64 -80
- package/README.zh-TW.md +40 -79
- package/dist/CacheManager.d.ts +484 -0
- package/dist/CacheRepository.d.ts +495 -0
- package/dist/RateLimiter.d.ts +152 -0
- package/dist/cache-events.d.ts +58 -0
- package/dist/index.cjs +373 -1762
- package/dist/index.cjs.map +27 -0
- package/dist/index.d.ts +29 -2676
- package/dist/index.js +356 -1709
- package/dist/index.js.map +27 -0
- package/dist/locks.d.ts +193 -0
- package/dist/prediction/AccessPredictor.d.ts +64 -0
- package/dist/store.d.ts +200 -0
- package/dist/stores/CircuitBreakerStore.d.ts +78 -0
- package/dist/stores/FileStore.d.ts +36 -0
- package/dist/stores/MemoryStore.d.ts +261 -0
- package/dist/stores/NullStore.d.ts +115 -0
- package/dist/stores/PredictiveStore.d.ts +40 -0
- package/dist/stores/RedisStore.d.ts +83 -0
- package/dist/stores/TieredStore.d.ts +37 -0
- package/dist/tagged-store.d.ts +29 -0
- package/dist/types.d.ts +149 -0
- package/dist/utils/LRUCache.d.ts +104 -0
- package/package.json +8 -6
- package/dist/index.d.cts +0 -2989
package/README.md
CHANGED
|
@@ -1,115 +1,99 @@
|
|
|
1
1
|
# @gravito/stasis 🧊
|
|
2
2
|
|
|
3
|
-
> High-performance
|
|
3
|
+
> High-performance Cache and State Management Orbit for Gravito.
|
|
4
4
|
|
|
5
|
-
`@gravito/stasis`
|
|
5
|
+
`@gravito/stasis` wraps complex caching logic into an elegant and powerful API, ensuring your application handles high concurrency and massive datasets with ease.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
---
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
- **💾 Multiple Storage Drivers**: Native support for Memory, Redis, File-based, and Null storage.
|
|
11
|
-
- **🔒 Distributed Locks**: Prevent race conditions with atomic, cross-process locks.
|
|
12
|
-
- **⚡ Flexible Caching (SWR)**: Stale-While-Revalidate support to serve data fast while refreshing in the background.
|
|
13
|
-
- **🚦 Integrated Rate Limiting**: Throttling mechanism built directly on top of your cache infrastructure.
|
|
14
|
-
- **🏷️ Cache Tagging**: Group related items for bulk invalidation (supported in Memory driver).
|
|
15
|
-
- **🪝 Hook System**: Lifecycle events for monitoring cache hits, misses, and writes.
|
|
9
|
+
## 📚 Documentation
|
|
16
10
|
|
|
17
|
-
|
|
11
|
+
Detailed guides and references for the Galaxy Architecture:
|
|
12
|
+
|
|
13
|
+
- [🏗️ **Architecture Deep Dive**](./docs/architecture.md) — Under the hood of tiered caching.
|
|
14
|
+
- [🧊 **Hybrid Caching**](./doc/HYBRID_CACHING.md) — **NEW**: L1/L2 strategy and predictive warming.
|
|
15
|
+
- [📊 **Observability**](./docs/observability.md) — Monitoring cache health and hit rates.
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## 🌟 Core Capabilities
|
|
20
|
+
* 🚀 **Unified API**: Seamlessly switch between Memory, Redis, File, and other storage drivers.
|
|
21
|
+
* 🏗️ **Tiered Cache (Hybrid)**: Combine local Memory with distributed Redis for extreme read speeds.
|
|
22
|
+
* 🧠 **Predictive State Warming**: Access path prediction and automated pre-fetching powered by Markov Chains.
|
|
23
|
+
* 🔒 **Distributed Locks**: Atomic cross-instance concurrency control across the Galaxy.
|
|
24
|
+
* 🚦 **Rate Limiting**: Built-in traffic throttling on top of your cache infrastructure.
|
|
25
|
+
* 🪐 **Galaxy-Ready**: Native integration with PlanetCore for universal caching.
|
|
26
|
+
|
|
27
|
+
## 🌌 Role in Galaxy Architecture
|
|
28
|
+
|
|
29
|
+
In the **Gravito Galaxy Architecture**, Stasis acts as the **Thermal Buffer (Insulation Layer)**.
|
|
30
|
+
|
|
31
|
+
- **Load Insulation**: Protects the `Atlas` Data Gravity core from being overwhelmed by repetitive queries, ensuring low-latency responses for the `Photon` Sensing Layer.
|
|
32
|
+
- **Distributed Consistency**: Works with `Plasma` to provide a consistent view of frequently accessed state across multiple Satellite instances.
|
|
33
|
+
- **Predictive Efficiency**: Uses advanced algorithms to warm up the cache before a Satellite even receives a request, minimizing cold-start latency in serverless or edge environments.
|
|
18
34
|
|
|
35
|
+
```mermaid
|
|
36
|
+
graph TD
|
|
37
|
+
P[Photon: Sensing] --> S[Satellite]
|
|
38
|
+
S --> Stasis{Stasis Buffer}
|
|
39
|
+
Stasis -- "Hit" --> S
|
|
40
|
+
Stasis -- "Miss" --> Atlas[(Atlas: DB)]
|
|
41
|
+
Atlas --> Stasis
|
|
42
|
+
Stasis -.-> Plasma[(Plasma: Shared State)]
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## 📦 Installation
|
|
19
46
|
```bash
|
|
20
47
|
bun add @gravito/stasis
|
|
21
48
|
```
|
|
22
49
|
|
|
23
|
-
## 🚀 Quick Start
|
|
24
|
-
|
|
25
|
-
### 1. Register the Orbit
|
|
50
|
+
## 🚀 5-Minute Quick Start
|
|
26
51
|
|
|
52
|
+
### 1. Configure the Orbit
|
|
27
53
|
```typescript
|
|
28
|
-
import {
|
|
54
|
+
import { defineConfig } from '@gravito/core'
|
|
29
55
|
import { OrbitStasis } from '@gravito/stasis'
|
|
30
56
|
|
|
31
|
-
|
|
57
|
+
export default defineConfig({
|
|
32
58
|
config: {
|
|
33
59
|
cache: {
|
|
34
|
-
default: '
|
|
60
|
+
default: 'tiered', // default to tiered caching
|
|
35
61
|
stores: {
|
|
36
|
-
|
|
37
|
-
|
|
62
|
+
local: { driver: 'memory', maxItems: 1000 },
|
|
63
|
+
remote: { driver: 'redis', connection: 'default' },
|
|
64
|
+
tiered: { driver: 'tiered', local: 'local', remote: 'remote' }
|
|
38
65
|
}
|
|
39
66
|
}
|
|
40
67
|
},
|
|
41
68
|
orbits: [new OrbitStasis()]
|
|
42
69
|
})
|
|
43
|
-
|
|
44
|
-
const core = await PlanetCore.boot(config)
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
### 2. Basic Caching
|
|
48
|
-
|
|
49
|
-
```typescript
|
|
50
|
-
const cache = core.container.make('cache')
|
|
51
|
-
|
|
52
|
-
// Simple storage
|
|
53
|
-
await cache.put('stats:total', 100, 3600) // Store for 1 hour
|
|
54
|
-
|
|
55
|
-
// "Remember" pattern (Get or Set)
|
|
56
|
-
const users = await cache.remember('users:all', 300, async () => {
|
|
57
|
-
return await db.users.findMany()
|
|
58
|
-
})
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
### 3. Distributed Locking
|
|
62
|
-
|
|
63
|
-
```typescript
|
|
64
|
-
const lock = cache.lock('process-invoice:123', 10)
|
|
65
|
-
|
|
66
|
-
if (await lock.get()) {
|
|
67
|
-
try {
|
|
68
|
-
// Perform critical task...
|
|
69
|
-
} finally {
|
|
70
|
-
await lock.release()
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
70
|
```
|
|
74
71
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
Easily throttle requests or actions using your cache backend.
|
|
78
|
-
|
|
72
|
+
### 2. Basic Caching Example
|
|
79
73
|
```typescript
|
|
80
|
-
const
|
|
74
|
+
const cache = core.container.make('cache');
|
|
81
75
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
await limiter.hit('login:127.0.0.1', 60) // Decay in 60s
|
|
76
|
+
// 💡 Classic "Remember" pattern
|
|
77
|
+
const news = await cache.remember('news:today', 3600, () => {
|
|
78
|
+
return await db.news.latest();
|
|
79
|
+
});
|
|
88
80
|
```
|
|
89
81
|
|
|
90
|
-
|
|
82
|
+
---
|
|
91
83
|
|
|
92
|
-
|
|
93
|
-
|---|---|---|
|
|
94
|
-
| **Memory** | Local dev & Small apps | Fast, Tags, LRU |
|
|
95
|
-
| **Redis** | Distributed production | Multi-node, Locks, Persistent |
|
|
96
|
-
| **File** | Simple persistence | No external deps |
|
|
97
|
-
| **Null** | Testing / Disabling cache | No-op |
|
|
84
|
+
## 🛠️ Drivers Overview
|
|
98
85
|
|
|
99
|
-
|
|
86
|
+
| Driver Name | Tier | Best For |
|
|
87
|
+
| :--- | :--- | :--- |
|
|
88
|
+
| **Memory** | L1 | Local hotspots, LRU restricted |
|
|
89
|
+
| **Redis** | L2 | Distributed sharing, Atomic locks |
|
|
90
|
+
| **Tiered** | Hybrid | **Recommended**: Balance of speed and consistency |
|
|
91
|
+
| **Predictive**| Smart | Scenarios with clear access patterns |
|
|
92
|
+
| **File** | Persistent | Simple local persistence |
|
|
100
93
|
|
|
101
|
-
|
|
102
|
-
- `cache.get(key, default?)`: Retrieve an item.
|
|
103
|
-
- `cache.put(key, value, ttl?)`: Store an item.
|
|
104
|
-
- `cache.remember(key, ttl, callback)`: Get or execute callback and store.
|
|
105
|
-
- `cache.flexible(key, ttl, stale, callback)`: Stale-While-Revalidate.
|
|
106
|
-
- `cache.increment / decrement`: Atomic numeric updates.
|
|
107
|
-
- `cache.tags(['tag1']).flush()`: Invalidate by tag.
|
|
94
|
+
---
|
|
108
95
|
|
|
109
96
|
## 🤝 Contributing
|
|
97
|
+
We welcome any optimization suggestions! Please see our [Contributing Guide](../../CONTRIBUTING.md).
|
|
110
98
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
## 📄 License
|
|
114
|
-
|
|
115
|
-
MIT © Carl Lee
|
|
99
|
+
MIT © Carl Lee
|
package/README.zh-TW.md
CHANGED
|
@@ -1,115 +1,76 @@
|
|
|
1
1
|
# @gravito/stasis 🧊
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> Gravito 的高效能快取與狀態管理 Orbit。
|
|
4
4
|
|
|
5
|
-
`@gravito/stasis`
|
|
5
|
+
`@gravito/stasis` 將複雜的快取邏輯封裝成優雅且強大的 API,讓你的應用在處理高併發與海量數據時依然游刃有餘。
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
---
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
- **⚡ 彈性快取 (SWR)**:支援 Stale-While-Revalidate 模式,在背景更新數據的同時快速響應請求。
|
|
13
|
-
- **🚦 整合式速率限制**:直接在快取基礎設施上構建的流量節流機制。
|
|
14
|
-
- **🏷️ 快取標籤 (Tagging)**:支援將相關項目分組,以便進行批量刪除(Memory 驅動支援)。
|
|
15
|
-
- **🪝 Hook 系統**:提供生命週期事件,用於監控快取命中 (Hit)、未命中 (Miss) 與寫入。
|
|
9
|
+
## 📖 快速索引
|
|
10
|
+
* [**技術架構深探**](./docs/architecture.zh-TW.md) — 了解混合式快取與預測機制的運作原理。
|
|
11
|
+
* [**可觀察性與資源保護**](./docs/observability.zh-TW.md) — 如何防止 OOM 並監控快取效能。
|
|
16
12
|
|
|
17
|
-
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## 🌟 核心能力
|
|
16
|
+
* 🚀 **統一 API**:無縫切換 Memory, Redis, File 等不同存儲驅動。
|
|
17
|
+
* 🏗️ **混合快取 (Tiered)**:結合本地 Memory 與分散式 Redis,實現極限讀取速度。
|
|
18
|
+
* 🔒 **分散式鎖**:基於原子操作的跨實例並發控制。
|
|
19
|
+
* 🚦 **流量節流**:內建基於快取基礎設施的 Rate Limiting。
|
|
20
|
+
* 🧠 **智慧預熱**:基於馬可夫鏈的存取路徑預測與自動讀取。
|
|
18
21
|
|
|
22
|
+
## 📦 安裝
|
|
19
23
|
```bash
|
|
20
24
|
bun add @gravito/stasis
|
|
21
25
|
```
|
|
22
26
|
|
|
23
|
-
## 🚀
|
|
24
|
-
|
|
25
|
-
### 1. 註冊 Orbit
|
|
27
|
+
## 🚀 5 分鐘快速上手
|
|
26
28
|
|
|
29
|
+
### 1. 配置 Orbit
|
|
27
30
|
```typescript
|
|
28
|
-
import {
|
|
31
|
+
import { defineConfig } from '@gravito/core'
|
|
29
32
|
import { OrbitStasis } from '@gravito/stasis'
|
|
30
33
|
|
|
31
|
-
|
|
34
|
+
export default defineConfig({
|
|
32
35
|
config: {
|
|
33
36
|
cache: {
|
|
34
|
-
default: '
|
|
37
|
+
default: 'tiered', // 預設使用分層快取
|
|
35
38
|
stores: {
|
|
36
|
-
|
|
37
|
-
|
|
39
|
+
local: { driver: 'memory', maxItems: 1000 },
|
|
40
|
+
remote: { driver: 'redis', connection: 'default' },
|
|
41
|
+
tiered: { driver: 'tiered', local: 'local', remote: 'remote' }
|
|
38
42
|
}
|
|
39
43
|
}
|
|
40
44
|
},
|
|
41
45
|
orbits: [new OrbitStasis()]
|
|
42
46
|
})
|
|
43
|
-
|
|
44
|
-
const core = await PlanetCore.boot(config)
|
|
45
47
|
```
|
|
46
48
|
|
|
47
|
-
### 2.
|
|
48
|
-
|
|
49
|
+
### 2. 資料存取範例
|
|
49
50
|
```typescript
|
|
50
|
-
const cache = core.container.make('cache')
|
|
51
|
+
const cache = core.container.make('cache');
|
|
51
52
|
|
|
52
|
-
//
|
|
53
|
-
await cache.
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const users = await cache.remember('users:all', 300, async () => {
|
|
57
|
-
return await db.users.findMany()
|
|
58
|
-
})
|
|
53
|
+
// 💡 經典的「讀取或存儲」模式
|
|
54
|
+
const news = await cache.remember('news:today', 3600, () => {
|
|
55
|
+
return await db.news.latest();
|
|
56
|
+
});
|
|
59
57
|
```
|
|
60
58
|
|
|
61
|
-
|
|
59
|
+
---
|
|
62
60
|
|
|
63
|
-
|
|
64
|
-
const lock = cache.lock('process-invoice:123', 10)
|
|
65
|
-
|
|
66
|
-
if (await lock.get()) {
|
|
67
|
-
try {
|
|
68
|
-
// 執行關鍵任務...
|
|
69
|
-
} finally {
|
|
70
|
-
await lock.release()
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
```
|
|
61
|
+
## 🛠️ 驅動程式一覽
|
|
74
62
|
|
|
75
|
-
|
|
63
|
+
| 驅動名稱 | 性質 | 適用場景 |
|
|
64
|
+
| :--- | :--- | :--- |
|
|
65
|
+
| **Memory** | L1 | 本地熱點、LRU 限制 |
|
|
66
|
+
| **Redis** | L2 | 分散式共用、原子性鎖 |
|
|
67
|
+
| **Tiered** | Hybrid | **推薦**:平衡效能與一致性 |
|
|
68
|
+
| **Predictive**| Smart | 具有明顯路徑規律的存取場景 |
|
|
69
|
+
| **File** | Persistent | 簡單的本地持久化 |
|
|
76
70
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
```typescript
|
|
80
|
-
const limiter = cache.limiter()
|
|
81
|
-
|
|
82
|
-
if (await limiter.tooManyAttempts('login:127.0.0.1', 5)) {
|
|
83
|
-
const seconds = await limiter.availableIn('login:127.0.0.1')
|
|
84
|
-
throw new Error(`嘗試次數過多。請在 ${seconds} 秒後重試。`)
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
await limiter.hit('login:127.0.0.1', 60) // 60 秒後衰減
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
## 🛠️ 支援的驅動程式 (Drivers)
|
|
91
|
-
|
|
92
|
-
| 驅動名稱 | 適用場景 | 核心功能 |
|
|
93
|
-
|---|---|---|
|
|
94
|
-
| **Memory** | 本地開發與小型應用 | 極速、標籤支援、LRU |
|
|
95
|
-
| **Redis** | 分佈式生產環境 | 多節點共享、鎖定、持久化 |
|
|
96
|
-
| **File** | 簡單的持久化需求 | 無外部依賴 |
|
|
97
|
-
| **Null** | 測試或停用快取 | 不執行任何操作 |
|
|
98
|
-
|
|
99
|
-
## 🧩 API 參考
|
|
100
|
-
|
|
101
|
-
### `CacheManager`
|
|
102
|
-
- `cache.get(key, default?)`:讀取項目。
|
|
103
|
-
- `cache.put(key, value, ttl?)`:存儲項目。
|
|
104
|
-
- `cache.remember(key, ttl, callback)`:讀取或執行回調並存儲。
|
|
105
|
-
- `cache.flexible(key, ttl, stale, callback)`:Stale-While-Revalidate 模式。
|
|
106
|
-
- `cache.increment / decrement`:原子性的數值更新。
|
|
107
|
-
- `cache.tags(['tag1']).flush()`:按標籤清除快取。
|
|
71
|
+
---
|
|
108
72
|
|
|
109
73
|
## 🤝 參與貢獻
|
|
110
|
-
|
|
111
|
-
我們歡迎任何形式的貢獻!詳細資訊請參閱 [貢獻指南](../../CONTRIBUTING.md)。
|
|
112
|
-
|
|
113
|
-
## 📄 開源授權
|
|
74
|
+
我們歡迎任何優化建議!請參閱 [貢獻指南](../../CONTRIBUTING.md)。
|
|
114
75
|
|
|
115
76
|
MIT © Carl Lee
|