Skip to content

Commit

Permalink
refactor: improve clear useless cache (#7)
Browse files Browse the repository at this point in the history
* refactor: improve clear useless cache

* fix: delete only

* feat: skip
  • Loading branch information
adaex authored May 14, 2022
1 parent 99e28ce commit f768f3f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"url": "https://github.com/coajs/coa-redis.git"
},
"scripts": {
"dev": "tsc -w",
"tsc": "tsc -w",
"test": "mocha dist/test",
"test:dev": "mocha dist/test --watch",
"build": "rm -rf dist && tsc && cp package.json *.md dist/src",
Expand Down
26 changes: 20 additions & 6 deletions src/RedisCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,31 @@ export class RedisCache {
}

// 清除无效的缓存
async clearUseless() {
async clearUseless(match = '*') {
const now = _.now()
const keys1 = await this.io.keys(this.key('*'))
const keys1 = await this.io.keys(this.key(match))
const result = {} as Record<string, [number, number]>
// 循环处理每一个key
for (const key1 of keys1) {
// 按1000分组
const keys2 = await this.io.hkeys(key1)
for (const key2 of keys2) {
const value = (await this.io.hget(key1, key2)) ?? ''
const expire = _.toInteger(value.substr(1, 13))
if (expire < now) await this.io.hdel(key1, key2)
const keys2Chunks = _.chunk(keys2, 1000)
result[key1] = [0, keys2.length]
for (const keys2 of keys2Chunks) {
// 批量获取
const values = await this.io.hmget(key1, keys2)
const deleteIds = [] as string[]
// 判断是否过期
_.forEach(values, (value, index) => {
const expire = _.toInteger((value || '').substring(1, 14))
if (expire < now) deleteIds.push(keys2[index])
})
// 删除过期的
if (deleteIds.length) await this.io.hdel(key1, ...deleteIds)
result[key1][0] += deleteIds.length
}
}
return result
}

// 清除指定命名空间的缓存
Expand Down
41 changes: 41 additions & 0 deletions test/RedisCache.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* eslint-disable @typescript-eslint/no-unused-expressions */

import { RedisBin, RedisCache } from '../src'

const redisConfig = {
// 服务器地址
host: '127.0.0.1',
// 端口
port: 6379,
// 密码,若无填空字符串
password: '',
// 数据库,默认为0
db: 0,
// 键前缀,可区分不同的项目
prefix: 'test___pre_',
// 是否回显查询语句
trace: false,
}

// 一般一个数据库只需要使用一个实例,内部会管理连接池,无需创建多个
const redisBin = new RedisBin(redisConfig)

// 创建一个缓存实例
const redisCache = new RedisCache(redisBin)

const nsp1 = 'NSP_1'

describe.skip('RedisCache class test', function () {
it('init data', async () => {
await redisCache.mSet(nsp1, { a: 1, b: 2, c: 3 }, 1)
await redisCache.mSet(nsp1, { a1: 1, b1: 2, c1: 3 }, 10)
await redisCache.delete(nsp1, ['b', 'c1'])
await redisCache.set(nsp1, 'b', 'v-b')
await redisCache.set(nsp1, 'c1', 'v-c1')
})

it('clear useless cache', async () => {
const res = await redisCache.clearUseless('*')
console.log(res)
})
})

0 comments on commit f768f3f

Please sign in to comment.