Skip to content

Commit

Permalink
fix(ttl): allow overwriting ttls
Browse files Browse the repository at this point in the history
  • Loading branch information
simllll committed Aug 30, 2020
1 parent 7d2e5b8 commit ff24982
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
4 changes: 2 additions & 2 deletions storages/lru-redis/src/LRUWithRedisStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ export class LRUWithRedisStorage implements AsynchronousCacheType {
return localCache || undefined;
}

public async setItem(key: string, content: any): Promise<void> {
public async setItem(key: string, content: any, options?: { ttl?: number }): Promise<void> {
this.myCache.set(key, content);
if (this.options?.maxAge) {
await this.redis().setex(key, this.options.maxAge, JSON.stringify(content));
await this.redis().setex(key, options?.ttl || this.options.maxAge, JSON.stringify(content));
} else {
await this.redis().set(key, JSON.stringify(content));
}
Expand Down
2 changes: 1 addition & 1 deletion storages/redisio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@
"@types/ioredis": "^4.17.2",
"ioredis-mock": "^4.21.2"
},
"gitHead": "179ca6def52cbf51e09fc22c09f7313d6356105b"
"gitHead": "7d2e5b85914c7a6e54edad434b0fcbae37c484b4"
}
8 changes: 6 additions & 2 deletions storages/redisio/src/redisio.storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ export class RedisIOStorage implements AsynchronousCacheType {
return finalItem || undefined;
}

public async setItem(key: string, content: any): Promise<void> {
public async setItem(key: string, content: any, options?: { ttl?: number }): Promise<void> {
if (typeof content === "object") {
content = JSON.stringify(content);
} else if (content === undefined) {
await this.redis().del(key);
return;
}
await this.redis().set(key, content);
if (options?.ttl) {
await this.redis().setex(key, options.ttl, content);
} else {
await this.redis().set(key, content);
}
}

public async clear(): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion storages/redisio/test/redis.storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const MockedRedis = new RedisMock({
password: "pass"
});

const storage = new RedisIOStorage({}, MockedRedis);
const storage = new RedisIOStorage(() => MockedRedis);

describe("RedisIOStorage", () => {
it("Should clear Redis without errors", async () => {
Expand Down

0 comments on commit ff24982

Please sign in to comment.