Download src/cache/redisCache.ts from lynfera/api: direct link, hf CLI and curl.
- Browser
- Download file 1.86 kB
-
https://huggingface.co/spaces/lynfera/api/resolve/9711ca71d84debd7c5d12bfadebe2f693bc677fb/src/cache/redisCache.ts
- Command line
-
hf download hf://spaces/lynfera/api@9711ca71d84debd7c5d12bfadebe2f693bc677fb/src/cache/redisCache.ts
-
curl -L -o redisCache.ts https://huggingface.co/spaces/lynfera/api/resolve/9711ca71d84debd7c5d12bfadebe2f693bc677fb/src/cache/redisCache.ts
1.86 kB
| import { IRedisCache } from "@/interfaces/cache/IRedisCache.js"; | |
| import { Redis } from "ioredis"; | |
| class RedisService implements IRedisCache { | |
| private client: Redis; | |
| constructor(client: Redis) { | |
| this.client = client; | |
| } | |
| async get<T>(key: string): Promise<T | null> { | |
| const value = await this.client.get(key); | |
| return value ? JSON.parse(value) : null; | |
| } | |
| async set(key: string, value: unknown, ttlSeconds?: number): Promise<void> { | |
| const data = JSON.stringify(value); | |
| if (ttlSeconds) { | |
| await this.client.set(key, data, "EX", ttlSeconds); | |
| } else { | |
| await this.client.set(key, data, "EX", 1800); | |
| } | |
| } | |
| async del(key: string): Promise<void> { | |
| await this.client.del(key); | |
| } | |
| async exists(key: string): Promise<boolean> { | |
| return (await this.client.exists(key)) === 1; | |
| } | |
| async setAdd(key: string, value: string): Promise<number> { | |
| return await this.client.sadd(key, value); | |
| } | |
| async setRemove(key: string, value: string): Promise<number> { | |
| return await this.client.srem(key, value); | |
| } | |
| async getSetLength(key: string): Promise<number> { | |
| return await this.client.scard(key); | |
| } | |
| async incrementKey(key: string): Promise<number> { | |
| return this.client.incr(key); | |
| } | |
| async decrementKey(key: string): Promise<number> { | |
| return this.client.decr(key); | |
| } | |
| async setKeyExpiry(key: string, time: number): Promise<number> { | |
| return this.client.expire(key, time); | |
| } | |
| async publishInvalidation(type: string, slug: string): Promise<number> { | |
| return this.client.publish("cache:invalidate", JSON.stringify({ type, slug })); | |
| } | |
| async publishBuild(data: { deploymentId: string; projectId: string; envs: { name: string; value: string }[] }): Promise<number> { | |
| return this.client.publish("build:start", JSON.stringify(data)); | |
| } | |
| async disconnect(): Promise<void> { | |
| await this.client.quit(); | |
| } | |
| } | |
| export default RedisService; | |