-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add incrementBatch
support
#17
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,9 +1,11 @@ | ||
import { | ||
_stringMapEntries, | ||
AnyObject, | ||
CommonLogger, | ||
NullableBuffer, | ||
NullableString, | ||
Promisable, | ||
StringMap, | ||
UnixTimestampNumber, | ||
} from '@naturalcycles/js-lib' | ||
import { ReadableTyped } from '@naturalcycles/nodejs-lib' | ||
|
@@ -157,6 +159,25 @@ export class RedisClient implements CommonClient { | |
return await this.redis().hincrby(key, field, increment) | ||
} | ||
|
||
async hincrBatch(key: string, incrementTuples: [string, number][]): Promise<[string, number][]> { | ||
const results: StringMap<number | undefined> = {} | ||
|
||
await this.withPipeline(async pipeline => { | ||
for (const [field, increment] of incrementTuples) { | ||
pipeline.hincrby(key, field, increment, (_err, newValue) => { | ||
results[field] = newValue | ||
}) | ||
} | ||
}) | ||
|
||
const validResults = _stringMapEntries(results).filter(([_, v]) => v !== undefined) as [ | ||
string, | ||
number, | ||
][] | ||
|
||
return validResults | ||
} | ||
|
||
async setWithTTL( | ||
key: string, | ||
value: string | number | Buffer, | ||
|
@@ -165,14 +186,19 @@ export class RedisClient implements CommonClient { | |
await this.redis().set(key, value, 'EXAT', expireAt) | ||
} | ||
|
||
async hsetWithTTL(key: string, value: AnyObject, expireAt: UnixTimestampNumber): Promise<void> { | ||
const valueKeys = Object.keys(value) | ||
const numberOfKeys = valueKeys.length | ||
const keyList = valueKeys.join(' ') | ||
const commandString = `HEXPIREAT ${key} ${expireAt} FIELDS ${numberOfKeys} ${keyList}` | ||
const [command, ...args] = commandString.split(' ') | ||
await this.redis().hset(key, value) | ||
await this.redis().call(command!, args) | ||
async hsetWithTTL( | ||
_key: string, | ||
_value: AnyObject, | ||
_expireAt: UnixTimestampNumber, | ||
): Promise<void> { | ||
throw new Error('Not supported until Redis 7.4.0') | ||
// const valueKeys = Object.keys(value) | ||
// const numberOfKeys = valueKeys.length | ||
// const keyList = valueKeys.join(' ') | ||
// const commandString = `HEXPIREAT ${key} ${expireAt} FIELDS ${numberOfKeys} ${keyList}` | ||
// const [command, ...args] = commandString.split(' ') | ||
// await this.redis().hset(key, value) | ||
// await this.redis().call(command!, args) | ||
Comment on lines
+194
to
+201
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thinking open source, this is probably not a good idea. On the other hand, none of OUR usually accessible redises support it. |
||
} | ||
|
||
async mset(obj: Record<string, string | number>): Promise<void> { | ||
|
@@ -187,6 +213,25 @@ export class RedisClient implements CommonClient { | |
return await this.redis().incrby(key, by) | ||
} | ||
|
||
async incrBatch(incrementTuples: [string, number][]): Promise<[string, number][]> { | ||
const results: StringMap<number | undefined> = {} | ||
|
||
await this.withPipeline(async pipeline => { | ||
for (const [key, increment] of incrementTuples) { | ||
pipeline.incrby(key, increment, (_err, newValue) => { | ||
results[key] = newValue | ||
}) | ||
} | ||
}) | ||
|
||
const validResults = _stringMapEntries(results).filter(([_, v]) => v !== undefined) as [ | ||
string, | ||
number, | ||
][] | ||
|
||
return validResults | ||
} | ||
|
||
async ttl(key: string): Promise<number> { | ||
return await this.redis().ttl(key) | ||
} | ||
|
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the batch support into our client-wrapper, because the
withPipeline()
exposes the underlying lib, and I don't think that consumers of our wrapper should be using that (if avoidable).The consuming logic (
redis*KeyValueDb.incrementBatch
) has also its own business translating the keys - so this separation of concerns (here we pipe, there we translate ids) is also a plus,