Skip to content

Commit

Permalink
feat: support EXAT and PXAT options of the SET command
Browse files Browse the repository at this point in the history
  • Loading branch information
uki00a committed Oct 21, 2024
1 parent 1672157 commit 99646aa
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
12 changes: 12 additions & 0 deletions command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ export type StralgoTarget = "KEYS" | "STRINGS";
export interface SetOpts {
ex?: number;
px?: number;
/**
* Sets `EXAT` option.
*
* `EXAT` option was added in Redis v6.2.
*/
exat?: number;
/**
* Sets `PXAT` option.
*
* `PXAT` option was added in Redis v6.2.
*/
pxat?: number;
keepttl?: boolean;
/**
* Enables `GET` option.
Expand Down
10 changes: 8 additions & 2 deletions redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1364,14 +1364,20 @@ class RedisImpl implements Redis {
| SetWithModeOpts,
) {
const args: RedisValue[] = [key, value];
if (opts?.ex !== undefined) {
if (opts?.ex != null) {
args.push("EX", opts.ex);
} else if (opts?.px !== undefined) {
} else if (opts?.px != null) {
args.push("PX", opts.px);
} else if (opts?.exat != null) {
args.push("EXAT", opts.exat);
} else if (opts?.pxat != null) {
args.push("PXAT", opts.pxat);
}
// TODO: Isn't `KEEPTTL` option exclusive with `EX`, `PX`, etc.?
if (opts?.keepttl) {
args.push("KEEPTTL");
}

let isAbleToReturnNil = false;
if ((opts as SetWithModeOpts)?.mode) {
args.push((opts as SetWithModeOpts).mode);
Expand Down
22 changes: 20 additions & 2 deletions tests/commands/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
describe,
it,
} from "../../deps/std/testing.ts";
import type { Connector, TestServer } from "../test_util.ts";
import {
type Connector,
type TestServer,
usesRedisVersion,
} from "../test_util.ts";
import type { Redis } from "../../mod.ts";

export function stringTests(
Expand Down Expand Up @@ -205,6 +209,20 @@ export function stringTests(
assertEquals(v2, null);
assertType<IsNullable<typeof v2>>(true);
});

it("supports `EXAT` option", async () => {
const t = Math.floor(new Date().valueOf() / 1000);
await client.set("setWithEXAT", "foo", { exat: t });
const ttl = await client.ttl("setWithEXAT");
assertEquals(t, ttl);
});

it("supports `PXAT` option", async () => {
const t = new Date().valueOf();
await client.set("setWithPXAT", "bar", { pxat: t });
const ttl = await client.ttl("setWithPXAT");
assertEquals(t, ttl);
});
});

it("setbit", async () => {
Expand Down Expand Up @@ -244,7 +262,7 @@ export function stringTests(

it("stralgo", {
// NOTE(#454): STRALGO has been dropped
ignore: !Deno.env.get("REDIS_VERSION")?.startsWith("6."),
ignore: !usesRedisVersion("6"),
}, async () => {
await client.set("a", "Hello");
await client.set("b", "Deno!");
Expand Down
4 changes: 4 additions & 0 deletions tests/test_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,7 @@ function tempPath(fileName: string): string {
const url = new URL(`./tmp/${fileName}`, import.meta.url);
return url.pathname;
}

export function usesRedisVersion(version: "6" | "7"): boolean {
return !!Deno.env.get("REDIS_VERSION")?.startsWith(`${version}.`);
}

0 comments on commit 99646aa

Please sign in to comment.