VanDalkvist
10/31/2014 - 1:46 PM

angular.cache.js

(function () {
    'use strict';

    angular.module('main')
        .provider('cache', function () {

            // #region initialization

            var storage;

            var storageFactory = {
                create: function (storageType) {
                    return window[storageType];
                }
            };

            this.init = function (storageType) {
                storage = storageFactory.create(storageType);
            };

            this.$get = function () {
                return {
                    get: _get,
                    put: _put,
                    remove: _remove
                };
            };

            // #region private functions

            function _get(key) {
                var item = storage.getItem(key);
                return item ? angular.fromJson(item) : item;
            }

            function _put(key, value) {
                storage.setItem(key, angular.toJson(value));
            }

            function _remove(key) {
                storage.removeItem(key);
            }
        });
})();