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

ampersand-local-cache-mixin

v3.0.2

Published

Only fetch when your data is old or stale. Easily configurable localStorage cache for ampersand & backbone state objects, models, and collections.

Downloads

4

Readme

ampersand-local-cache-mixin

Only fetch when your data is old or stale. Easily configurable localStorage cache for ampersand & backbone state objects, models, and collections.

Note that this is not an alternate sync implementation, this is to let you configure and trust a local cache but ultimately get your data from a remote source.

Works with Ampersand.js and Backbone in state objects, models, and collections.

install

npm install ampersand-local-cache-mixin

example

var cacheMixin = require('ampersand-local-cache-mixin');
var Model = require('ampersand-model');
var ms = require('milliseconds');

module.exports = Model.extend(cacheMixin, {
    // "Time To Live". If more than the `ttl` (a number in milliseconds)
    // has passed since last stored last, it won't use the cache.
    // (note we're using the `miliseconds` module here for better readability)
    ttl: ms.weeks(1),

    // "Time To Stale". Similarly you can optinally provide a `tts`.
    // This is the time in milliseconds after which to consider the data
    // to be stale. In this scenario a `stale` event will be triggered
    // on the instance, but the data will still be read/set. This is good
    // for cases where you quickly want to show the data you have but still
    // got fetch new stuff in the background.
    // (note we're using the `miliseconds` module here for better readability)
    tts: ms.minutes(5),

    // The key (or function that returns a key) to use when storing this object
    // to localstorage
    storageKey: 'me',

    // After setting these options, call `initStorage` on your host object
    initialize: function () {
        // sets up our mixin and reads if it has it
        this.initStorage();

        // listen to `stale` and `empty` events
        // fetch on both
        this.on('stale empty', this.fetch, this)

        // **note: you have to tell it when to write to storage**
        // otherwise nothing ever gets cached.
        this.on('change', this.writeToStorage, this)
    }
})

Other details

There are two overwriteable methods that determine what gets stored and retrieved.

Here's how they look by default:

  serializeToStorage: function () {
    return this.toJSON()
  },
  parseFromStorage: function (data) {
    return data
  },

If you want to customize how it gets parsed/read from storage, simply overwrite those methods.

note: By default only props will get stored, not session or derived properties

More docs?

This is really just a simple object with a few methods. It's quite short and readable, I'd suggest reading the source to know exactly what's going on.

But also,

credits

If you like this follow @HenrikJoreteg on twitter.

license

MIT