Giter VIP home page Giter VIP logo

payload-redis-cache's Introduction

Payload Redis Cache Plugin

This plugin for Payload CMS adds a cache layer to API endpoints. The cache is based on the combination of the user's collection and the original URL.

Requirements

  • Payload version 1.0.19 or higher is required
  • A Redis instance is required

Installation

To install the plugin, run one of the following commands:

yarn add @aengz/payload-redis-cache

or

npm install @aengz/payload-redis-cache

Redis package

You also need to install the redis package if it is not already installed:

yarn add redis

or

npm install redis

Usage

To use the plugin, add it to the Payload config as follows:

import { buildConfig } from 'payload/config';
import { cachePlugin } from '@aengz/payload-redis-cache'

const config = buildConfig({
  // your config here
  
  plugins: [
    cachePlugin({ 
      excludedCollections: ['users'],
      //  excludedGlobals: ['myglobal']
    })
  ]
})

Add the initializer function in server.ts

import { cachePlugin } from '@aengz/payload-redis-cache'

...

initRedis({
  redisUrl: process.env.REDIS_URI
})

Plugin options

Option Type Description
redisUrl * string Redis instance's url.
redisNamespace string Choose the prefix to use for cache redis keys. Defaults to payload.
redisIndexesName string Choose the index key for cache redis indexes. Defaults to payload-cache-index.
excludedCollections string[] An array of collection names to be excluded.
excludedGlobals string[] An array of globals names to be excluded.
includedPaths string[] An array of custom routes to be included.

A * denotes that the property is required.

Helpers

This package provides utility functions for managing the cache. Here's an example of how to use the invalidateCache function:

import { invalidateCache }  from '@aengz/payload-redis-cache'

...

invalidateCache()

Development

There is a development environment in the /dev directory of the repository. To use it, create a new .env file in the /dev directory using the example .env.example file as a reference:

cd dev
cp .env.example .env

Before using the plugin in the development environment, the package needs to be built. To build the library, run one of the following commands:

Build the lib

Build the lib using:

yarn dev:build

or

npm run dev:build

Use development environment

To run the development environment, use the following command:

yarn dev

or

npm run dev

Running test

To run the test suite, use one of the following commands:

yarn test

or

npm run test

License

MIT

payload-redis-cache's People

Contributors

danieledeluis avatar gusfune avatar sangallimarco avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.