-
-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from ghoshRitesh12/cache
Add caching layer for improved performance
- Loading branch information
Showing
7 changed files
with
204 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
ANIWATCH_API_PORT=4000 | ||
|
||
# env to control allowed origins | ||
ANIWATCH_API_CORS_ALLOWED_ORIGINS=https://your-production-domain.com,https://another-trusted-domain.com | ||
ANIWATCH_API_CORS_ALLOWED_ORIGINS=<https://your-production-domain.com,https://another-trusted-domain.com> | ||
|
||
# RATE LIMIT | ||
# duration to track requests (in milliseconds) for rate limiting. here, 30*60*1000 = 1800000 = 30 minutes | ||
|
@@ -12,12 +12,14 @@ ANIWATCH_API_MAX_REQS=70 | |
|
||
# CAUTION: | ||
# For personal deployments, if you wanna have rate limitting | ||
# in your application, then set this env to your deployed | ||
# instance's hostname, otherwise don't set or have this env at all. | ||
# If you set this env to an incorrect value, you may face other issues. | ||
|
||
# in your application, then set the env below to your deployed | ||
# instance's hostname, otherwise don't set or have it at all. | ||
# If you set the env below to an incorrect value, you may face other issues. | ||
# ANIWATCH_API_HOSTNAME="api-aniwatch.onrender.com" | ||
|
||
|
||
# NOTE: this env is "required" for vercel deployments | ||
# ANIWATCH_VERCEL_DEPLOYMENT=<true or any non zero value> | ||
# ANIWATCH_API_VERCEL_DEPLOYMENT=<true or any non zero value> | ||
|
||
# env to use optional redis caching functionality | ||
ANIWATCH_API_REDIS_CONN_URL=<rediss://default:[email protected]:6379> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { config } from "dotenv"; | ||
import { Redis } from "ioredis"; | ||
|
||
config(); | ||
|
||
export class AniwatchAPICache { | ||
private _client: Redis | null; | ||
public isOptional: boolean = true; | ||
|
||
static DEFAULT_CACHE_EXPIRY_SECONDS = 60 as const; | ||
static CACHE_EXPIRY_HEADER_NAME = "X-ANIWATCH-CACHE-EXPIRY" as const; | ||
|
||
constructor() { | ||
const redisConnURL = process.env?.ANIWATCH_API_REDIS_CONN_URL; | ||
this.isOptional = !Boolean(redisConnURL); | ||
this._client = this.isOptional ? null : new Redis(String(redisConnURL)); | ||
} | ||
|
||
set(key: string | Buffer, value: string | Buffer | number) { | ||
if (this.isOptional) return; | ||
return this._client?.set(key, value); | ||
} | ||
|
||
get(key: string | Buffer) { | ||
if (this.isOptional) return; | ||
return this._client?.get(key); | ||
} | ||
|
||
/** | ||
* @param expirySeconds set to 60 by default | ||
*/ | ||
async getOrSet<T>( | ||
key: string | Buffer, | ||
setCB: () => Promise<T>, | ||
expirySeconds: number = AniwatchAPICache.DEFAULT_CACHE_EXPIRY_SECONDS | ||
) { | ||
const cachedData = this.isOptional | ||
? null | ||
: (await this._client?.get(key)) || null; | ||
let data = JSON.parse(String(cachedData)) as T; | ||
|
||
if (!data) { | ||
data = await setCB(); | ||
await this._client?.set(key, JSON.stringify(data), "EX", expirySeconds); | ||
} | ||
return data; | ||
} | ||
} | ||
|
||
export const cache = new AniwatchAPICache(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
type CacheVariables = { | ||
CACHE_CONFIG: { | ||
key: string; | ||
duration: number; | ||
}; | ||
}; | ||
|
||
export type AniwatchAPIVariables = {} & CacheVariables; |
Oops, something went wrong.