@gravito/stasis 3.0.1 → 3.1.1

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 CHANGED
@@ -1,86 +1,76 @@
1
- # @gravito/stasis
1
+ # @gravito/stasis 🧊
2
2
 
3
- > The Standard Cache Orbit for Galaxy Architecture.
3
+ > High-performance Cache and State Management Orbit for Gravito.
4
4
 
5
- Provides a Laravel-like cache layer with:
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
- - `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`)
7
+ ---
11
8
 
12
- ## 📦 Installation
9
+ ## 📖 Quick Index
10
+ * [**Architecture Deep Dive**](./docs/architecture.md) — Understand the mechanics of hybrid caching and predictive engines.
11
+ * [**Observability & Protection**](./docs/observability.md) — How to prevent OOM and monitor cache performance.
12
+
13
+ ---
14
+
15
+ ## 🌟 Core Capabilities
16
+ * 🚀 **Unified API**: Seamlessly switch between Memory, Redis, File, and other storage drivers.
17
+ * 🏗️ **Tiered Cache (Hybrid)**: Combine local Memory with distributed Redis for extreme read speeds.
18
+ * 🔒 **Distributed Locks**: Atomic cross-instance concurrency control.
19
+ * 🚦 **Rate Limiting**: Built-in traffic throttling on top of your cache infrastructure.
20
+ * 🧠 **Smart Pre-warming**: Access path prediction and automated pre-fetching powered by Markov Chains.
13
21
 
22
+ ## 📦 Installation
14
23
  ```bash
15
24
  bun add @gravito/stasis
16
25
  ```
17
26
 
18
- ## 🚀 Usage
27
+ ## 🚀 5-Minute Quick Start
19
28
 
29
+ ### 1. Configure the Orbit
20
30
  ```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' },
31
+ import { defineConfig } from '@gravito/core'
32
+ import { OrbitStasis } from '@gravito/stasis'
33
+
34
+ export default defineConfig({
35
+ config: {
36
+ cache: {
37
+ default: 'tiered', // default to tiered caching
38
+ stores: {
39
+ local: { driver: 'memory', maxItems: 1000 },
40
+ remote: { driver: 'redis', connection: 'default' },
41
+ tiered: { driver: 'tiered', local: 'local', remote: 'remote' }
42
+ }
43
+ }
39
44
  },
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
- });
45
+ orbits: [new OrbitStasis()]
46
+ })
54
47
  ```
55
48
 
56
- ### Tags (memory store)
49
+ ### 2. Basic Caching Example
50
+ ```typescript
51
+ const cache = core.container.make('cache');
57
52
 
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
53
+ // 💡 Classic "Remember" pattern
54
+ const news = await cache.remember('news:today', 3600, () => {
55
+ return await db.news.latest();
56
+ });
62
57
  ```
63
58
 
64
- ### Locks
59
+ ---
65
60
 
66
- ```ts
67
- const cache = c.get('cache');
68
- await cache.lock('jobs:rebuild', 10).block(5, async () => {
69
- // exclusive section
70
- });
71
- ```
61
+ ## 🛠️ Drivers Overview
72
62
 
73
- ## 🪝 Hooks
63
+ | Driver Name | Tier | Best For |
64
+ | :--- | :--- | :--- |
65
+ | **Memory** | L1 | Local hotspots, LRU restricted |
66
+ | **Redis** | L2 | Distributed sharing, Atomic locks |
67
+ | **Tiered** | Hybrid | **Recommended**: Balance of speed and consistency |
68
+ | **Predictive**| Smart | Scenarios with clear access patterns |
69
+ | **File** | Persistent | Simple local persistence |
74
70
 
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.
71
+ ---
81
72
 
82
- By default, hook events run in `async` mode and never block cache reads/writes. For debug, set:
73
+ ## 🤝 Contributing
74
+ We welcome any optimization suggestions! Please see our [Contributing Guide](../../CONTRIBUTING.md).
83
75
 
84
- ```ts
85
- orbitCache(core, { eventsMode: 'sync', throwOnEventError: true });
86
- ```
76
+ MIT © Carl Lee
package/README.zh-TW.md CHANGED
@@ -1,38 +1,76 @@
1
- # @gravito/stasis
1
+ # @gravito/stasis 🧊
2
2
 
