Memory, disk, and S3 cache implementations with simple TTL
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
dist
src
.gitignore
LICENSE
README.md
package-lock.json
package.json
tsconfig.json

README.md

simple-cache

Memory, disk, and S3 cache implementations with simple TTL.

For use with multilayer-async-cache-builder

Install

npm i ssh://github.com/ken107/simple-cache

Example

Create a 3-layer cache:

Layer Type Info
1 Memory ttl: 60 seconds, cleanupInterval: 60 seconds
2 Disk ttl: 1 hour, cleanupInterval: 15 minutes
3 S3 ttl: (use bucket lifecycle rules)
import { cached } from "multilayer-async-cache-builder"
import { MemCache, DiskCache, S3Cache } from "simple-cache"

const s3: AWS.S3;
const fetchItem: (id: string) => Promise<{data: Buffer, metadata: any}>;

const getItem = cached(fetchItem, [
  new MemCache(ms("1 minute"), ms("1 minute")),
  new DiskCache("path/to/cache/folder", ms("1 hour"), ms("15 minutes")),
  new S3Cache(s3, "my-bucket")
])

//use
getItem("item-id").then(useItem);

Note: In this simple implementation, an item is considered valid as long as it exists in the cache, even after its TTL has expired (and is waiting to be purged). TTL is calculated from the item's last access time.