Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node: added OBJECT FREQ command #1542

Merged
merged 7 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Python: Added SINTERCARD command ([#1511](https://github.com/aws/glide-for-redis/pull/1511))
* Python: Added SORT command ([#1439](https://github.com/aws/glide-for-redis/pull/1439))
* Node: Added OBJECT ENCODING command ([#1518](https://github.com/aws/glide-for-redis/pull/1518))
* Node: Added OBJECT FREQ command ([#1542](https://github.com/aws/glide-for-redis/pull/1542))

### Breaking Changes
* Node: Update XREAD to return a Map of Map ([#1494](https://github.com/aws/glide-for-redis/pull/1494))
Expand Down
18 changes: 18 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
createMGet,
createMSet,
createObjectEncoding,
createObjectFreq,
createPExpire,
createPExpireAt,
createPTTL,
Expand Down Expand Up @@ -2398,6 +2399,23 @@ export class BaseClient {
return this.createWritePromise(createObjectEncoding(key));
}

/** Returns the logarithmic access frequency counter of a Redis object stored at `key`.
*
* See https://valkey.io/commands/object-freq for more details.
*
* @param key - The `key` of the object to get the logarithmic access frequency counter of.
* @returns - If `key` exists, returns the logarithmic access frequency counter of the object
* stored at `key` as an integer. Otherwise, returns `null`.
yipin-chen marked this conversation as resolved.
Show resolved Hide resolved
* @example
* ```typescript
* const result = await client.object_freq("my_hash");
* console.log(result); // Output: 2 - The logarithmic access frequency counter of "my_hash".
* ```
*/
public object_freq(key: string): Promise<number | null> {
return this.createWritePromise(createObjectFreq(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 @@ -1375,3 +1375,10 @@ export function createPfAdd(
export function createObjectEncoding(key: string): redis_request.Command {
return createCommand(RequestType.ObjectEncoding, [key]);
}

/**
* @internal
*/
export function createObjectFreq(key: string): redis_request.Command {
return createCommand(RequestType.ObjectFreq, [key]);
}
13 changes: 13 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {
createMGet,
createMSet,
createObjectEncoding,
createObjectFreq,
createPExpire,
createPExpireAt,
createPTTL,
Expand Down Expand Up @@ -1364,6 +1365,18 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
public object_encoding(key: string): T {
return this.addAndReturn(createObjectEncoding(key));
}

/** Returns the logarithmic access frequency counter of a Redis object stored at `key`.
*
* See https://valkey.io/commands/object-freq for more details.
*
* @param key - The `key` of the object to get the logarithmic access frequency counter of.
* Command Response - If `key` exists, returns the logarithmic access frequency counter of
* the object stored at `key` as an integer. Otherwise, returns `null`.
yipin-chen marked this conversation as resolved.
Show resolved Hide resolved
*/
public object_freq(key: string): T {
return this.addAndReturn(createObjectFreq(key));
}
}

/**
Expand Down
41 changes: 41 additions & 0 deletions node/tests/RedisClusterClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,45 @@ describe("RedisClusterClient", () => {
client.close();
},
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
"object freq transaction test_%p",
async (protocol) => {
const client = await RedisClusterClient.createClient(
yipin-chen marked this conversation as resolved.
Show resolved Hide resolved
getClientConfigurationOption(cluster.getAddresses(), protocol),
);

const key = uuidv4();
const maxmemory_policy_key = "maxmemory-policy";
yipin-chen marked this conversation as resolved.
Show resolved Hide resolved
const config = await client.configGet([maxmemory_policy_key]);
const maxmemory_policy = String(config[maxmemory_policy_key]);

try {
const transaction = new ClusterTransaction();
transaction.configSet({
[maxmemory_policy_key]: "allkeys-lfu",
});
transaction.set(key, "foo");
transaction.object_freq(key);

const response = await client.exec(transaction);
expect(response).not.toBeNull();

if (response != null) {
expect(response.length).toEqual(3);
expect(response[0]).toEqual("OK");
expect(response[1]).toEqual("OK");
expect(response[2]).toBeGreaterThanOrEqual(0);
}
} finally {
expect(
await client.configSet({
[maxmemory_policy_key]: maxmemory_policy,
}),
).toEqual("OK");
}

client.close();
},
);
});
35 changes: 35 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2844,6 +2844,41 @@ export function runBaseTests<Context>(config: {
},
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
"object freq test_%p",
async (protocol) => {
await runTest(async (client: BaseClient) => {
const key = uuidv4();
const non_existing_key = uuidv4();
yipin-chen marked this conversation as resolved.
Show resolved Hide resolved
const maxmemory_policy_key = "maxmemory-policy";
const config = await client.configGet([maxmemory_policy_key]);
const maxmemory_policy = String(config[maxmemory_policy_key]);

try {
expect(
await client.configSet({
[maxmemory_policy_key]: "allkeys-lfu",
}),
).toEqual("OK");
expect(await client.object_freq(non_existing_key)).toEqual(
null,
);
expect(await client.set(key, "")).toEqual("OK");
yipin-chen marked this conversation as resolved.
Show resolved Hide resolved
expect(
await client.object_freq(key),
).toBeGreaterThanOrEqual(0);
} finally {
expect(
await client.configSet({
[maxmemory_policy_key]: maxmemory_policy,
}),
).toEqual("OK");
}
}, protocol);
},
config.timeout,
);
}

export function runCommonTests<Context>(config: {
Expand Down
Loading