All Projects → DominicTobias → Keshi

DominicTobias / Keshi

A better in-memory cache for Node and the browser

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Keshi

Hitchcock
The Master of Suspense 🍿
Stars: ✭ 167 (+122.67%)
Mutual labels:  async, cache
incache
Powerful key/value in-memory storage or on disk to persist data
Stars: ✭ 16 (-78.67%)
Mutual labels:  cache, in-memory
Cphalcon7
Dao7 - Web framework for PHP7.x,项目接洽 QQ 176013762
Stars: ✭ 237 (+216%)
Mutual labels:  async, cache
Governor
A rate-limiting library for Rust (formerly ratelimit_meter)
Stars: ✭ 99 (+32%)
Mutual labels:  async, in-memory
Reservoir
Android library to easily serialize and cache your objects to disk using key/value pairs.
Stars: ✭ 674 (+798.67%)
Mutual labels:  async, cache
Datastore
🐹 Bloat free and flexible interface for data store and database access.
Stars: ✭ 99 (+32%)
Mutual labels:  async, store
keshi
A better in-memory cache for Node and the browser
Stars: ✭ 76 (+1.33%)
Mutual labels:  store, in-memory
Data Store
Easily get, set and persist config data. Fast. Supports dot-notation in keys. No dependencies.
Stars: ✭ 120 (+60%)
Mutual labels:  cache, store
Redux Ecosystem Links
A categorized list of Redux-related addons, libraries, and utilities
Stars: ✭ 5,076 (+6668%)
Mutual labels:  async, store
React Query
⚛️ Hooks for fetching, caching and updating asynchronous data in React
Stars: ✭ 24,427 (+32469.33%)
Mutual labels:  async, cache
Libcache
A Lightweight in-memory key:value cache library for Go.
Stars: ✭ 152 (+102.67%)
Mutual labels:  cache, in-memory
Go Cache
Go in-memory cache library
Stars: ✭ 15 (-80%)
Mutual labels:  cache, in-memory
Olric
Distributed cache and in-memory key/value data store. It can be used both as an embedded Go library and as a language-independent service.
Stars: ✭ 2,067 (+2656%)
Mutual labels:  cache, in-memory
Redis
Async Redis Client for PHP based on Amp.
Stars: ✭ 107 (+42.67%)
Mutual labels:  async, cache
Cash
HTTP response caching for Koa. Supports Redis, in-memory store, and more!
Stars: ✭ 122 (+62.67%)
Mutual labels:  cache, store
Task Easy
A simple, customizable, and lightweight priority queue for promises.
Stars: ✭ 244 (+225.33%)
Mutual labels:  async, in-memory
Bojack
🐴 The unreliable key-value store
Stars: ✭ 101 (+34.67%)
Mutual labels:  cache, store
Gcache
An in-memory cache library for golang. It supports multiple eviction policies: LRU, LFU, ARC
Stars: ✭ 1,787 (+2282.67%)
Mutual labels:  cache, in-memory
Apex Recipes
A library of concise, meaningful examples of Apex code for common use cases following best practices.
Stars: ✭ 307 (+309.33%)
Mutual labels:  async, cache
Cache Chunk Store
In-memory LRU (least-recently-used) cache for abstract-chunk-store compliant stores
Stars: ✭ 24 (-68%)
Mutual labels:  cache, in-memory

Keshi

Keshi on NPM Keshi on TravisCI

Keshi is a better in-memory (or custom) cache for Node and the browser.

const createCache = require('keshi');

or

import createCache from 'keshi';

Usage

const cache = createCache();

const user = await cache.resolve(
  'user',
  () => fetch('https://myapi.com/user').then((r) => r.json()),
  '30 mins'
);

What this will do:

  • Fetch the user from the API as it doesn't have it in cache.
  • If called again within 30 minutes it will return the cached user.
  • If called after 30 minutes it will fetch the user again and re-cache.

Cache the data you need

You should return only the data you need to keep the cache efficient. Here's a real world example of caching repository information from GitHub:

// In the browser
const fetchProjectMeta = (user, repo) =>
  fetch(`https://api.github.com/repos/${user}/${repo}`)
    .then((r) => r.json())
    .then((r) => ({ name: r.full_name, description: r.description }));

// ...or in Node
const fetchProjectMeta = (user, repo) =>
  got
    .get(`https://api.github.com/repos/${user}/${repo}`, { json: true })
    .then((r) => ({ name: r.body.full_name, description: r.body.description }));

// And call it (for 1 hour it will return cached results).
const meta = await cache.resolve('myRepo', fetchProjectMeta('DominicTobias', 'keshi'), '1 hour');

Rate limited APIs (as above), saving bandwidth, dealing with poor client network speeds, returning server responses faster are some of the reasons you might consider caching requests.

Keshi will automatically keep memory low by cleaning up expired items.

API

resolve(key, [value], [expiresIn])

key → String → Required

value → Any → Optional

A function which resolves to a value, or simply a literal value.

expiresIn → Number | String → Optional

A number in milliseconds or anything that ms accepts after which the value is considered expired. If no expiry is provided the item will never expire.

del(key, matchStart)

Delete a cached item by key.

You can also delete any that start with the key by passing true to matchStart.

cache.del(`project.${projectId}.`, true)

clear()

Clear all cached items.

Custom storage

The default cache is in-memory, however the storage can be anything you like. To pass in a custom storage:

const cache = createCache({ customStorage });

Your cache must implement the following methods:

customStorage.get(key)

Returns the cache value given the key. Cache values must be returned as an Array of [value, <expiresIn>]. expiresIn is an ISO Date string.

This method can be async.

customStorage.set(key, value)

Values set are of type Array in the following format: [value, <expiresIn>]. expiresIn should be an ISO Date string.

This method can be async.

customStorage.del(key)

Removes the item specified by key from the cache.

This method can be async.

customStorage.keys()

Returns an array of cache keys.

This method can be async.

customStorage.clear()

Clears all items from the cache.

The clear method of the public interface will return the results of this call. You could for example return a Promise that your app can wait on before performing subsequent actions.

Example

import { get, set, keys, del, clear } from 'idb-keyval';

const customStorage = { get, set, keys, del, clear };
const cache = createCache({ customStorage });
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].