Improved Caching with Redis

In this lesson, we'll install and configure the AdonisJS Redis package. We'll then swap out our singleton in-memory cache service with a Redis cache implementation.

Published
Feb 04, 24
Duration
10m 44s

Developer, dog lover, and burrito eater. Currently teaching AdonisJS, a fully featured NodeJS framework, and running Adocasts where I post new lessons weekly. Professionally, I work with JavaScript, .Net C#, and SQL Server.

Adocasts

Burlington, KY

Join The Discussion! (4 Comments)

Please sign in or sign up for free to join in on the dicussion.

  1. Commented 10 months ago

    Incase you have issues with writing to redis, head over to redis-cli on the terminal and run this

    `config set stop-writes-on-bgsave-error no`

    Restart your redis server and all will be good.

    1

    Please sign in or sign up for free to reply

    1. Commented 10 months ago

      Thanks for sharing, Phillip!!

      1

      Please sign in or sign up for free to reply

  2. Commented 6 days ago

    Hi Tom, why do you define an overlay on redis with the CacheService class? I don't understand why you don't use the methods provided by redis directly. Is it an abstraction layer that preserves the name of the methods independently of the service used? A bit like what you explained with the aliases for routes?

    1

    Please sign in or sign up for free to reply

    1. Commented 6 days ago

      Hi n2zb! We started our CacheService without using Redis, but rather an in-memory key/value store. We kept using the CacheService even with Redis for a couple reasons.

      1. I felt it would be easier to understand and follow by refactoring our preexisting service to use Redis rather than directly replacing the service with Redis. That way the before/after is all in one file.

      2. To keep the JSON parse & stringify calls abstracted to the service's get & set methods.

      3. When we have things like this that may be swapped out at some point, I like to keep them abstracted in my code to simplify any updates down the road. For example, if this lesson were released today we would've used Bentocache rather than the AdonisJS Redis package directly as Bentocache is specially attuned for caching with Redis - and will be used for the official AdonisJS Cache package coming soon.

      So yes, you're pretty spot on! 😊

      1

      Please sign in or sign up for free to reply

Playing Next Lesson In
seconds