@neezco/cache 0.1.0 → 0.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/CHANGELOG.md +22 -0
- package/README.md +3 -3
- package/dist/browser/index.d.ts +30 -6
- package/dist/browser/index.js +30 -8
- package/dist/browser/index.js.map +1 -1
- package/dist/node/index.cjs +31 -8
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.d.cts +30 -6
- package/dist/node/index.d.mts +30 -6
- package/dist/node/index.mjs +31 -8
- package/dist/node/index.mjs.map +1 -1
- package/docs/api-reference.md +21 -6
- package/docs/configuration.md +42 -3
- package/docs/contributing/code-style.md +40 -0
- package/docs/contributing/issues.md +30 -0
- package/docs/contributing/project-structure.md +35 -0
- package/docs/contributing/scripts.md +38 -0
- package/docs/contributing/setup.md +49 -0
- package/docs/contributing/workflow.md +71 -0
- package/docs/examples/stale-window.md +66 -0
- package/docs/examples/tag-based-invalidation.md +183 -0
- package/docs/examples.md +20 -125
- package/docs/getting-started.md +1 -1
- package/package.json +2 -2
package/docs/examples.md
CHANGED
|
@@ -1,145 +1,40 @@
|
|
|
1
1
|
# Examples
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Learn Neezco Cache through practical examples for common use cases.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
```javascript
|
|
8
|
-
const cache = new LocalTtlCache();
|
|
9
|
-
|
|
10
|
-
async function getUser(userId) {
|
|
11
|
-
// Check cache first
|
|
12
|
-
const cached = cache.get(`user:${userId}`);
|
|
13
|
-
if (cached) return cached;
|
|
14
|
-
|
|
15
|
-
// Not in cache or expired, fetch it
|
|
16
|
-
const user = await fetch(`/api/users/${userId}`).then(r => r.json());
|
|
17
|
-
|
|
18
|
-
// Store for 5 minutes, but serve stale for 1 more minute while refreshing
|
|
19
|
-
cache.set(`user:${userId}`, user, {
|
|
20
|
-
ttl: 5 * 60 * 1000,
|
|
21
|
-
staleWindow: 1 * 60 * 1000,
|
|
22
|
-
tags: `user:${userId}`, // Tag it for easy invalidation
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
return user;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// When a user updates their profile
|
|
29
|
-
function handleUserUpdate(userId) {
|
|
30
|
-
cache.invalidateTag(`user:${userId}`); // Instantly clear their cache
|
|
31
|
-
}
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
## Database Query Caching
|
|
35
|
-
|
|
36
|
-
Speed up repeated database queries:
|
|
37
|
-
|
|
38
|
-
```javascript
|
|
39
|
-
const cache = new LocalTtlCache({
|
|
40
|
-
defaultTtl: 2 * 60 * 1000, // 2 minutes
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
async function getProducts() {
|
|
44
|
-
const cached = cache.get("products:list");
|
|
45
|
-
if (cached) return cached;
|
|
46
|
-
|
|
47
|
-
const products = await db.query("SELECT * FROM products");
|
|
48
|
-
cache.set("products:list", products, { tags: "products" });
|
|
49
|
-
return products;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async function deleteProduct(id) {
|
|
53
|
-
await db.query("DELETE FROM products WHERE id = ?", [id]);
|
|
54
|
-
cache.invalidateTag("products"); // Clear all product caches
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
async function updateProduct(id, data) {
|
|
58
|
-
await db.query("UPDATE products SET ? WHERE id = ?", [data, id]);
|
|
59
|
-
cache.invalidateTag("products"); // Clear all product caches
|
|
60
|
-
}
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
## Graceful Stale Data Serving
|
|
64
|
-
|
|
65
|
-
Keep serving old data while fetching fresh data in the background:
|
|
66
|
-
|
|
67
|
-
```javascript
|
|
68
|
-
const cache = new LocalTtlCache();
|
|
69
|
-
|
|
70
|
-
async function getWeatherWithBackground() {
|
|
71
|
-
// Fresh for 5 minutes, but we'll serve it for 2 more minutes even if expired
|
|
72
|
-
cache.set(
|
|
73
|
-
"weather:NYC",
|
|
74
|
-
{ temp: 72, city: "NYC", fetched: Date.now() },
|
|
75
|
-
{
|
|
76
|
-
ttl: 5 * 60 * 1000,
|
|
77
|
-
staleWindow: 2 * 60 * 1000, // Keep serving for 2 extra minutes while fetching
|
|
78
|
-
},
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// NEXT skipStale, full entry
|
|
83
|
-
// This is perfect for UIs where slight data staleness is acceptable
|
|
84
|
-
// You can serve old data instantly while refreshing in the background
|
|
85
|
-
async function fetchWeatherIfNeeded(city) {
|
|
86
|
-
let weather = cache.get(`weather:${city}`);
|
|
87
|
-
|
|
88
|
-
// Stale? Fetch fresh data in the background
|
|
89
|
-
if (!weather) {
|
|
90
|
-
weather = await fetchWeatherAPI(city);
|
|
91
|
-
getWeatherWithBackground(city, weather);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return weather;
|
|
95
|
-
}
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
## Tag-Based Invalidation
|
|
99
|
-
|
|
100
|
-
Group related data and clear it all at once:
|
|
5
|
+
---
|
|
101
6
|
|
|
102
|
-
|
|
103
|
-
const cache = new LocalTtlCache();
|
|
7
|
+
## 📚 Learning Examples
|
|
104
8
|
|
|
105
|
-
|
|
106
|
-
cache.set("user:456:name", "Bob", { tags: ["user:456"] });
|
|
107
|
-
cache.set("user:456:email", "bob@example.com", { tags: ["user:456"] });
|
|
108
|
-
cache.set("user:456:preferences", { theme: "dark" }, { tags: ["user:456"] });
|
|
9
|
+
Core concepts for using Neezco Cache effectively.
|
|
109
10
|
|
|
110
|
-
|
|
111
|
-
cache.invalidateTag("user:456");
|
|
11
|
+
### [Tag-Based Invalidation](./examples/tag-based-invalidation.md)
|
|
112
12
|
|
|
113
|
-
|
|
114
|
-
console.log(cache.get("user:456:name")); // undefined
|
|
115
|
-
console.log(cache.get("user:456:email")); // undefined
|
|
116
|
-
console.log(cache.get("user:456:preferences")); // undefined
|
|
117
|
-
```
|
|
13
|
+
Learn how to manage cache invalidation—when to use tags and when to use direct deletion.
|
|
118
14
|
|
|
119
|
-
|
|
15
|
+
**Key concepts:**
|
|
120
16
|
|
|
121
|
-
|
|
17
|
+
- `cache.delete()` vs `cache.invalidateTag()`
|
|
18
|
+
- Single tag vs multiple tags
|
|
19
|
+
- When tags are worth the overhead
|
|
20
|
+
- Performance considerations
|
|
122
21
|
|
|
123
|
-
|
|
124
|
-
cache.set("user:789:profile", profileData, {
|
|
125
|
-
ttl: 10 * 60 * 1000,
|
|
126
|
-
tags: ["user:789", "profiles", "public-data"], // Multiple tags
|
|
127
|
-
});
|
|
22
|
+
### [Stale Windows](./examples/stale-window.md)
|
|
128
23
|
|
|
129
|
-
|
|
130
|
-
cache.invalidateTag("user:789");
|
|
24
|
+
Master stale windows to control memory usage and improve responsiveness.
|
|
131
25
|
|
|
132
|
-
|
|
133
|
-
cache.invalidateTag("profiles");
|
|
26
|
+
**Key concepts:**
|
|
134
27
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
28
|
+
- What stale windows are and how they work
|
|
29
|
+
- `purgeStaleOnGet` for tight memory constraints
|
|
30
|
+
- `purgeStaleOnSweep` for efficient batch cleanup
|
|
31
|
+
- Stale-while-revalidate pattern
|
|
32
|
+
- Per-entry stale window customization
|
|
138
33
|
|
|
139
34
|
---
|
|
140
35
|
|
|
141
36
|
## Navigation
|
|
142
37
|
|
|
143
|
-
- **[Getting Started](./getting-started.md)** -
|
|
38
|
+
- **[Getting Started](./getting-started.md)** - Installation and setup
|
|
144
39
|
- **[API Reference](./api-reference.md)** - All methods explained
|
|
145
40
|
- **[Configuration](./configuration.md)** - Customize your cache
|
package/docs/getting-started.md
CHANGED
package/package.json
CHANGED