npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cached-provider

v1.1.0

Published

Optimize data creation by synchronizing and caching

Downloads

191

Readme

cached-provider

Optimize data creation by synchronizing and caching

  • Check cached object
    • If valid cache exist, return cached value (hitA)
  • Synchronize execution
    • Check cached object
      • If valid cache exist, return cached value (hitS)
    • Call provider to update cached value
    • return cached value (miss)

How to install

npm install cached-provider

How to use

Lazy Cache

Call provider once, and keep the result until TTL.

const value = cached({
    ttl: 1000 * 60,// Cache TTL for get
    provider: async () => "slow data provider"
})
await value() // Access Value

Eager Cache

In addition to lazy caching, run cache update automatically to minimize get latency

const value = cached({
    ttl: 1000 * 60,// Cache TTL for get
    provider: async () => "slow data provider",
    autoUpdater: {
        interval: 1000 * 5, // Validate cache every 5 sec
        ttl: 1000 * 50, // Cache TTL for update , which should be smaller than TTL for get
    }
})
await value() // Access Value

Smart Eager Cache

Adjust cache update interval based on user access.

const value = cached({
    ttl: 1000 * 60 * 60,
    provider: async () => "slow data provider",
    autoUpdater: {
        interval: 1000 * 5, // Validate cache every 5 sec
        ttl: ({sinceAccessedAt}) => {
            // Cache TTL for update , which should be smaller than TTL for get
            // Use longer update cycle when the value is not accessed
            return (sinceAccessedAt !== undefined) && (sinceAccessedAt < 1000 * 60 * 10)
                ? 1000 * 50
                : 1000 * 60 * 59
        }
    }
})
await value() // Access Value

validator callback

In addition to the time based cache expire, you can add validator callback to validate cache object just before using it

How to shut down auto updater

If you specify onShouldContinue callback, cached provider ask you if it can continue running auto updater.

See example/example.ts for detail

export type CachedProviderOptions<T> = {
    autoUpdater?: {
        onShouldContinue?: () => boolean
    }
}

Monitoring Cache Behavior

onEvent callback allow you to monitor cache access in detail

See example/example.ts for detail

export type CachedProviderOptions<T> = {
    onEvent?: CacheEventCallback
}
export type MethodType = "get" | "update"
export type EventType = "hitA" | "hitS" | "miss"

export type CacheEvent = {
    methodType: MethodType
    eventType: EventType
    requestAt: Date
    responseAt: Date
    cachedAt: Date
}

export type CacheEventCallback = (event: CacheEvent) => void

MethodType

| type | description | |--------|--------------------------------| | get | Get method call (on demand) | | update | Update method call (scheduled) |

EventType

| type | description | |------|----------------------------------------------| | hitA | Cache hit before synchronization. Super fast | | hitS | Cache hit after synchronization. Fast | | miss | Cache miss, updated value. Slow |

Examples

% npx ts-node example/example.ts 
Sample: Lazy Cache
┌─────────┬────────────┬───────────┬───────┬─────┬─────┬─────┐
│ (index) │ methodType │ eventType │ count │ min │ avg │ max │
├─────────┼────────────┼───────────┼───────┼─────┼─────┼─────┤
│ 0       │ 'get'      │ 'hitA'    │ 38    │ 0   │ 0   │ 1   │
│ 1       │ 'get'      │ 'hitS'    │ 8     │ 98  │ 150 │ 201 │
│ 2       │ 'get'      │ 'miss'    │ 4     │ 300 │ 301 │ 302 │
└─────────┴────────────┴───────────┴───────┴─────┴─────┴─────┘
Sample: Lazy Cache with warmup
┌─────────┬────────────┬───────────┬───────┬─────┬─────┬─────┐
│ (index) │ methodType │ eventType │ count │ min │ avg │ max │
├─────────┼────────────┼───────────┼───────┼─────┼─────┼─────┤
│ 0       │ 'get'      │ 'hitA'    │ 40    │ 0   │ 0   │ 1   │
│ 1       │ 'get'      │ 'hitS'    │ 6     │ 99  │ 150 │ 203 │
│ 2       │ 'get'      │ 'miss'    │ 3     │ 301 │ 302 │ 303 │
└─────────┴────────────┴───────────┴───────┴─────┴─────┴─────┘
Sample: Eager Cache
┌─────────┬────────────┬───────────┬───────┬─────┬─────┬─────┐
│ (index) │ methodType │ eventType │ count │ min │ avg │ max │
├─────────┼────────────┼───────────┼───────┼─────┼─────┼─────┤
│ 0       │ 'get'      │ 'hitA'    │ 47    │ 0   │ 0   │ 1   │
│ 1       │ 'get'      │ 'hitS'    │ 2     │ 99  │ 149 │ 198 │
│ 2       │ 'get'      │ 'miss'    │ 1     │ 301 │ 301 │ 301 │
│ 3       │ 'update'   │ 'hitA'    │ 30    │ 0   │ 0   │ 1   │
│ 4       │ 'update'   │ 'hitS'    │ 12    │ 99  │ 150 │ 201 │
│ 5       │ 'update'   │ 'miss'    │ 5     │ 301 │ 302 │ 302 │
└─────────┴────────────┴───────────┴───────┴─────┴─────┴─────┘
Sample: Eager Cache with warmup
┌─────────┬────────────┬───────────┬───────┬─────┬─────┬─────┐
│ (index) │ methodType │ eventType │ count │ min │ avg │ max │
├─────────┼────────────┼───────────┼───────┼─────┼─────┼─────┤
│ 0       │ 'get'      │ 'hitA'    │ 50    │ 0   │ 0   │ 1   │
│ 1       │ 'update'   │ 'hitA'    │ 32    │ 0   │ 0   │ 1   │
│ 2       │ 'update'   │ 'hitS'    │ 15    │ 1   │ 140 │ 201 │
│ 3       │ 'update'   │ 'miss'    │ 6     │ 300 │ 301 │ 302 │
└─────────┴────────────┴───────────┴───────┴─────┴─────┴─────┘

Version History

  • 1.1.0
    • Add validator callback
  • 1.0.5
    • Enhance publish step
  • 1.0.4
    • Use seq based id instead of UUID for object id
  • 1.0.3
    • Add unit tests
  • 1.0.2
    • Update README(typo)
  • 1.0.1
    • Update README
  • 1.0.0
    • Initial Release

License

This project is under the MIT license. Copyright (c) 2024 Tomohisa Ota