Index

### collection-cache 1.1.0

# collection-cache

> Simple request data caching.

## Getting Started

#### Create a new cache instance

    var fruits = new CacheCollection();

#### Assuming...

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

#### Add items to cache

    fruits.add(queryParams, ['apples', 'oranges'], function(err, cachedFruits) {
      if (err) { throw err; }
      console.log(cachedFruits);
    });

    // or shorthand
    var cachedFruits = fruits.add(queryParams, ['apples', 'oranges']);

#### Get items from cache

    fruits.get(queryParams, function(err, cachedFruits) {
      if (err) { throw err; }
      console.log(cachedFruits);
    });

    // or shorthand
    var cachedFruits = fruits.get(queryParams);

#### All together now

    var deferred = $.Deferred();

    // Check if already cached
    fruits.get(queryParams, function(err, cachedFruits) {
      // If cached, return cached version
      if (cachedFruits.length) {
        deferred.resolve(cachedFruits);
      }
      // Otherwise, fetch from backend
      else {
        $http({
          url: '/fruits',
          method: 'GET',
          params: queryParams
        }).success(function(newFruits) {
          // And cache response
          fruits.add(queryParams, newFruits);
          deferred.resolve(newFruits);
        });
      }
    });

    return deferred.promise;

## License

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