Skip to content

Commit

Permalink
caching
Browse files Browse the repository at this point in the history
  • Loading branch information
derekpitt committed Oct 17, 2016
0 parents commit 1968fc5
Show file tree
Hide file tree
Showing 19 changed files with 633 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.DS_Store
13 changes: 13 additions & 0 deletions dist/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Cache = function Cache() {
_classCallCheck(this, Cache);
};

exports.Cache = Cache;
79 changes: 79 additions & 0 deletions dist/caching.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
declare module 'caching/cache' {
export interface cacheValue {
value: any;
expire: number;
}
export type cacheEntries = {
[key: string]: cacheValue;
};
export abstract class Cache {
abstract set<T>(key: string, value: T, durationMS?: number): void;
abstract get<T>(key: string): T;
abstract remove(key: string): void;
}

}
declare module 'caching/helpers' {
import { cacheValue } from 'caching/cache';
export function isExpired(val: cacheValue): boolean;
export function cacheFor(n: number): {
minutes: number;
seconds: number;
hours: number;
};

}
declare module 'caching/memory-cache' {
import { Cache } from 'caching/cache';
export class MemoryCache implements Cache {
private cache;
set<T>(key: string, value: T, durationMS?: any): void;
get<T>(key: string): T;
remove(key: string): void;
}

}
declare module 'caching/cookie-cache' {
import { Cache } from 'caching/cache';
export class CookieCache implements Cache {
private CookieName;
constructor(CookieName?: string);
set<T>(key: string, value: T, durationMS?: any): void;
get<T>(key: string): T;
remove(key: string): void;
private getCacheEntries();
private setCacheEntries(entries);
}

}
declare module 'caching/local-storage-cache' {
import { Cache } from 'caching/cache';
export class LocalStorageCache implements Cache {
set<T>(key: string, value: T, durationMS?: number): void;
get<T>(key: string): T;
remove(key: string): void;
}

}
declare module 'caching/scoped-cache' {
import { Cache } from 'caching/cache';
export class ScopedCache implements Cache {
private scope;
private cache;
constructor(scope: string, cache: Cache);
setScope(scope: string): void;
set<T>(key: string, value: T, durationMS?: any): void;
get<T>(key: string): T;
remove(key: string): void;
}

}
declare module 'caching' {
export { Cache } from 'caching/cache';
export { cacheFor } from 'caching/helpers';
export { MemoryCache } from 'caching/memory-cache';
export { CookieCache } from 'caching/cookie-cache';
export { LocalStorageCache } from 'caching/local-storage-cache';
export { ScopedCache } from 'caching/scoped-cache';

}
71 changes: 71 additions & 0 deletions dist/cookie-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});

var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var _helpers = require("./helpers");

var CookieCache = (function () {
function CookieCache() {
var CookieName = arguments.length <= 0 || arguments[0] === undefined ? "__cookie_cache" : arguments[0];

_classCallCheck(this, CookieCache);

this.CookieName = CookieName;
}

_createClass(CookieCache, [{
key: "set",
value: function set(key, value) {
var durationMS = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];

var expire = durationMS ? Date.now() + durationMS : null;
var currentEntries = this.getCacheEntries();
currentEntries[key] = { value: value, expire: expire };
this.setCacheEntries(currentEntries);
}
}, {
key: "get",
value: function get(key) {
var currentEntries = this.getCacheEntries();
var val = currentEntries[key];
if (val) {
if (!(0, _helpers.isExpired)(val)) {
return val.value;
} else {
// expired, delete
this.remove(key);
}
}
return null;
}
}, {
key: "remove",
value: function remove(key) {
var currentEntries = this.getCacheEntries();
delete currentEntries[key];
this.setCacheEntries(currentEntries);
}
}, {
key: "getCacheEntries",
value: function getCacheEntries() {
var decoded = window.atob(document.cookie.replace(new RegExp("(?:(?:^|.*;\\s*)" + this.CookieName + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1"));
return JSON.parse(decoded || "{}");
}
}, {
key: "setCacheEntries",
value: function setCacheEntries(entries) {
var encoded = window.btoa(JSON.stringify(entries));
document.cookie = this.CookieName + "=" + encoded + "; expires=0; path=/";
}
}]);

return CookieCache;
})();

exports.CookieCache = CookieCache;
19 changes: 19 additions & 0 deletions dist/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isExpired = isExpired;
exports.cacheFor = cacheFor;

function isExpired(val) {
if (val.expire == null) return false;
return val.expire < Date.now();
}

function cacheFor(n) {
var seconds = n * 1000;
var minutes = n * 60 * 1000;
var hours = n * 60 * 60 * 1000;
return { minutes: minutes, seconds: seconds, hours: hours };
}
59 changes: 59 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});

var _cache = require("./cache");

Object.defineProperty(exports, "Cache", {
enumerable: true,
get: function get() {
return _cache.Cache;
}
});

var _helpers = require("./helpers");

Object.defineProperty(exports, "cacheFor", {
enumerable: true,
get: function get() {
return _helpers.cacheFor;
}
});

var _memoryCache = require("./memory-cache");

Object.defineProperty(exports, "MemoryCache", {
enumerable: true,
get: function get() {
return _memoryCache.MemoryCache;
}
});

var _cookieCache = require("./cookie-cache");

Object.defineProperty(exports, "CookieCache", {
enumerable: true,
get: function get() {
return _cookieCache.CookieCache;
}
});

var _localStorageCache = require("./local-storage-cache");

Object.defineProperty(exports, "LocalStorageCache", {
enumerable: true,
get: function get() {
return _localStorageCache.LocalStorageCache;
}
});

var _scopedCache = require("./scoped-cache");

Object.defineProperty(exports, "ScopedCache", {
enumerable: true,
get: function get() {
return _scopedCache.ScopedCache;
}
});
47 changes: 47 additions & 0 deletions dist/local-storage-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});

var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var _helpers = require("./helpers");

var LocalStorageCache = (function () {
function LocalStorageCache() {
_classCallCheck(this, LocalStorageCache);
}

_createClass(LocalStorageCache, [{
key: "set",
value: function set(key, value, durationMS) {
var expire = durationMS ? Date.now() + durationMS : null;
localStorage[key] = JSON.stringify({ value: value, expire: expire });
}
}, {
key: "get",
value: function get(key) {
var val = JSON.parse(localStorage[key] || "null");
if (val) {
if (!(0, _helpers.isExpired)(val)) {
return val.value;
} else {
this.remove(key);
}
}
return null;
}
}, {
key: "remove",
value: function remove(key) {
delete localStorage[key];
}
}]);

return LocalStorageCache;
})();

exports.LocalStorageCache = LocalStorageCache;
51 changes: 51 additions & 0 deletions dist/memory-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});

var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var _helpers = require("./helpers");

var MemoryCache = (function () {
function MemoryCache() {
_classCallCheck(this, MemoryCache);

this.cache = {};
}

_createClass(MemoryCache, [{
key: "set",
value: function set(key, value) {
var durationMS = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];

var expire = durationMS ? Date.now() + durationMS : null;
this.cache[key] = { value: value, expire: expire };
}
}, {
key: "get",
value: function get(key) {
var val = this.cache[key];
if (val) {
if (!(0, _helpers.isExpired)(val)) {
return val.value;
} else {
this.remove(key);
}
}
return null;
}
}, {
key: "remove",
value: function remove(key) {
delete this.cache[key];
}
}]);

return MemoryCache;
})();

exports.MemoryCache = MemoryCache;
Loading

0 comments on commit 1968fc5

Please sign in to comment.