Skip to content

Commit

Permalink
Node: added persist command.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adan committed Mar 6, 2024
1 parent d179378 commit 98c0951
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion glide-core/src/client/value_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ pub(crate) fn expected_type_for_cmd(cmd: &Cmd) -> Option<ExpectedReturnType> {
}
b"INCRBYFLOAT" | b"HINCRBYFLOAT" => Some(ExpectedReturnType::Double),
b"HEXISTS" | b"HSETNX" | b"EXPIRE" | b"EXPIREAT" | b"PEXPIRE" | b"PEXPIREAT"
| b"SISMEMBER" => Some(ExpectedReturnType::Boolean),
| b"SISMEMBER" | b"PERSIST" => Some(ExpectedReturnType::Boolean),
b"SMEMBERS" => Some(ExpectedReturnType::Set),
b"ZSCORE" => Some(ExpectedReturnType::DoubleOrNull),
b"ZPOPMIN" | b"ZPOPMAX" => Some(ExpectedReturnType::MapOfStringToDouble),
Expand Down
1 change: 1 addition & 0 deletions glide-core/src/protobuf/redis_request.proto
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ enum RequestType {
SIsMember = 83;
Hvals = 84;
PTTL = 85;
Persist = 86;
}

message Command {
Expand Down
1 change: 1 addition & 0 deletions glide-core/src/socket_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ fn get_command(request: &Command) -> Option<Cmd> {
RequestType::SIsMember => Some(cmd("SISMEMBER")),
RequestType::Hvals => Some(cmd("HVALS")),
RequestType::PTTL => Some(cmd("PTTL")),
RequestType::Persist => Some(cmd("PERSIST")),
}
}

Expand Down
12 changes: 12 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
createMSet,
createPExpire,
createPExpireAt,
createPersist,
createRPop,
createRPush,
createSAdd,
Expand Down Expand Up @@ -1183,6 +1184,17 @@ export class BaseClient {
return this.createWritePromise(createLindex(key, index));
}

/** Remove the existing timeout on `key`, turning the key from volatile (a key with an expire set) to
* persistent (a key that will never expire as no timeout is associated).
* See https://redis.io/commands/persist/ for more details.
*
* @param key - The key to remove the existing timeout on.
* @returns false if `key` does not exist or does not have an associated timeout. true if the timeout has been removed.
*/
public persist(key: string): Promise<Boolean> {
return this.createWritePromise(createPersist(key));
}

/**
* @internal
*/
Expand Down
7 changes: 7 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -877,3 +877,10 @@ export function createZpopmax(key: string, count?: number): redis_request.Comman
export function createEcho(message: string): redis_request.Command {
return createCommand(RequestType.Echo, [message]);
}

/**
* @internal
*/
export function createPersist(key: string): redis_request.Command {
return createCommand(RequestType.Persist, [key]);
}
13 changes: 13 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
createMSet,
createPExpire,
createPExpireAt,
createPersist,
createPing,
createRPop,
createRPush,
Expand Down Expand Up @@ -924,6 +925,18 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createEcho(message));
}

/** Remove the existing timeout on `key`, turning the key from volatile (a key with an expire set) to
* persistent (a key that will never expire as no timeout is associated).
* See https://redis.io/commands/persist/ for more details.
*
* @param key - The key to remove the existing timeout on.
*
* Command Response - false if `key` does not exist or does not have an associated timeout. true if the timeout has been removed.
*/
public persist(key: string): T {
return this.addAndReturn(createPersist(key));
}

/** Executes a single command, without checking inputs. Every part of the command, including subcommands,
* should be added as a separate value in args.
*
Expand Down
15 changes: 15 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1651,6 +1651,21 @@ export function runBaseTests<Context>(config: {
},
config.timeout
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`persist test_%p`,
async (protocol) => {
await runTest(async (client: BaseClient) => {
const key = uuidv4();
expect(await client.set(key, "foo")).toEqual("OK");
expect(await client.persist(key)).toEqual(false);

expect(await client.expire(key, 10)).toEqual(true);
expect(await client.persist(key)).toEqual(true);
}, protocol);
},
config.timeout
);
}

export function runCommonTests<Context>(config: {
Expand Down
2 changes: 2 additions & 0 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export function transactionTest(
args.push("string");
baseTransaction.echo(value);
args.push(value);
baseTransaction.persist(key1);
args.push(false);
baseTransaction.set(key2, "baz", {
returnOldValue: true,
});
Expand Down

0 comments on commit 98c0951

Please sign in to comment.