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

Cache Support on top of cmd parser changes #2738

Draft
wants to merge 11 commits into
base: v5
Choose a base branch
from
34 changes: 30 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/client/lib/RESP/encoder.ts
Expand Up @@ -2,7 +2,7 @@ import { RedisArgument } from './types';

const CRLF = '\r\n';

export default function encodeCommand(args: Array<RedisArgument>): Array<RedisArgument> {
export default function encodeCommand(args: ReadonlyArray<RedisArgument>): ReadonlyArray<RedisArgument> {
const toWrite: Array<RedisArgument> = [];

let strings = '*' + args.length + CRLF;
Expand Down
2 changes: 2 additions & 0 deletions packages/client/lib/RESP/types.ts
@@ -1,3 +1,4 @@
import { CommandParser } from '../client/parser';
import { BlobError, SimpleError } from '../errors';
import { RedisScriptConfig, SHA1 } from '../lua-script';
import { RESP_TYPES } from './decoder';
Expand Down Expand Up @@ -272,6 +273,7 @@ export type Command = {
*/
IS_FORWARD_COMMAND?: boolean;
// POLICIES?: CommandPolicies;
parseCommand?(this: void, parser: CommandParser, ...args: Array<any>): void;
transformArguments(this: void, ...args: Array<any>): CommandArguments;
TRANSFORM_LEGACY_REPLY?: boolean;
transformReply: TransformReply | Record<RespVersions, TransformReply>;
Expand Down
64 changes: 64 additions & 0 deletions packages/client/lib/client/README-cache.md
@@ -0,0 +1,64 @@
# Client Side Caching Support

Client Side Caching enables Redis Servers and Clients to work together to enable a client to cache results from command sent to a server and be informed by the server when the cached result is no longer valid.

## Usage

node-redis supports two ways of instantiating client side caching support

Note: Client Side Caching is only supported with RESP3.

### Anonymous Cache

```javascript
const client = createClient({RESP: 3, clientSideCache: {ttl: 0, maxEntries: 0, lru: false}})
```

In this instance, the cache is opaque to the user, and they have no control over it.

### Controllable Cache

```javascript
const ttl = 0, maxEntries = 0, lru = false;
const cache = new BasicClientSideCache(ttl, maxEntries, lru);
const client = createClient({RESP: 3, clientSideCache: cache});
```

In this instance, the user has full control over the cache, as they have access to the cache object.

They can manually invalidate keys

```javascript
cache.invalidate(key);
```

they can clear the entire cache
g
```javascript
cache.clear();
```

as well as get cache metrics

```typescript
const hits: number = cache.cacheHits();
const misses: number = cache.cacheMisses();
```

## Pooled Caching

Similar to individual clients, node-redis also supports caching for its pooled client object, with the cache being able to be instantiated in an anonymous manner or a controllable manner.

### Anonymous Cache

```javascript
const client = createClientPool({RESP: 3}, {clientSideCache: {ttl: 0, maxEntries: 0, lru: false}, minimum: 8});
```

### Controllable Cache

```javascript
const ttl = 0, maxEntries = 0, lru = false;
const cache = new BasicPooledClientSideCache(ttl, maxEntries, lru);
const client = createClientPool({RESP: 3}, {clientSideCache: cache, minimum: 8});
```