SECRET OF CSS

Caching in Deno using Zoic Cache. By Joe Burrow, Celena Chan, Aaron… | by Celena Chan | Jul, 2022


By Joe Burrow, Celena Chan, Aaron Dreyfuss, Hank Jackson and Jasper Narvil

1*cY2 2yJ Pv2Pa2cDySmRSQ

Caching is an important technology and a necessity for many scaled-out applications, or applications looking to scale out. Developers looking for a caching solution for the Deno run-time, which integrates with Oak middleware, should take a look at Zoic Cache. In this article we’ll walk through the basics of Zoic and outline the few quick and easy steps to take advantage of the Zoic caching library in our Deno / Oak app.

Zoic Cache is a fast, light-weight, middleware library for Oak in the Deno runtime that allows users to quickly set up a RESTful server-side API web cache with a configurable built-in LRU eviction policy. The Zoic Cache library allows users to abstract away much of the boilerplate present in a homegrown caching solution and permits them to quickly spin up a cache using the popular cache-aside design pattern. Alternatively, for those users looking for an instance of a Redis cache, Zoic Cache makes that available through its configuration settings. Let’s walk through the setup of an instance of Zoic Cache in a typical configuration.

As we mentioned earlier, Zoic’s caching library aligns with a middleware design pattern. As a result, Zoic is exclusively for caching back-end, server-side requests. The best part is, it’s super easy to import Zoic and get started!

Download it here!

Deno’s built-in module import feature is intuitive and lightweight.

Simple type the following at the top of your server script (ex. your server.js or server.ts file)

0*RZ0tDgLGWrUVVW5D

If you’re new to the Deno runtime, please note that the first time you use Zoic, Deno will have to install and cache it on your system so it can easily be called up again for future use. This is true for all Deno modules. Also note that if Deno’s Oak library isn’t installed on your machine yet, it will be installed as a dependency when you use the Zoic library.

After you’ve imported the library, you’re ready to instantiate the cache. Luckily, you have quite a few optional customizations:

  • Cache Type (default is ‘LRU’) — This can be either LRU (least recently used eviction policy) or Redis (be sure to keep an eye out for new releases in the near future — LFU support is on the horizon!).
    – Choosing a Redis cache type will allow you to integrate your Redis instance to the cache, allowing you to utilize all the methods and other goodies associated with your Redis deployment. (*Note — if you choose a Redis cache type, be sure to include your port number as the final parameter)
    – Choosing an LRU cache type on the other hand, while less featured, is the more performant as it has constant time complexity.
  • Time to expiration (default is Infinity) — You might be able to guess what this one means. Time to expiration is how long you would like your data to exist in your cache before it’s deleted. A smaller time to expiration value might accommodate an application with very dynamic, fast-changing responses as you would not want to feed stale or outdated data to your frontend. You can choose to input this value as a number in milliseconds as a comma separated string indicating the ‘h’ (hours), ‘m’ (minutes), and ‘s’ (seconds) — as illustrated in the example below.
  • Capacity (default is Infinity) — Want to limit the size of your cache? The capacity property lets you dictate how many key/value pairs (i.e. the number of responses) your cache will hold.
  • Respond on hit (default is true) — Need a speed boost? This boolean value tells our library whether or not you’d like to increase performance by automatically sending responses to the frontend, without the chance to edit those responses with additional middleware you might have after the cache check.

Here’s an example of what a cache initialization might look like:

0*2QnFS3oWt3XNKySF
Initializing your Zoic Cache

And embedding the caching methods from Zoic in your Oak middleware chain is as simple as…

How to embed Zoic in your Oak middleware chain

For a GUI-based perspective on what is happening in your Zoic caching middleware instance, utilize the free and open source Zoic Dev Tools in the Chrome Web Store.

0*CSJqCi71 N1aisjZ

We wanted everyone who uses Zoic to be able to see the improvement to their applications, so we made a simple, easy to use Google Chrome developer tool that lets you track all of key metrics like number of entries, read / writes processed and latency to let you see how much more efficiently your application is working.

How to install and configure the Zoic Cache dev tool

To use our Chrome dev tool, simply download it here from the Chrome Web Store. Once you have it installed (you may have to restart Chrome), open up Zoic in your developer tools. First, insert your server address and endpoint at which you will serve the cache metrics from via the input field on the dev tool panel. Second, in your server routes, create a new route matching the endpoint specified in the dev tool. In this route add the middleware Zoic.getMetrics (assuming you’ve already imported the Zoic middleware library and instantiated a new instance of the cache).

Note: This route will have CORS enabled.

1*lOS4SOLrZH6Y4hIcgMe8Jw
Example configuration in your server routes

The proven performance, usability and versatility of Zoic makes it the perfect choice for all your Deno caching needs. Download it here!

And if there are any questions regarding Zoic, feel free to reach out to the Zoic team. We welcome any feedback!

Zoic Github | WebsiteJoe Borrow: Linkedin | GithubCelena Chan: Linkedin | GithubAaron Dreyfuss: Linkedin | GithubHank Jackson: Linkedin | GithubJasper Narvil: Linkedin | Github



News Credit

%d bloggers like this: