-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
get
method to store (#195)
- Loading branch information
1 parent
102bbc2
commit f1880d9
Showing
4 changed files
with
171 additions
and
83 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
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,35 @@ | ||
// /source/scripts.ts | ||
// The lua scripts for the increment and get operations. | ||
|
||
/** | ||
* The lua scripts, used to make consecutive queries on the same key and avoid | ||
* race conditions by doing all the work on the redis server. | ||
*/ | ||
const scripts = { | ||
increment: ` | ||
local totalHits = redis.call("INCR", KEYS[1]) | ||
local timeToExpire = redis.call("PTTL", KEYS[1]) | ||
if timeToExpire <= 0 or ARGV[1] == "1" | ||
then | ||
redis.call("PEXPIRE", KEYS[1], tonumber(ARGV[2])) | ||
timeToExpire = tonumber(ARGV[2]) | ||
end | ||
return { totalHits, timeToExpire } | ||
` | ||
// Ensure that code changes that affect whitespace do not affect | ||
// the script contents. | ||
.replaceAll(/^\s+/gm, '') | ||
.trim(), | ||
get: ` | ||
local totalHits = redis.call("GET", KEYS[1]) | ||
local timeToExpire = redis.call("PTTL", KEYS[1]) | ||
return { totalHits, timeToExpire } | ||
` | ||
.replaceAll(/^\s+/gm, '') | ||
.trim(), | ||
} | ||
|
||
// Export them so we can use them in the `lib.ts` file. | ||
export default scripts |
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