-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclear-cache.js
More file actions
42 lines (34 loc) · 1021 Bytes
/
clear-cache.js
File metadata and controls
42 lines (34 loc) · 1021 Bytes
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
/**
* 清除Redis缓存脚本
*/
const { createClient } = require('redis');
async function clearCache() {
const redis = createClient({
socket: {
host: process.env.REDIS_HOST || 'localhost',
port: parseInt(process.env.REDIS_PORT || '6379'),
},
password: process.env.REDIS_PASSWORD || undefined,
database: parseInt(process.env.REDIS_DB || '0'),
});
await redis.connect();
try {
console.log('🧹 清除Redis缓存...\n');
// 获取所有缓存键
const keys = await redis.keys('*');
console.log(`📋 找到 ${keys.length} 个缓存键:`);
keys.forEach(key => console.log(` - ${key}`));
if (keys.length > 0) {
// 清除所有缓存
await redis.del(...keys);
console.log(`\n✅ 已清除 ${keys.length} 个缓存键`);
} else {
console.log('\n📭 没有找到缓存键');
}
} catch (error) {
console.error('❌ 清除缓存失败:', error);
} finally {
await redis.quit();
}
}
clearCache();