From 046b480e13b0a716c99569b67d29864d40d6e64b Mon Sep 17 00:00:00 2001 From: Jesse Zhu Date: Tue, 14 Nov 2023 06:36:23 +0800 Subject: [PATCH 1/2] Bump version to 1.2.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f4f8989..14fd061 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "image-optimizer", - "version": "1.2.1", + "version": "1.2.2", "main": "index.js", "license": "MIT", "scripts": { From d6f4754fbab8be0076eb32329b47ce7d88947333 Mon Sep 17 00:00:00 2001 From: Jesse Zhu Date: Tue, 14 Nov 2023 06:36:45 +0800 Subject: [PATCH 2/2] refactor: Simplify caching process by removing locker mechanism - Simplify caching process in the `Cache` class within `cache.ts`. - Remove redundant lock management mechanism from `get` and `set` methods. - Enhance efficiency by eliminating `cacheLocker` from `Cache` class. --- src/lib/cache.ts | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/src/lib/cache.ts b/src/lib/cache.ts index eef6879..15106c0 100644 --- a/src/lib/cache.ts +++ b/src/lib/cache.ts @@ -20,20 +20,12 @@ const getCacheFilePath = (hash: string) => { export class Cache { private key: string - private cacheLocker: Locker constructor(params: CachaParams) { this.key = `image_cache:${hash(params)}` - this.cacheLocker = new Locker(params) } get = async (): Promise<[null] | [string, number]> => { - while (await this.cacheLocker.isLocked()) { - await delay(10) - } - const [_, cached] = await Promise.all([ - this.cacheLocker.lock(), - redisClient.hgetall(this.key), - ]) + const cached = await redisClient.hgetall(this.key) const { file, timestamp } = cached if (!file) return [null] const filePath = getCacheFilePath(file) @@ -44,19 +36,12 @@ export class Cache { ]) return [null] } - await Promise.all([ - redisClient.zincrby('cache_access_count', 1, this.key), - this.cacheLocker.unlock(), - ]) + await redisClient.zincrby('cache_access_count', 1, this.key) return [filePath, Math.floor((Date.now() - parseInt(timestamp)) / 1000)] } set = (data: PassThrough) => new Promise(async (resolve, reject) => { - while (await this.cacheLocker.isLocked()) { - await delay(10) - } - await this.cacheLocker.lock() const bufs = [] const cipher = crypto.createHash('sha1') data.on('data', (chunk) => { @@ -84,8 +69,6 @@ export class Cache { } catch (err) { logger.error('Error while create image cache: ', err) reject(err) - } finally { - await this.cacheLocker.unlock() } }) })