Skip to content

Commit f768f3f

Browse files
authored
refactor: improve clear useless cache (#7)
* refactor: improve clear useless cache * fix: delete only * feat: skip
1 parent 99e28ce commit f768f3f

3 files changed

Lines changed: 62 additions & 7 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"url": "https://github.com/coajs/coa-redis.git"
1616
},
1717
"scripts": {
18-
"dev": "tsc -w",
18+
"tsc": "tsc -w",
1919
"test": "mocha dist/test",
2020
"test:dev": "mocha dist/test --watch",
2121
"build": "rm -rf dist && tsc && cp package.json *.md dist/src",

src/RedisCache.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,31 @@ export class RedisCache {
9797
}
9898

9999
// 清除无效的缓存
100-
async clearUseless() {
100+
async clearUseless(match = '*') {
101101
const now = _.now()
102-
const keys1 = await this.io.keys(this.key('*'))
102+
const keys1 = await this.io.keys(this.key(match))
103+
const result = {} as Record<string, [number, number]>
104+
// 循环处理每一个key
103105
for (const key1 of keys1) {
106+
// 按1000分组
104107
const keys2 = await this.io.hkeys(key1)
105-
for (const key2 of keys2) {
106-
const value = (await this.io.hget(key1, key2)) ?? ''
107-
const expire = _.toInteger(value.substr(1, 13))
108-
if (expire < now) await this.io.hdel(key1, key2)
108+
const keys2Chunks = _.chunk(keys2, 1000)
109+
result[key1] = [0, keys2.length]
110+
for (const keys2 of keys2Chunks) {
111+
// 批量获取
112+
const values = await this.io.hmget(key1, keys2)
113+
const deleteIds = [] as string[]
114+
// 判断是否过期
115+
_.forEach(values, (value, index) => {
116+
const expire = _.toInteger((value || '').substring(1, 14))
117+
if (expire < now) deleteIds.push(keys2[index])
118+
})
119+
// 删除过期的
120+
if (deleteIds.length) await this.io.hdel(key1, ...deleteIds)
121+
result[key1][0] += deleteIds.length
109122
}
110123
}
124+
return result
111125
}
112126

113127
// 清除指定命名空间的缓存

test/RedisCache.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* eslint-disable @typescript-eslint/no-unused-expressions */
2+
3+
import { RedisBin, RedisCache } from '../src'
4+
5+
const redisConfig = {
6+
// 服务器地址
7+
host: '127.0.0.1',
8+
// 端口
9+
port: 6379,
10+
// 密码,若无填空字符串
11+
password: '',
12+
// 数据库,默认为0
13+
db: 0,
14+
// 键前缀,可区分不同的项目
15+
prefix: 'test___pre_',
16+
// 是否回显查询语句
17+
trace: false,
18+
}
19+
20+
// 一般一个数据库只需要使用一个实例,内部会管理连接池,无需创建多个
21+
const redisBin = new RedisBin(redisConfig)
22+
23+
// 创建一个缓存实例
24+
const redisCache = new RedisCache(redisBin)
25+
26+
const nsp1 = 'NSP_1'
27+
28+
describe.skip('RedisCache class test', function () {
29+
it('init data', async () => {
30+
await redisCache.mSet(nsp1, { a: 1, b: 2, c: 3 }, 1)
31+
await redisCache.mSet(nsp1, { a1: 1, b1: 2, c1: 3 }, 10)
32+
await redisCache.delete(nsp1, ['b', 'c1'])
33+
await redisCache.set(nsp1, 'b', 'v-b')
34+
await redisCache.set(nsp1, 'c1', 'v-c1')
35+
})
36+
37+
it('clear useless cache', async () => {
38+
const res = await redisCache.clearUseless('*')
39+
console.log(res)
40+
})
41+
})

0 commit comments

Comments
 (0)