3
- > Gravito 的快取 Orbit,提供類 Laravel 的 Cache API
3
+ > Gravito 的高效能快取與狀態管理 Orbit。
4
4
 
5
- ## 安裝
5
+ `@gravito/stasis` 將複雜的快取邏輯封裝成優雅且強大的 API,讓你的應用在處理高併發與海量數據時依然游刃有餘。
6
6
 
7
+ ---
8
+
9
+ ## 📖 快速索引
10
+ * [**技術架構深探**](./docs/architecture.zh-TW.md) — 了解混合式快取與預測機制的運作原理。
11
+ * [**可觀察性與資源保護**](./docs/observability.zh-TW.md) — 如何防止 OOM 並監控快取效能。
12
+
13
+ ---
14
+
15
+ ## 🌟 核心能力
16
+ * 🚀 **統一 API**:無縫切換 Memory, Redis, File 等不同存儲驅動。
17
+ * 🏗️ **混合快取 (Tiered)**:結合本地 Memory 與分散式 Redis,實現極限讀取速度。
18
+ * 🔒 **分散式鎖**:基於原子操作的跨實例並發控制。
19
+ * 🚦 **流量節流**:內建基於快取基礎設施的 Rate Limiting。
20
+ * 🧠 **智慧預熱**:基於馬可夫鏈的存取路徑預測與自動讀取。
21
+
22
+ ## 📦 安裝
7
23
  ```bash
8
24
  bun add @gravito/stasis
9
25
  ```
10
26
 
11
- ## 快速開始
27
+ ## 🚀 5 分鐘快速上手
12
28
 
29
+ ### 1. 配置 Orbit
13
30
  ```typescript
14
- import { PlanetCore } from '@gravito/core'
15
- import orbitCache from '@gravito/stasis'
16
-
17
- const core = new PlanetCore()
18
-
19
- const cache = orbitCache(core, {
20
- exposeAs: 'cache',
21
- default: 'memory',
22
- prefix: 'app_cache:',
23
- defaultTtl: 60,
24
- stores: {
25
- memory: { driver: 'memory', maxItems: 10_000 },
26
- file: { driver: 'file', directory: './tmp/cache' },
27
- null: { driver: 'null' },
31
+ import { defineConfig } from '@gravito/core'
32
+ import { OrbitStasis } from '@gravito/stasis'
33
+
34
+ export default defineConfig({
35
+ config: {
36
+ cache: {
37
+ default: 'tiered', // 預設使用分層快取
38
+ stores: {
39
+ local: { driver: 'memory', maxItems: 1000 },
40
+ remote: { driver: 'redis', connection: 'default' },
41
+ tiered: { driver: 'tiered', local: 'local', remote: 'remote' }
42
+ }
43
+ }
28
44
  },
45
+ orbits: [new OrbitStasis()]
29
46
  })
47
+ ```
30
48
 
31
- core.app.get('/heavy-data', async (c) => {
32
- const data = await cache.remember('heavy_key', 300, async () => {
33
- return { result: 42 }
34
- })
49
+ ### 2. 資料存取範例
50
+ ```typescript
51
+ const cache = core.container.make('cache');
35
52
 
36
- return c.json(data)
37
- })
53
+ // 💡 經典的「讀取或存儲」模式
54
+ const news = await cache.remember('news:today', 3600, () => {
55
+ return await db.news.latest();
56
+ });
38
57
  ```
58
+
59
+ ---
60
+
61
+ ## 🛠️ 驅動程式一覽
62
+
63
+ | 驅動名稱 | 性質 | 適用場景 |
64
+ | :--- | :--- | :--- |
65
+ | **Memory** | L1 | 本地熱點、LRU 限制 |
66
+ | **Redis** | L2 | 分散式共用、原子性鎖 |
67
+ | **Tiered** | Hybrid | **推薦**:平衡效能與一致性 |
68
+ | **Predictive**| Smart | 具有明顯路徑規律的存取場景 |
69
+ | **File** | Persistent | 簡單的本地持久化 |
70
+
71
+ ---
72
+
73
+ ## 🤝 參與貢獻
74
+ 我們歡迎任何優化建議!請參閱 [貢獻指南](../../CONTRIBUTING.md)。
75
+
76
+ MIT © Carl Lee