-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest-cache-standalone.js
More file actions
121 lines (103 loc) · 4.03 KB
/
test-cache-standalone.js
File metadata and controls
121 lines (103 loc) · 4.03 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
// Standalone test for the token cache implementation
// This simulates the caching logic without requiring the full NestJS server
class TokensController {
constructor() {
this.cachedTokens = null;
this.cacheTimestamp = 0;
this.CACHE_DURATION_MS = 5 * 60 * 1000; // 5 minutes
}
searchTokens(searchQuery) {
if (!searchQuery) {
return {
message: 'No search query provided',
searchQuery: '',
results: []
};
}
// Check if cache is valid (exists and not expired)
const currentTime = Date.now();
const isCacheValid = this.cachedTokens !== null &&
(currentTime - this.cacheTimestamp) < this.CACHE_DURATION_MS;
let tokens;
if (isCacheValid) {
// Use cached tokens
tokens = this.cachedTokens;
console.log('🎯 Cache HIT - Using cached tokens');
} else {
// Cache miss or expired - fetch fresh data
console.log('❌ Cache MISS - Fetching fresh data');
tokens = ['BTC', 'ETH', 'USDT', 'BNB', 'ADA', 'DOT', 'LINK', 'UNI']; // Simulate database query
this.cachedTokens = tokens;
this.cacheTimestamp = currentTime;
}
const filteredTokens = tokens.filter(token =>
token.toLowerCase().includes(searchQuery.toLowerCase())
);
return {
message: `Search results for: ${searchQuery}`,
searchQuery: searchQuery,
results: filteredTokens,
cached: isCacheValid
};
}
// Helper method to clear cache for testing
clearCache() {
this.cachedTokens = null;
this.cacheTimestamp = 0;
console.log('🧹 Cache cleared');
}
// Helper method to expire cache for testing
expireCache() {
this.cacheTimestamp = Date.now() - this.CACHE_DURATION_MS - 1000;
console.log('⏰ Cache expired');
}
}
// Test the caching implementation
async function testCacheImplementation() {
console.log('🚀 Testing Token Cache Implementation\n');
const controller = new TokensController();
console.log('1️⃣ First request (should be cache miss):');
const start1 = Date.now();
const result1 = controller.searchTokens('BT');
const end1 = Date.now();
console.log(` Response time: ${end1 - start1}ms`);
console.log(` Cached: ${result1.cached}`);
console.log(` Results: ${JSON.stringify(result1.results)}\n`);
console.log('2️⃣ Second request (should be cache hit):');
const start2 = Date.now();
const result2 = controller.searchTokens('BT');
const end2 = Date.now();
console.log(` Response time: ${end2 - start2}ms`);
console.log(` Cached: ${result2.cached}`);
console.log(` Results: ${JSON.stringify(result2.results)}\n`);
console.log('3️⃣ Third request with different search (should still be cache hit):');
const start3 = Date.now();
const result3 = controller.searchTokens('ET');
const end3 = Date.now();
console.log(` Response time: ${end3 - start3}ms`);
console.log(` Cached: ${result3.cached}`);
console.log(` Results: ${JSON.stringify(result3.results)}\n`);
console.log('4️⃣ Testing cache expiration:');
controller.expireCache();
const start4 = Date.now();
const result4 = controller.searchTokens('BT');
const end4 = Date.now();
console.log(` Response time: ${end4 - start4}ms`);
console.log(` Cached: ${result4.cached}`);
console.log(` Results: ${JSON.stringify(result4.results)}\n`);
console.log('5️⃣ Testing cache clear:');
controller.clearCache();
const start5 = Date.now();
const result5 = controller.searchTokens('BT');
const end5 = Date.now();
console.log(` Response time: ${end5 - start5}ms`);
console.log(` Cached: ${result5.cached}`);
console.log(` Results: ${JSON.stringify(result5.results)}\n`);
console.log('✅ Cache implementation test completed successfully!');
console.log('\n📊 Summary:');
console.log('- Cache miss requests take longer (simulating database query)');
console.log('- Cache hit requests are faster');
console.log('- Cache expires after 5 minutes');
console.log('- Cache can be cleared manually');
}
testCacheImplementation();