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

Client Side Caching #2854

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
last change from review
  • Loading branch information
sjpotter committed Nov 7, 2024
commit 97176f427104f97763bb72ce86bd8391169f971d
27 changes: 12 additions & 15 deletions packages/client/lib/client/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { BasicCommandParser } from './parser';
type CachingClient = RedisClient<any, any, any, any, any>;
type CmdFunc = () => Promise<ReplyUnion>;

type EvictionPolicy = "LRU" | "FIFO"

export interface ClientSideCacheConfig {
ttl?: number;
maxEntries?: number;
lru?: boolean;
evictPolocy?: EvictionPolicy;
}

type CacheCreator = {
Expand Down Expand Up @@ -107,7 +109,7 @@ export class BasicClientSideCache extends ClientSideCacheProvider {
this.#keyToCacheKeySetMap = new Map<string, Set<string>>();
this.ttl = config?.ttl ?? 0;
this.maxEntries = config?.maxEntries ?? 0;
this.lru = config?.lru ?? true;
this.lru = config?.evictPolocy !== "FIFO"
}

/* logic of how caching works:
Expand Down Expand Up @@ -165,6 +167,7 @@ export class BasicClientSideCache extends ClientSideCacheProvider {
if (cacheEntry.validate()) { // on error, have to remove promise from cache
this.delete(cacheKey!);
}

throw err;
}
}
Expand Down Expand Up @@ -217,25 +220,21 @@ export class BasicClientSideCache extends ClientSideCacheProvider {
this.emit('invalidate', key);
}

override clear(reset = true) {
override clear(resetStats = true) {
this.#cacheKeyToEntryMap.clear();
this.#keyToCacheKeySetMap.clear();
if (reset) {
if (resetStats) {
this.#cacheHits = 0;
this.#cacheMisses = 0;
}
}

get(cacheKey?: string | undefined) {
if (cacheKey === undefined) {
return undefined
}

get(cacheKey: string) {
const val = this.#cacheKeyToEntryMap.get(cacheKey);

if (val && !val.validate()) {
this.delete(cacheKey);
this.emit("invalidate", cacheKey);
this.emit("cache-evict", cacheKey);

return undefined;
}
Expand Down Expand Up @@ -263,6 +262,7 @@ export class BasicClientSideCache extends ClientSideCacheProvider {
set(cacheKey: string, cacheEntry: ClientSideCacheEntry, keys: Array<RedisArgument>) {
let count = this.#cacheKeyToEntryMap.size;
const oldEntry = this.#cacheKeyToEntryMap.get(cacheKey);

if (oldEntry) {
count--; // overwriting, so not incrementig
oldEntry.invalidate();
Expand Down Expand Up @@ -419,11 +419,8 @@ class PooledClientSideCacheEntryPromise extends ClientSideCacheEntryPromise {

override validate(): boolean {
let ret = super.validate();
if (this.#creator) {
ret = ret && this.#creator.client.isReady && this.#creator.client.socketEpoch == this.#creator.epoch
}

return ret;

return ret && this.#creator.client.isReady && this.#creator.client.socketEpoch == this.#creator.epoch
}
}

Expand Down