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

nuxt-perfect-cache

v1.1.4

Published

nuxt page cache and nuxt api cache using redis

Downloads

2,413

Readme

usage

npm i nuxt-perfect-cache

first of all config redis to your machine if you are using ubuntu there is a good video here is the link: https://www.youtube.com/watch?v=gNPgaBugCWk

// nuxt.config.js

modules: [
    [
        'nuxt-perfect-cache',
        {
          disable: false, // or in runtime config from .env file
          appendHost: true,
          ignoreConnectionErrors:false, //it's better to be true in production
          prefix: 'r-',
          url: 'redis://127.0.0.1:6379',
          getCacheData(route, context) {          
            if (route !== '/') {
              return false
            }
            return { key: 'my-home-page', expire: 60 * 60 }//1hour
          }
        }
      ]
]

publicRuntimeConfig: {
    nuxtPerfectCache: {
        disable: process.env.NUXT_PERFECT_CACHE_DISABLED
    }
}

options

| Property | Type | Required? | Description |:---|:---|:---|:---| | disable | boolean | no | default is true you can disable all module features | appendHost | boolean | no | default is true append host to the key | ignoreConnectionErrors | boolean | no | default is false ignore connection errors and render data as normal | prefix | string | no | default is r- it's redis prefix key | url | string | no | default is redis://127.0.0.1:6379 url for redis connection | getCacheData | function | yes | should return an object include key and expire if return false page will not cache

note

ignoreConnectionErrors added in 1.0.4 version

caveat

important security warning : don't load secret keys such as user credential on the server for cached pages. this is because they will cache for all users!

side note

if during test process in your local machine your page start rerender over and over it is not a big deal that is because package changed nuxt render function to solve that open a route not include cache in your browser until build process done

api request caching

asyncData(ctx) {
    return ctx.$cacheFetch({ key: 'myApiKey', expire: 60 * 2 }, () => {
      console.log('my callback called*******')
      return ctx.$axios.$get('https://jsonplaceholder.typicode.com/todos/1')
    })
  }

ok let me explain what is happening: nuxt-perfect-cache inject a plugin cacheFetch this is a function with two parameters the first one get an object include key and expire for redis second parameter is a callback function should return your normal request as a promise cacheFetch method will check if the process is in the server then check key in redis if key exist return data from redis if not call your callback as normal this method useful for consistent requests such as menu

caveat

then callback function should return a valid json for JSON.stringify method

features

  • easy to use
  • cache whole page in the redis
  • separate expire time for each page
  • api request cache

more control

to save a page to another redis server just return {key:string,expire:number,url:"my new url"} inside getCacheData method

also, it is possible for catcheFetch method here is full object you can pass { key,expire,disable,url,prefix,ignoreConnectionErrors}

for write and read directly you can use two injected methods:

const data = await $cacheRead({ key:'youtKey' })

data is null if key is not exist

const flag = await $cacheWrite({ key, expire: 60*60*24 }, 'yout content')

flag is false if can not write

note that you have to make sure process is in the server for cacheRead and cacheWrite methods

security warning

just note that you should use private redis server, if you are using a public redis server with a password be aware your password will bundle in client and leaks