Skip to content

tclbe/async-json-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

async-json-cache

A simple cache.

Usage

It can function as a normal cache.

import JSONCache from "async-json-cache"

// Initialisation
// The generic argument is how the cached data is stored in the cache.
const cache = new JSONCache<number>()

// Loading from JSON file
cache.loadFromFile("./numbers.json")

// Keys are always strings
cache.get("1")

It can be used to cache and retrieve results from an API.

import JSONCache from "async-json-cache"
import axios from "axios"

interface Result {
  views: number
  time: Date
}
const cache = new JSONCache<Result>()

// Manual updating... boo!
const { data } = axios.get("some api endpoint" + "key")
cache.set("key", data)

// Using JSONCache
function updater(key: string): Promise<Result> {
  const { data } = axios.get("some api endpoint" + "key")
  return data
}
const cache2 = new JSONCache<Result>(updater)
await cache.update("key")
cache.get("key")

Examples

A simple example can be found in ./examples. async.ts shows how the async update functionality could be used.

About

File-based cached for JSON objects, with user-defined asynchronous update functionality.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published