https://github.com/m4p4/nextjs-redis-cache
Redis Cache for NextJs Framework to speedup your sites even more.
https://github.com/m4p4/nextjs-redis-cache
cache inmemory-cache next-cache next-redis-cache nextjs nextjs-cache redis redis-cache
Last synced: 3 months ago
JSON representation
Redis Cache for NextJs Framework to speedup your sites even more.
- Host: GitHub
- URL: https://github.com/m4p4/nextjs-redis-cache
- Owner: M4p4
- Created: 2022-09-13T17:58:45.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-09-15T10:16:30.000Z (over 2 years ago)
- Last Synced: 2024-10-16T10:01:16.817Z (8 months ago)
- Topics: cache, inmemory-cache, next-cache, next-redis-cache, nextjs, nextjs-cache, redis, redis-cache
- Language: TypeScript
- Homepage:
- Size: 6.84 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Next.js Redis Cache
A **Redis Cache** for **Next.js Framework** that supports expiration after a given time and also compress the data in memory. Perfect to speed up server side data collection even on a large scale.
### Installation
```
npm install nextjs-redis-cache
```### How to use
```ts
import cache from 'nextjs-redis-cache';// your redis client from redis package (createClient)
// this should come from a singleton in your Next.js app!
const redisClient = createClient();// set
// last param (options that also contain expireIn) is optional default is that it never expires!
const data = { foo: 'bar' };
await cache.set(redisClient, 'examplekey', data, { expireIn: 24 * 60 * 60 });// exists?
// returns true or false
const res = await cache.exists(redisClient, 'examplekey');// get
// returns decompressed and JSON.parsed object!
// if there is non data for the given key it return null
const loadedData = await cache.get(redisClient, 'examplekey');
if (loadedData) {
//... you can also use exists function to be sure cache exists
}// delete
// delete array of keys
await cache.delete(redisClient, ['examplekey', 'otherkey']);
```**Note:** Your object will be stringified to a json string and then compressed with zlib. When you get the data from cache you will get your object back!