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 7, 2024
1 parent 54bf451 commit 147778b
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 7 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
* Python, Node: Added TYPE command ([#945](https://github.com/aws/glide-for-redis/pull/945), [#980](https://github.com/aws/glide-for-redis/pull/980))
* Python, Node: Added HLEN command ([#944](https://github.com/aws/glide-for-redis/pull/944), [#981](https://github.com/aws/glide-for-redis/pull/981))
* Python, Node: Added ZCOUNT command ([#878](https://github.com/aws/glide-for-redis/pull/878)) ([#909](https://github.com/aws/glide-for-redis/pull/909))
* Python: Added ECHO command ([#953](https://github.com/aws/glide-for-redis/pull/953))
* Python, Node: Added ECHO command ([#953](https://github.com/aws/glide-for-redis/pull/953), [#1010](https://github.com/aws/glide-for-redis/pull/1010))
* Python, Node: Added ZPOPMIN command ([#975](https://github.com/aws/glide-for-redis/pull/975), [#1008](https://github.com/aws/glide-for-redis/pull/1008))
* Node: Added STRLEN command ([#993](https://github.com/aws/glide-for-redis/pull/993))
* Node: Added LINDEX command ([#999](https://github.com/aws/glide-for-redis/pull/999))
* Python, Node: Added ZPOPMAX command ([#996](https://github.com/aws/glide-for-redis/pull/996), [#1009](https://github.com/aws/glide-for-redis/pull/1009))
* Python: Added ZRANGE command ([#906](https://github.com/aws/glide-for-redis/pull/906))
* Python, Node: Added PTTL command ([#1036](https://github.com/aws/glide-for-redis/pull/1036), [#1082](https://github.com/aws/glide-for-redis/pull/1082))
* Node: Added HVAL command ([#1022](https://github.com/aws/glide-for-redis/pull/1022))
* Node: Added PERSIST command ([#1023](https://github.com/aws/glide-for-redis/pull/1023))

#### Features

Expand Down
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 @@ -200,7 +200,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 @@ -128,6 +128,7 @@ enum RequestType {
Hvals = 84;
PTTL = 85;
ZRemRangeByRank = 86;
Persist = 87;
}

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 @@ -358,6 +358,7 @@ fn get_command(request: &Command) -> Option<Cmd> {
RequestType::Hvals => Some(cmd("HVALS")),
RequestType::PTTL => Some(cmd("PTTL")),
RequestType::ZRemRangeByRank => Some(cmd("ZREMRANGEBYRANK")),
RequestType::Persist => Some(cmd("PERSIST")),
}
}

Expand Down
20 changes: 18 additions & 2 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,
createPttl,
createRPop,
createRPush,
Expand Down Expand Up @@ -1176,7 +1177,7 @@ export class BaseClient {
* Both `start` and `end` are zero-based indexes with 0 being the element with the lowest score.
* These indexes can be negative numbers, where they indicate offsets starting at the element with the highest score.
* See https://redis.io/commands/zremrangebyrank/ for more details.
*
*
* @param key - The key of the sorted set.
* @param start - The starting point of the range.
* @param end - The end of the range.
Expand All @@ -1185,7 +1186,11 @@ export class BaseClient {
* If `end` exceeds the actual end of the sorted set, the range will stop at the actual end of the sorted set.
* If `key` does not exist 0 will be returned.
*/
public zremRangeByRank(key: string, start: number, end: number): Promise<number> {
public zremRangeByRank(
key: string,
start: number,
end: number,
): Promise<number> {
return this.createWritePromise(createZremRangeByRank(key, start, end));
}

Expand All @@ -1212,6 +1217,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
9 changes: 8 additions & 1 deletion node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -897,11 +897,18 @@ export function createPttl(key: string): redis_request.Command {
export function createZremRangeByRank(
key: string,
start: number,
stop: number
stop: number,
): redis_request.Command {
return createCommand(RequestType.ZRemRangeByRank, [
key,
start.toString(),
stop.toString(),
]);
}

/**
* @internal
*/
export function createPersist(key: string): redis_request.Command {
return createCommand(RequestType.Persist, [key]);
}
17 changes: 15 additions & 2 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,
createPttl,
createRPop,
Expand Down Expand Up @@ -941,11 +942,11 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
* Both `start` and `end` are zero-based indexes with 0 being the element with the lowest score.
* These indexes can be negative numbers, where they indicate offsets starting at the element with the highest score.
* See https://redis.io/commands/zremrangebyrank/ for more details.
*
*
* @param key - The key of the sorted set.
* @param start - The starting point of the range.
* @param end - The end of the range.
*
*
* Command Response - The number of members removed.
* If `start` exceeds the end of the sorted set, or if `start` is greater than `end`, 0 returned.
* If `end` exceeds the actual end of the sorted set, the range will stop at the actual end of the sorted set.
Expand All @@ -955,6 +956,18 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createZremRangeByRank(key, start, end));
}

/** 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 @@ -1711,6 +1711,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 147778b

Please sign in to comment.