-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcache-manager.js
More file actions
189 lines (159 loc) · 4.08 KB
/
Copy pathcache-manager.js
File metadata and controls
189 lines (159 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* Cache Manager with LRU Eviction Policy
*
* Provides efficient in-memory caching with automatic eviction based on:
* - Least Recently Used (LRU) policy
* - Maximum size limits
*/
const { LRUCache } = require('lru-cache');
class CacheManager {
/**
* Initialize cache manager
* @param {number} maxSize - Maximum number of items in cache
* @param {number} ttl - Time-to-live in milliseconds (optional)
*/
constructor(maxSize = 10000, ttl = null) {
this.maxSize = maxSize;
this.ttl = ttl;
const options = {
max: maxSize
};
if (ttl) {
options.ttl = ttl;
}
this.cache = new LRUCache(options);
this.hits = 0;
this.misses = 0;
}
/**
* Retrieve item from cache
* @param {string} key - Cache key
* @returns {*} Cached value or undefined if not found
*/
get(key) {
const value = this.cache.get(key);
if (value !== undefined) {
this.hits++;
return value;
}
this.misses++;
return undefined;
}
/**
* Add or update item in cache
* @param {string} key - Cache key
* @param {*} value - Value to cache
*/
set(key, value) {
this.cache.set(key, value);
}
/**
* Remove item from cache
* @param {string} key - Cache key
* @returns {boolean} True if item was removed
*/
delete(key) {
return this.cache.delete(key);
}
/**
* Clear all items from cache
*/
clear() {
this.cache.clear();
this.hits = 0;
this.misses = 0;
}
/**
* Get current cache size
* @returns {number} Current size
*/
size() {
return this.cache.size;
}
/**
* Get cache statistics
* @returns {Object} Cache statistics
*/
getStats() {
const totalRequests = this.hits + this.misses;
const hitRate = totalRequests > 0 ? (this.hits / totalRequests * 100) : 0;
return {
size: this.cache.size,
maxSize: this.maxSize,
hits: this.hits,
misses: this.misses,
hitRate: `${hitRate.toFixed(2)}%`,
totalRequests
};
}
}
class MarketDataCache extends CacheManager {
/**
* Specialized cache for market data with symbol-based keys
*/
/**
* Get order by ID
* @param {string} orderId - Order ID
* @returns {Object|undefined} Order data
*/
getOrder(orderId) {
return this.get(`order:${orderId}`);
}
/**
* Cache order data
* @param {string} orderId - Order ID
* @param {Object} orderData - Order data
*/
setOrder(orderId, orderData) {
this.set(`order:${orderId}`, orderData);
}
/**
* Get cached price for symbol
* @param {string} symbol - Stock symbol
* @returns {number|undefined} Price
*/
getSymbolPrice(symbol) {
return this.get(`price:${symbol}`);
}
/**
* Cache symbol price
* @param {string} symbol - Stock symbol
* @param {number} price - Price
*/
setSymbolPrice(symbol, price) {
this.set(`price:${symbol}`, price);
}
}
// Example usage
if (require.main === module) {
console.log('=== Basic Cache Operations ===');
const cache = new CacheManager(5);
cache.set('key1', 'value1');
cache.set('key2', 'value2');
cache.set('key3', 'value3');
console.log('Get key1:', cache.get('key1'));
console.log('Get key2:', cache.get('key2'));
console.log('Get missing:', cache.get('missing'));
console.log('\nCache stats:', cache.getStats());
// LRU eviction demo
console.log('\n=== LRU Eviction Demo ===');
for (let i = 0; i < 10; i++) {
cache.set(`key${i}`, `value${i}`);
}
console.log(`Cache size: ${cache.size()} (max: ${cache.maxSize})`);
console.log('Oldest keys evicted, newest keys remain');
// Market data cache
console.log('\n=== Market Data Cache ===');
const marketCache = new MarketDataCache(1000);
marketCache.setOrder('ORD001', {
symbol: 'AAPL',
quantity: 100,
price: 150.50,
side: 'BUY'
});
marketCache.setSymbolPrice('AAPL', 150.75);
console.log('Order:', marketCache.getOrder('ORD001'));
console.log('Price:', marketCache.getSymbolPrice('AAPL'));
console.log('\nStats:', marketCache.getStats());
}
module.exports = { CacheManager, MarketDataCache };