A caching middeware library for Deno HTTP framework, Oak.

Overview

Zoic logo

example workflow



Caching middleware library for Oak



Table of Contents

  1. Description
  2. Getting Started
  3. Middleware and caching
  4. Authors
  5. License

Description

Zoic is an easy-to-use middleware library for caching responses from RESTful API endpoints in Oak, built for the Deno JavaScript runtime. Zoic provides an LRU in-memory cache, as well as support for Redis caches. Developers can use Zoic to easily cache HTTP responses with one simple middleware function that automatically handles both caching response data in the event of a cache miss, and sending responses on cache hits.

Zoic Developer Tool

The Zoic Developer Tool allows developers to monitor cache metrics in real time. Checkout the Zoic Developer Tool README for installation and configuration details.

Getting Started

As Zoic is a middleware library for Oak in the Deno runtime environment, it is paramount that Deno is first installed and configured.

Quick Start

In your application, import the Zoic module from the deno.land module.

import { Zoic } from "https://deno.land/x/zoic/zoic.ts";

Create a cache

Initalize a new Zoic object, passing in your user defined options object. If no options object is passed, Zoic will set all properties to their default values.

  • cache: Sets cache eviction policy. Default value: 'LRU'
  • expire: Sets cache invalidation/expiration time for each entry. This can be set in human readable time, as a comma seperated string, denoting hours with value followed by 'h', minutes followed by 'm', and seconds followed by 's'. Alternatively, you may pass in the time as a number representing seconds. Default value: 'Infinity'
  • capacity: Sets the maximum number of entries. Default value: Infinity
  • respondOnHit: Determines if cache hits should be sent as an HTTP response immediately upon retrival. If this is set to false, the cached response data will be attached to Oak Context.state property, context.state.zoicResponse. It is recommended to leave this set to true, unless additonal processing on the response data is desired in the event of a cache hit. Default value: true

Example:

const cache = new Zoic({
  cache: 'LRU',
  expire: '5m, 3s',
  capacity: 50,
});

Redis cache

To use an instance of Redis as your cache, initalize a new Zoic object, passing in 'redis' as the cache property on your options object. You also must specify the port your instance of Redis is running on, via the port property. Optionally, you may pass the hostname as well. This value defaults to '127.0.0.1'.

NOTE: Options expire and capacity do not have an effect on Zoic if using Redis, as these would be defined in your Redis configuration.

Example:

const cache = new Zoic({
  cache: 'redis',
  port: 6379
})

Middleware and caching

- Zoic.use()

Zoic.use() is responsible for both sending cached responses, and storing responses in the cache. When .use() is called in a middleware chain, it will check if data exists in the cache at a key representing that route's endpoint. If the query is successful, it will send an HTTP response with the cached body, headers, and status. If the query is unsucessful, .use() will automatically listen for when the subsequent middleware in that route has been executed, and will cache the contents of the HTTP response before being sent to the client. This way, the developer only needs to place .use() in their middleware chain at the point where they would like the response to be sent in the event of a cache hit, making it extremely easy to use.

NOTE: if the user has selected false for respondOnHit when intializing Zoic, the reponse data will be stored on ctx.state.zoicResponse instead of being sent as an HTTP response.

Example:

const cache = new Zoic();

router.get('/userInfo/:name', cache.use, controller.dbRead, ctx => {
    ctx.response.headers.set('Content-Type', 'application/json');
    ctx.response.body = ctx.state.somethingFromYourDB;
});

- Zoic.put()

Zoic.put() will add responses to the cache without first querying to see if an entry already exists. The primary use being to replace data at an already existing keys, or manually add responses without anything being returned. Like with .use(), .put() will automatically store the response body, headers, and status at the end of a middleware chain before the response is sent.

Example:

const cache = new Zoic();

router.put('/userInfo/:name', cache.put, controller.dbWrite, ctx => {
    ctx.response.body = ctx.state.someDataYouChanged;
});

- Zoic.clear()

Zoic.clear() clears the contents of the cache.

Example:

const cache = new Zoic();

// On it's own..
router.post('/userInfo/:name', cache.clear);

// In conjunction with another function...
router.post('/otherUserInfo/', cache.clear, controller.dbWrite, ctx => {
    ctx.response.body = ctx.state.someFreshData;
});

Authors

License

This product is licensed under the MIT License - see the LICENSE file for details.

This is an open source product.

This product is accelerated by OS Labs.

You might also like...

A fast and optimized middleware server with an absurdly small amount of code (300 lines) built on top of Deno's native HTTP APIs

A fast and optimized middleware server with an absurdly small amount of code (300 lines) built on top of Deno's native HTTP APIs with no dependencies. It also has a collection of useful middlewares: log file, serve static, CORS, session, rate limit, token, body parsers, redirect, proxy and handle upload. In "README" there are examples of all the resources. Faster's ideology is: all you need is an optimized middleware manager, all other functionality is middleware.

Dec 28, 2022

This is a simple boilerplate for a Deno website, deployed with Deno Deploy.

Simple Deno Website Boilerplate This is a simple website boilerplate built using Deno and deployed using Deno Deploy. Demo at simple-deno-website-boil

Dec 3, 2022

TypeSafe MongoDB Atlas Data API SDK for Deno & Deno Deploy

Atlas SDK atlas_sdk is a TypeSafe MongoDB Atlas Data API SDK for Deno & Deno Deploy Links Docs Import Replace LATEST_VERSION with current latest versi

Dec 26, 2022

Deno bindings for yoga, using Deno FFI.

deno_yoga Deno bindings for yoga, using Deno FFI. Usage flags: --allow-ffi: Requires ffi access to "yogacore.dll", "libyogacore.so", "libyogacore.dyli

Feb 11, 2022

deno-ja (Deno Japanese community) showcase

Showcase Deno本家よりも気軽に作ったものを公開できるようなShowcaseです。 スクリーンショットの撮影方法 短めのidを決めていただいて、下記のようにスクリプトを実行してください。 deno task screenshot [url] [id] ※エラーが出る場合は、下記を実行してみ

Oct 28, 2022

A command-line tool to manage Deno scripts installed via deno install

🏞️ nublar nublar is a command-line tool to manage your scripts installed via deno install. 🛳️ Installation deno install --allow-read --allow-write -

Dec 26, 2022

🪐 The IPFS gateway for NFT.Storage is not "another gateway", but a caching layer for NFTs that sits on top of existing IPFS public gateways.

nftstorage.link The IPFS gateway for nft.storage is not "another gateway", but a caching layer for NFT’s that sits on top of existing IPFS public gate

Dec 19, 2022

A fast, safe and easy caching mechanism similar to Redis written in typescript

Viper Viper is a memory based caching mechanism which is aimed towards ease of use and speed. It's in a very early stage right now and not meant to us

Jan 24, 2022

Next-level mongoose caching layer with event based cache clearing

SpeedGoose ## About The Project This project is a next-level mongoose caching library which is fully written in typescript. It's caching on two levels

Dec 15, 2022
Releases(v1.0.5)
Owner
OSLabs Beta
OSLabs Beta
A small, but powerful HTTP library for Deno & Deno Deploy, built for convenience and simplicity

Wren Wren is a small, but powerful HTTP library for Deno & Deno Deploy, built for convenience and simplicity. convenient aliases for HTTP responses au

Jakub Neander 69 Dec 12, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
🛣️ A tiny and fast http request router designed for use with deno and deno deploy

Rutt Rutt is a tiny http router designed for use with deno and deno deploy. It is written in about 200 lines of code and is pretty fast, using an exte

Denosaurs 26 Dec 10, 2022
Opinionated collection of TypeScript definitions and utilities for Deno and Deno Deploy. With complete types for Deno/NPM/TS config files, constructed from official JSON schemas.

Schemas Note: You can also import any type from the default module, ./mod.ts deno.json import { type DenoJson } from "https://deno.land/x/[email protected]

deno911 2 Oct 12, 2022
Allo Caching for Deno

Simple caching solution in Typescript.

Adam Josefus 5 Feb 14, 2022
Project implements decorators for oak like Nest.js.

oak decorators Project implements decorators like implementation of Nest.js for Deno's web framework oak. Usage The following are the core files that

akihiro tanaka 5 Dec 5, 2022
📡Usagi-http-interaction: A library for interacting with Http Interaction API

?? - A library for interacting with Http Interaction API (API for receiving interactions.)

Rabbit House Corp 3 Oct 24, 2022
A set of APIs for handling HTTP and HTTPS requests with Deno 🐿️ 🦕

oak commons A set of APIs that are common to HTTP/HTTPS servers. HTTP Methods (/method.ts) A set of APIs for dealing with HTTP methods. Content Negoti

oak 7 May 23, 2022