NestJS module that implements the server-side Stale-While-Revalidate caching pattern using Redis.
npm install nestjs-swr-cache
import { SwrCacheModule } from 'nestjs-swr-cache';
@Module({
imports: [
SwrCacheModule.forRoot({
host: 'YOUR_REDIS_HOST',
port: YOUR_REDIS_PORT,
// ... other Redis options
}),
],
})
export class YourModule {}
Return type of 'getStaleAndRevalidate' function follows fetch function
import { SwrCacheService } from 'YOUR_NPM_PACKAGE_NAME';
@Injectable()
export class YourService {
constructor(private swrCacheService: SwrCacheService) {}
async someFunction() {
const data = await this.swrCacheService.getStaleAndRevalidate(
'your_cache_key',
60000, // stale time
() => this.fetch // This should be a factory function that returns a promise with the data you want to cache
);
return data;
}
private async fetch(): Promise<YourDataType> {
// Fetch your data from an API, database, etc.
}
}
Ensure you handle potential errors, especially when working with Redis and network operations. Configuration You can pass any Redis option to the forRoot method to configure the Redis client.
Copyright (c) 2023 @devyeon
[MIT]