collection-cache

3.2.0 • Public • Published

collection-cache

Simple request data caching.

npm bower build status

Getting Started

This module is used to cache backend data.

  • It has a queueing system to prevent multiple requests for the same data.
  • Data is tracked per request parameters (ex. sort method, etc.)
  • Data is stored in a single array (items are updated everywhere, regardless of request parameters).

It assumes data is stored as objects with ID keys.

Create a new cache instance

var fruits = new CollectionCache();

The following options can be passed in to the constructor (defaults shown):

{
  idKey: 'id',      // property that uniquely identifies object
  skipKey: 'skip',  // parameter representing start of data set
  limitKey: 'limit' // parameter representing amount of data stored
}

Assuming...

var queryParams = {
  sort: 'name',
  skip: 0,
  limit: 10
};

Querying and Storing

var deferred = $.Deferred();
 
fruits.get(
  queryParams,
  function(err, cachedFruits) {
    deferred.resolve(cachedFruits);
  },
  function(addToCache) {
    $http({
      url: '/fruits',
      method: 'GET',
      params: queryParams
    }).success(function(newFruits) {
      addToCache(newFruits);
    });
  }
);
 
return deferred.promise;

List All

fruits.list();
// => [
//      { id: 'orange', category: 'citrus' },
//      { id: 'blueberry', category: 'berries' }
//    ]

Find by ID

var orange = fruits.show('orange');
// => { id: 'orange', category: 'citrus' }

Find by ID and Update

fruits.update('orange', { category: 'berries' });
// => { id: 'orange', category: 'berries' }

Find by ID and Remove

fruits.remove('orange');

Clear Cache

fruits.destroy();

License

Copyright (c) 2015 Marius Craciunoiu. Licensed under the MIT license.

Readme

Keywords

Package Sidebar

Install

npm i collection-cache

Weekly Downloads

2

Version

3.2.0

License

MIT

Last publish

Collaborators

  • mariusc23