Code Monkey home page Code Monkey logo

simple-cache's Introduction

TypeScript First: Simple Cache Wrapper

Allow an async function' calls to be cached and have its cached invalidated in an unopinionated, declarative manner. The intention is to save repeated reads over network partitions such as sending GET requests on the client side or querying database reads from the server side.

Installation

npm install @typescript-first/simple-cache

Usage

Suppse we have a function expensiveUpperCase. Upper-casing isn't really expensive, we'll artifically make it so.

async function expensiveUpperCase(input: string) {
  await new Promise(resolve => setTimeout(resolve, 5000));
  return input.toUpperCase();
}

We can produce the cached version of the function like this:

import { withCache } from "@typescript-first/simple-cache";
const cachedExpensiveUpperCase = withCache(expensiveUpperCase, {
  paramHasher: (input: string) => string,
  paramState: (input: string) => null,
  maxEntries: 10
});

In general:

withCache(inputFunction, cacheSettings);

In the cacheSettings object:

  • You provide a paramHasher to produce a string that is used to look up a cache entry based on your function's input argument value. So this should be unique for each possible argument value.
  • You provide a paramState to associate a state to a cache entry. When the state changes on a subsequent call, the previous cached value is invalidated. In the example above, our computation is stateless so we just statically return a constant null -- the constant outcome means that the cache will be perpetually valid.
  • You provide how many cache entries to retain. Cache entries least recently used will be kicked out once the limit is exceeded.

The cached version of the function has exactly the same signature as the input function.

cacheExpensiveUpperCace("hello");

Current limitations

The input function must be async and must accept only one parameter.

simple-cache's People

Contributors

soul-codes avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.