@hazeljs/cache 0.2.0-beta.1 → 0.2.0-beta.15

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.
@@ -1,185 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.RedisCacheStore = void 0;
7
- const core_1 = __importDefault(require("@hazeljs/core"));
8
- /**
9
- * Redis cache store implementation
10
- * Note: This is a mock implementation. In production, you would use a real Redis client like ioredis
11
- */
12
- class RedisCacheStore {
13
- constructor(options) {
14
- this.options = options;
15
- this.mockStore = new Map();
16
- this.tagIndex = new Map();
17
- this.stats = {
18
- hits: 0,
19
- misses: 0,
20
- };
21
- core_1.default.info('Redis cache store initialized (mock mode)');
22
- // In production, initialize real Redis client here
23
- // this.client = new Redis(options);
24
- }
25
- /**
26
- * Get a value from cache
27
- */
28
- async get(key) {
29
- const entry = this.mockStore.get(key);
30
- if (!entry) {
31
- this.stats.misses++;
32
- core_1.default.debug(`Redis cache miss: ${key}`);
33
- return null;
34
- }
35
- // Check if expired
36
- if (Date.now() > entry.expiresAt) {
37
- this.mockStore.delete(key);
38
- this.stats.misses++;
39
- core_1.default.debug(`Redis cache expired: ${key}`);
40
- return null;
41
- }
42
- this.stats.hits++;
43
- core_1.default.debug(`Redis cache hit: ${key}`);
44
- try {
45
- return JSON.parse(entry.value);
46
- }
47
- catch (error) {
48
- core_1.default.error(`Failed to parse cached value for key ${key}:`, error);
49
- return null;
50
- }
51
- }
52
- /**
53
- * Set a value in cache
54
- */
55
- async set(key, value, ttl = 3600) {
56
- const serialized = JSON.stringify(value);
57
- const expiresAt = Date.now() + ttl * 1000;
58
- this.mockStore.set(key, { value: serialized, expiresAt });
59
- core_1.default.debug(`Redis cache set: ${key} (TTL: ${ttl}s)`);
60
- // In production:
61
- // await this.client.setex(key, ttl, serialized);
62
- }
63
- /**
64
- * Set a value with tags
65
- */
66
- async setWithTags(key, value, ttl, tags) {
67
- await this.set(key, value, ttl);
68
- // Update tag index
69
- if (tags) {
70
- for (const tag of tags) {
71
- if (!this.tagIndex.has(tag)) {
72
- this.tagIndex.set(tag, new Set());
73
- }
74
- this.tagIndex.get(tag).add(key);
75
- // In production, use Redis sets:
76
- // await this.client.sadd(`tag:${tag}`, key);
77
- }
78
- }
79
- core_1.default.debug(`Redis cache set with tags: ${key} (TTL: ${ttl}s, Tags: ${tags?.join(', ')})`);
80
- }
81
- /**
82
- * Delete a value from cache
83
- */
84
- async delete(key) {
85
- this.mockStore.delete(key);
86
- core_1.default.debug(`Redis cache deleted: ${key}`);
87
- // In production:
88
- // await this.client.del(key);
89
- }
90
- /**
91
- * Delete entries by tag
92
- */
93
- async deleteByTag(tag) {
94
- const keys = this.tagIndex.get(tag);
95
- if (!keys) {
96
- return;
97
- }
98
- for (const key of keys) {
99
- await this.delete(key);
100
- }
101
- this.tagIndex.delete(tag);
102
- core_1.default.debug(`Redis cache invalidated by tag: ${tag} (${keys.size} entries)`);
103
- // In production:
104
- // const keys = await this.client.smembers(`tag:${tag}`);
105
- // if (keys.length > 0) {
106
- // await this.client.del(...keys);
107
- // await this.client.del(`tag:${tag}`);
108
- // }
109
- }
110
- /**
111
- * Check if key exists in cache
112
- */
113
- async has(key) {
114
- const entry = this.mockStore.get(key);
115
- if (!entry) {
116
- return false;
117
- }
118
- // Check if expired
119
- if (Date.now() > entry.expiresAt) {
120
- this.mockStore.delete(key);
121
- return false;
122
- }
123
- return true;
124
- // In production:
125
- // return (await this.client.exists(key)) === 1;
126
- }
127
- /**
128
- * Clear all cache entries
129
- */
130
- async clear() {
131
- this.mockStore.clear();
132
- this.tagIndex.clear();
133
- core_1.default.info('Redis cache cleared');
134
- // In production:
135
- // await this.client.flushdb();
136
- }
137
- /**
138
- * Get all keys matching a pattern
139
- */
140
- async keys(pattern) {
141
- const allKeys = Array.from(this.mockStore.keys());
142
- if (!pattern) {
143
- return allKeys;
144
- }
145
- // Simple pattern matching
146
- const regex = new RegExp('^' + pattern.replace(/\*/g, '.*') + '$');
147
- return allKeys.filter((key) => regex.test(key));
148
- // In production:
149
- // return await this.client.keys(pattern || '*');
150
- }
151
- /**
152
- * Get cache statistics
153
- */
154
- async getStats() {
155
- const total = this.stats.hits + this.stats.misses;
156
- const hitRate = total > 0 ? (this.stats.hits / total) * 100 : 0;
157
- return {
158
- hits: this.stats.hits,
159
- misses: this.stats.misses,
160
- hitRate: Math.round(hitRate * 100) / 100,
161
- size: this.mockStore.size,
162
- };
163
- // In production:
164
- // const info = await this.client.info('stats');
165
- // Parse info and return stats
166
- }
167
- /**
168
- * Reset statistics
169
- */
170
- resetStats() {
171
- this.stats.hits = 0;
172
- this.stats.misses = 0;
173
- }
174
- /**
175
- * Disconnect from Redis
176
- */
177
- async disconnect() {
178
- this.mockStore.clear();
179
- this.tagIndex.clear();
180
- core_1.default.info('Redis cache disconnected');
181
- // In production:
182
- // await this.client.quit();
183
- }
184
- }
185
- exports.RedisCacheStore = RedisCacheStore;