Skip to content

Instantly share code, notes, and snippets.

@VanDalkvist
Created October 31, 2014 13:46
Show Gist options
  • Save VanDalkvist/1bba5ea61eefa5887f82 to your computer and use it in GitHub Desktop.
Save VanDalkvist/1bba5ea61eefa5887f82 to your computer and use it in GitHub Desktop.
(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);
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment