5.17.0-next.1596512215.a12e384c3f3b26857a870b383ec27bf484788586 • Published 5 years ago

cachely v5.17.0-next.1596512215.a12e384c3f3b26857a870b383ec27bf484788586

Weekly downloads
17
License
MIT
Repository
github
Last release
5 years ago

A tiny wrapper that sits around your request function that caches its data for a specified duration, provides updates as requested rather than polling each interval

Usage

Complete API Documentation.

// Import cachely
import Cachely from 'cachely'

// For our retriever, have a locally scoped variable that will showcase the caching
let fetches = 0

// Create the wrapper retriever in the cachely cache service
const cachely = new Cachely({
    // The method that will fetch the data
    retrieve() {
        return new Promise(function (resolve) {
            // after a one second delay, return the number of fetches that we have done
            setTimeout(() => resolve(++fetches), 1000)
        })
    },

    // An optional duration in milliseconds that our cache of the data will be valid for
    // When expired, on the next request of the data, we will use the method to get the latest data
    // Defaults to one day
    duration: 2000, // in this example we set it to two seconds

    // An optional function that receives debugging log messages
    // Defaults to nothing
    log: console.log,
})

// do an initial fetch of the data
cachely
    .resolve()
    .catch(console.error)
    .then(
        console.log.bind(
            console,
            'after one second as specified in our method, the result data should still be 1:'
        )
    )

// do a subsequent fetch of the data that will be from the cache
cachely
    .resolve()
    .catch(console.error)
    .then(
        console.log.bind(
            console,
            'after a tiny delay this will be from cache, the result data should still be 1:'
        )
    )

// wait 3000ms for the cache to invalidate itself
setTimeout(function () {
    // do a second fetch of the data
    cachely
        .resolve()
        .catch(console.error)
        .then(
            console.log.bind(
                console,
                'after one second as specified in our method, the result data should be 2, as it was our second fetch:'
            )
        )

    // do a subsequent fetch of the data that will be from the cache
    cachely
        .resolve()
        .catch(console.error)
        .then(
            console.log.bind(
                console,
                'after a tiny delay this will be from cache, the result data should still be 2:'
            )
        )

    // peform a manual invalidation
    cachely.invalidate()

    // do a third fetch of the data
    cachely
        .resolve()
        .catch(console.error)
        .then(
            console.log.bind(
                console,
                'after one second as specified in our method, the result data should be 3, as it was our third fetch:'
            )
        )

    // do a subsequent fetch of the data that will be from the cache
    cachely
        .resolve()
        .catch(console.error)
        .then(
            console.log.bind(
                console,
                'after a tiny delay this will be from cache, the result data should still be 3:'
            )
        )
}, 3000)

npm

Deno

import pkg from 'https://unpkg.com/cachely@^5.17.0/edition-deno/index.ts'

pika

<script type="module">
    import pkg from '//cdn.pika.dev/cachely/^5.17.0'
</script>

unpkg

<script type="module">
    import pkg from '//unpkg.com/cachely@^5.17.0'
</script>

jspm

<script type="module">
    import pkg from '//dev.jspm.io/cachely@5.17.0'
</script>

Discover the release history by heading on over to the HISTORY.md file.

Discover how you can contribute by heading on over to the CONTRIBUTING.md file.

These amazing people are maintaining this project:

No sponsors yet! Will you be the first?

These amazing people have contributed code to this project:

Discover how you can contribute by heading on over to the CONTRIBUTING.md file.

Unless stated otherwise all works are:

and licensed under:

6.8.0

2 years ago

6.7.0

2 years ago

6.6.0

2 years ago

6.1.0

2 years ago

6.5.0

2 years ago

6.2.0

2 years ago

5.24.0

2 years ago

6.3.0

2 years ago

6.0.0

2 years ago

6.4.0

2 years ago

5.23.0

4 years ago

5.22.0

4 years ago

5.21.0

4 years ago

5.20.0

5 years ago

5.18.0

5 years ago

5.17.0

5 years ago

5.16.0

5 years ago

5.15.0

5 years ago

5.14.0

6 years ago

5.13.0

6 years ago

5.12.0

6 years ago

5.11.0

6 years ago

5.10.0

6 years ago

5.9.0

6 years ago

5.8.0

6 years ago

5.7.0

6 years ago

5.6.0

6 years ago

5.5.0

6 years ago

5.4.0

6 years ago

5.3.0

6 years ago

5.2.0

6 years ago

5.1.0

6 years ago

5.0.0

6 years ago

4.0.0

6 years ago

3.1.0

6 years ago

3.0.0

6 years ago

2.5.0

6 years ago

2.7.0

6 years ago

2.6.0

6 years ago

2.4.0

6 years ago

2.3.0

6 years ago

2.2.0

6 years ago

2.1.0

7 years ago

2.0.0

8 years ago

1.1.0

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago