Skip to content

Commit 98c0951

Browse files
author
Adan
committed
Node: added persist command.
1 parent d179378 commit 98c0951

File tree

8 files changed

+52
-1
lines changed

8 files changed

+52
-1
lines changed

glide-core/src/client/value_conversion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ pub(crate) fn expected_type_for_cmd(cmd: &Cmd) -> Option<ExpectedReturnType> {
202202
}
203203
b"INCRBYFLOAT" | b"HINCRBYFLOAT" => Some(ExpectedReturnType::Double),
204204
b"HEXISTS" | b"HSETNX" | b"EXPIRE" | b"EXPIREAT" | b"PEXPIRE" | b"PEXPIREAT"
205-
| b"SISMEMBER" => Some(ExpectedReturnType::Boolean),
205+
| b"SISMEMBER" | b"PERSIST" => Some(ExpectedReturnType::Boolean),
206206
b"SMEMBERS" => Some(ExpectedReturnType::Set),
207207
b"ZSCORE" => Some(ExpectedReturnType::DoubleOrNull),
208208
b"ZPOPMIN" | b"ZPOPMAX" => Some(ExpectedReturnType::MapOfStringToDouble),

glide-core/src/protobuf/redis_request.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ enum RequestType {
127127
SIsMember = 83;
128128
Hvals = 84;
129129
PTTL = 85;
130+
Persist = 86;
130131
}
131132

132133
message Command {

glide-core/src/socket_listener.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ fn get_command(request: &Command) -> Option<Cmd> {
364364
RequestType::SIsMember => Some(cmd("SISMEMBER")),
365365
RequestType::Hvals => Some(cmd("HVALS")),
366366
RequestType::PTTL => Some(cmd("PTTL")),
367+
RequestType::Persist => Some(cmd("PERSIST")),
367368
}
368369
}
369370

node/src/BaseClient.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import {
4747
createMSet,
4848
createPExpire,
4949
createPExpireAt,
50+
createPersist,
5051
createRPop,
5152
createRPush,
5253
createSAdd,
@@ -1183,6 +1184,17 @@ export class BaseClient {
11831184
return this.createWritePromise(createLindex(key, index));
11841185
}
11851186

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

node/src/Commands.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,3 +877,10 @@ export function createZpopmax(key: string, count?: number): redis_request.Comman
877877
export function createEcho(message: string): redis_request.Command {
878878
return createCommand(RequestType.Echo, [message]);
879879
}
880+
881+
/**
882+
* @internal
883+
*/
884+
export function createPersist(key: string): redis_request.Command {
885+
return createCommand(RequestType.Persist, [key]);
886+
}

node/src/Transaction.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {
4848
createMSet,
4949
createPExpire,
5050
createPExpireAt,
51+
createPersist,
5152
createPing,
5253
createRPop,
5354
createRPush,
@@ -924,6 +925,18 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
924925
return this.addAndReturn(createEcho(message));
925926
}
926927

928+
/** Remove the existing timeout on `key`, turning the key from volatile (a key with an expire set) to
929+
* persistent (a key that will never expire as no timeout is associated).
930+
* See https://redis.io/commands/persist/ for more details.
931+
*
932+
* @param key - The key to remove the existing timeout on.
933+
*
934+
* Command Response - false if `key` does not exist or does not have an associated timeout. true if the timeout has been removed.
935+
*/
936+
public persist(key: string): T {
937+
return this.addAndReturn(createPersist(key));
938+
}
939+
927940
/** Executes a single command, without checking inputs. Every part of the command, including subcommands,
928941
* should be added as a separate value in args.
929942
*

node/tests/SharedTests.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,6 +1651,21 @@ export function runBaseTests<Context>(config: {
16511651
},
16521652
config.timeout
16531653
);
1654+
1655+
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
1656+
`persist test_%p`,
1657+
async (protocol) => {
1658+
await runTest(async (client: BaseClient) => {
1659+
const key = uuidv4();
1660+
expect(await client.set(key, "foo")).toEqual("OK");
1661+
expect(await client.persist(key)).toEqual(false);
1662+
1663+
expect(await client.expire(key, 10)).toEqual(true);
1664+
expect(await client.persist(key)).toEqual(true);
1665+
}, protocol);
1666+
},
1667+
config.timeout
1668+
);
16541669
}
16551670

16561671
export function runCommonTests<Context>(config: {

node/tests/TestUtilities.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ export function transactionTest(
7070
args.push("string");
7171
baseTransaction.echo(value);
7272
args.push(value);
73+
baseTransaction.persist(key1);
74+
args.push(false);
7375
baseTransaction.set(key2, "baz", {
7476
returnOldValue: true,
7577
});

0 commit comments

Comments
 (0)