npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@10life/strapi-plugin-advanced-cache-manager

v1.0.6

Published

A Cache Management for Strapi.

Downloads

362

Readme

strapi-plugin-advanced-cache-manager

An advanced cache management plugin for Strapi.

Strapi v4 GraphQL plugin does not come with a cache feature out of the box. A third-party plugin, apollo-server-plugin-response-cache, is used to enable caching, but it does not have a cache invalidation logic. There is another forked project that has a cache invalidation logic, but it only works when GraphQL mutations are executed. This is not suitable for users who create or update data inside Strapi or by direct SQL queries.

This plugin allows Strapi users to invalidate the cache according to the cache time defined in the plugin options. Besides, it also provides a way for users to clear AWS CDN cache.

How to use

Put this at the top of /config/plugins.js:

const { RedisCache } = require('apollo-server-cache-redis');
const apolloServerPluginResponseCache = require('apollo-server-plugin-response-cache').default;
const ApolloServerPluginCacheControl = require('apollo-server-core').ApolloServerPluginCacheControl;

To enable GraphQL caching, put the following code in your GraphQL config in /config/plugins.js:

  graphql: {
    enabled: true,
    config: {
        apolloServer: {
            // use redis for storing cache
            cache: (() => {
              if (env('REDIS_HOST')) {
                const redisCache = new RedisCache({
                    host: env('REDIS_HOST'),
                    password: env('REDIS_PASSWORD')
                });
                redisCache.cacheType = 'RedisCache';
                return redisCache;
              }
            })(),
            plugins: [
              // cache behavior lower age override higher age
              ApolloServerPluginCacheControl({ defaultMaxAge: env('STRAPI_GRAPHQL_DEFAULT_MAX_AGE') }),
              // customize your cache behavior according to your use case
              apolloServerPluginResponseCache({
                shouldReadFromCache: async(requestContext) => {
                  return true;
                },
                shouldWriteToCache: async(requestContext) => {
                  return true;
                },
                extraCacheKeyData: async(requestContext) => {
                  return true;
                },
                sessionId: async (requestContext) => {
                  return null;
                },
              }),                
            ]
        },
    },
  },

To enable this plugin, put the following code in /config/plugin.js:

  'advanced-cache-manager': {
    enabled: true,
    config: {
      max_age: env('STRAPI_GRAPHQL_MAX_AGE'),
      cache_control_matrix: [
        { query: 'usersPermissionsUser', maxAge: 0, scope: "PRIVATE" },
        { query: 'examplePosts', maxAge: env('STRAPI_GRAPHQL_MAX_AGE'), scope: "PUBLIC" },
      ],
      aws_config: {
        accessKeyId: env('STRAPI_AWS_ACCESS_KEY_ID'),
        secretAccessKey: env('STRAPI_AWS_ACCESS_SECRET'),
        region: env('STRAPI_AWS_REGION'),
      }
    }
  },

Prepare the environment variables as follows:

REDIS_HOST
REDIS_PASSWORD
STRAPI_GRAPHQL_DEFAULT_MAX_AGE
STRAPI_GRAPHQL_MAX_AGE
STRAPI_AWS_ACCESS_KEY_ID
STRAPI_AWS_ACCESS_SECRET
STRAPI_AWS_REGION

Cache administration

You can customize the cache scope and maxAge according to your needs. By default, entities will be cached by STRAPI_GRAPHQL_DEFAULT_MAX_AGE. The cache_control_matrix option can override the cache settings for specific entities by STRAPI_GRAPHQL_MAX_AGE. This plugin provides an admin page to clear the cache according to the cache maxAge. You can choose to clear the short cache (STRAPI_GRAPHQL_DEFAULT_MAX_AGE) or clear all the cache in Redis.

The admin page also provides the CDN cache clearing function. It will clear all the CDN cache.

Road map

  • A scheduled cache clearing feature
  • A cache clearing pattern input for CDN cache

Contributing

This section covers the way how to configure your environment if you want to contribute to this package.

Setting up the environment In order to start making changes in the plugin you first need to install Strapi infrastructure on top of the plugin repository.

npx create-strapi-app --quickstart strapi
cd strapi

By default Strapi does not create plugins folder so we need to create it.

mkdir -p src/plugins

Now we should clone this repository so we can work on it.

git clone https://github.com/10Life/strapi-plugin-advanced-cache-manager.git

Install dependencies:

npm install

Now we need to register plugin so strapi can use it. In order to do that we need to create (if not already created) ./config/plugins.js file and add entry to it. (Follow How to use section)

module.exports = ({ env }) => ({
  "advanced-cache-manager": {
    enabled: true,
    // add this line
    resolve: "./src/plugins/strapi-plugin-advanced-cache-manager
    // rest of the configuration
  },
});

Rebuild the project and start the server:

npm run build
npm run develop