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. I teach AdonisJS, a full-featured Node.js framework, at Adocasts where I publish weekly lessons. Professionally, I work with JavaScript, .NET, and SQL Server.

Adocasts

Burlington, KY

Get the Code

Download or explore the source code for this lesson on GitHub

Repository

Join The Discussion! (8 Comments)

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

  1. Commented 1 year 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 1 year ago

      Thanks for sharing, Phillip!!

      1

      Please sign in or sign up for free to reply

  2. Commented 6 months 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 months 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

  3. Commented 3 days ago

    I believe this redis module needs an active Redis server deployed locally? Developing on Windows, it looks like Windows Subsystem for Linux would be required to get that working. Perhaps that should be mentioned in the video?

    1

    Please sign in or sign up for free to reply

    1. Commented 3 days ago

      Hi holodevelop! First, thank you for your feedback! I'm always looking to improve my lesson making skills, and feedback like this definitely helps, I appreciate it!

      To answer your question, any Redis connection would do. You can even use a small free tiered one from Redis Cloud. However, we only use Redis in this lesson and the next within this series, so just skipping over is also a valid option.

      There's many things about this series I would add, remove, or do differently now if I were to go back and do them again. A heads up blurb like the below in this lesson and our before we begin lesson would definitely be one of those things.

      As a heads up, we'll only be utilizing Redis in this lesson and the next as a way to demonstrate installing, configuring, and using the package. Again, as we covered in lesson 1.1, you'll need a Redis connection to work with. If you're following along and don't see yourself using Redis for anything beyond this series, feel free to skip this and the next lesson and pick back up in lesson 2.16.

      👆 I'll circle back and add that to this lesson and another blurb to lesson 1.1, I have some updates to do in module 11 as well.

      0

      Please sign in or sign up for free to reply

      1. Commented 2 days ago

        Ah thanks for the response, I think I may have just skimmed lesson 1.1, which was quite a number of lessons ago. Someone who did everything in order would likely have no issue, but for people who skip around (like me), that new blurb you mentioned would indeed be quite useful!

        1

        Please sign in or sign up for free to reply

        1. Commented 1 day ago

          Yeah, I completely agree!! Thanks again for the helpful feedback!!

          0

          Please sign in or sign up for free to reply

Playing Next Lesson In
seconds