-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from mergermarket/redis-4
support Redis 4 client
- Loading branch information
Showing
8 changed files
with
7,285 additions
and
3,924 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
{ | ||
"extends": ["@acuris"] | ||
} | ||
"extends": [ | ||
"@acuris" | ||
], | ||
"parserOptions": { | ||
"project": [ | ||
"./tsconfig.json", | ||
"./tsconfig-test.json" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,44 @@ | ||
import { CacheStore, CacheItem, Cacheable } from '../types' | ||
import { RedisClient } from 'redis' | ||
import { RedisClientType } from 'redis' | ||
import { v4 as uuidV4 } from 'uuid' | ||
|
||
function lockKey(key: string): string { | ||
return `LOCK-${key}` | ||
} | ||
|
||
export class RedisCacheStore<T extends Cacheable = Cacheable> implements CacheStore<T> { | ||
public constructor(private redisClient: RedisClient) {} | ||
public constructor(private redisClient: RedisClientType<any>) {} | ||
|
||
public get(key: string): Promise<CacheItem<T> | null> { | ||
return new Promise<CacheItem<T> | null>((resolve, reject) => { | ||
this.redisClient.get(key, (error, result) => { | ||
if (error) { | ||
reject(error) | ||
} | ||
if (!result) { | ||
resolve(null) | ||
} | ||
resolve(JSON.parse(result)) | ||
}) | ||
}) | ||
public async get(key: string): Promise<CacheItem<T> | null> { | ||
const result = await this.redisClient.get(key) | ||
return JSON.parse(result) | ||
} | ||
|
||
public set(key: string, data: CacheItem<T>, ttl: number): Promise<boolean> { | ||
return new Promise<boolean>((resolve, reject) => { | ||
this.redisClient.set(key, JSON.stringify(data), 'PX', ttl, (error, result) => { | ||
if (error) { | ||
reject(error) | ||
} | ||
if (result === 'OK') { | ||
resolve(true) | ||
} | ||
resolve(false) | ||
}) | ||
}) | ||
public async set(key: string, data: CacheItem<T>, ttl: number): Promise<boolean> { | ||
const result = await this.redisClient.set(key, JSON.stringify(data), { PX: ttl }) | ||
return result === 'OK' | ||
} | ||
|
||
public del(key: string): Promise<boolean> { | ||
return new Promise<boolean>((resolve, reject) => { | ||
this.redisClient.del(key, (error, res) => { | ||
if (error) { | ||
reject(error) | ||
} | ||
resolve(res > 0) | ||
}) | ||
}) | ||
public async del(key: string): Promise<boolean> { | ||
const result = await this.redisClient.del(key) | ||
return result > 0 | ||
} | ||
|
||
public lock(key: string, ttl: number): Promise<string | false> { | ||
public async lock(key: string, ttl: number): Promise<string | false> { | ||
const lockId = uuidV4() | ||
return new Promise<string | false>((resolve, reject) => { | ||
this.redisClient.set(lockKey(key), lockId, 'PX', ttl, 'NX', (error, result) => { | ||
if (error) { | ||
reject(error) | ||
} | ||
if (result === 'OK') { | ||
resolve(lockId) | ||
} | ||
resolve(false) | ||
}) | ||
}) | ||
const result = await this.redisClient.set(lockKey(key), lockId, { PX: ttl, NX: true }) | ||
if (result === 'OK') { | ||
return lockId | ||
} | ||
return false | ||
} | ||
|
||
public unlock(key: string, lockId: string): Promise<boolean> { | ||
return new Promise((resolve, reject) => { | ||
this.redisClient.get(lockKey(key), (error, result) => { | ||
if (!error && result && result === lockId) { | ||
this.redisClient.del(lockKey(key), err => { | ||
if (err) { | ||
reject(err) | ||
} | ||
resolve(true) | ||
}) | ||
} else { | ||
resolve(false) | ||
} | ||
}) | ||
}) | ||
public async unlock(key: string, lockId: string): Promise<boolean> { | ||
const result = await this.redisClient.get(lockKey(key)) | ||
if (result && result === lockId) { | ||
await this.redisClient.del(lockKey(key)) | ||
return true | ||
} | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"extends": "@acuris/eslint-config/tsconfig.json", | ||
"compilerOptions": { | ||
"noEmit": false, | ||
"module": "commonjs", | ||
"target": "es2017", | ||
"declaration": true, | ||
"outDir": "./dist-test" | ||
}, | ||
"include": ["test/**/*"] | ||
} |