Skip to content

Commit 1968fc5

Browse files
committed
caching
0 parents  commit 1968fc5

19 files changed

+633
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.DS_Store

dist/cache.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8+
9+
var Cache = function Cache() {
10+
_classCallCheck(this, Cache);
11+
};
12+
13+
exports.Cache = Cache;

dist/caching.d.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
declare module 'caching/cache' {
2+
export interface cacheValue {
3+
value: any;
4+
expire: number;
5+
}
6+
export type cacheEntries = {
7+
[key: string]: cacheValue;
8+
};
9+
export abstract class Cache {
10+
abstract set<T>(key: string, value: T, durationMS?: number): void;
11+
abstract get<T>(key: string): T;
12+
abstract remove(key: string): void;
13+
}
14+
15+
}
16+
declare module 'caching/helpers' {
17+
import { cacheValue } from 'caching/cache';
18+
export function isExpired(val: cacheValue): boolean;
19+
export function cacheFor(n: number): {
20+
minutes: number;
21+
seconds: number;
22+
hours: number;
23+
};
24+
25+
}
26+
declare module 'caching/memory-cache' {
27+
import { Cache } from 'caching/cache';
28+
export class MemoryCache implements Cache {
29+
private cache;
30+
set<T>(key: string, value: T, durationMS?: any): void;
31+
get<T>(key: string): T;
32+
remove(key: string): void;
33+
}
34+
35+
}
36+
declare module 'caching/cookie-cache' {
37+
import { Cache } from 'caching/cache';
38+
export class CookieCache implements Cache {
39+
private CookieName;
40+
constructor(CookieName?: string);
41+
set<T>(key: string, value: T, durationMS?: any): void;
42+
get<T>(key: string): T;
43+
remove(key: string): void;
44+
private getCacheEntries();
45+
private setCacheEntries(entries);
46+
}
47+
48+
}
49+
declare module 'caching/local-storage-cache' {
50+
import { Cache } from 'caching/cache';
51+
export class LocalStorageCache implements Cache {
52+
set<T>(key: string, value: T, durationMS?: number): void;
53+
get<T>(key: string): T;
54+
remove(key: string): void;
55+
}
56+
57+
}
58+
declare module 'caching/scoped-cache' {
59+
import { Cache } from 'caching/cache';
60+
export class ScopedCache implements Cache {
61+
private scope;
62+
private cache;
63+
constructor(scope: string, cache: Cache);
64+
setScope(scope: string): void;
65+
set<T>(key: string, value: T, durationMS?: any): void;
66+
get<T>(key: string): T;
67+
remove(key: string): void;
68+
}
69+
70+
}
71+
declare module 'caching' {
72+
export { Cache } from 'caching/cache';
73+
export { cacheFor } from 'caching/helpers';
74+
export { MemoryCache } from 'caching/memory-cache';
75+
export { CookieCache } from 'caching/cookie-cache';
76+
export { LocalStorageCache } from 'caching/local-storage-cache';
77+
export { ScopedCache } from 'caching/scoped-cache';
78+
79+
}

dist/cookie-cache.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
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; }; })();
8+
9+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
10+
11+
var _helpers = require("./helpers");
12+
13+
var CookieCache = (function () {
14+
function CookieCache() {
15+
var CookieName = arguments.length <= 0 || arguments[0] === undefined ? "__cookie_cache" : arguments[0];
16+
17+
_classCallCheck(this, CookieCache);
18+
19+
this.CookieName = CookieName;
20+
}
21+
22+
_createClass(CookieCache, [{
23+
key: "set",
24+
value: function set(key, value) {
25+
var durationMS = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];
26+
27+
var expire = durationMS ? Date.now() + durationMS : null;
28+
var currentEntries = this.getCacheEntries();
29+
currentEntries[key] = { value: value, expire: expire };
30+
this.setCacheEntries(currentEntries);
31+
}
32+
}, {
33+
key: "get",
34+
value: function get(key) {
35+
var currentEntries = this.getCacheEntries();
36+
var val = currentEntries[key];
37+
if (val) {
38+
if (!(0, _helpers.isExpired)(val)) {
39+
return val.value;
40+
} else {
41+
// expired, delete
42+
this.remove(key);
43+
}
44+
}
45+
return null;
46+
}
47+
}, {
48+
key: "remove",
49+
value: function remove(key) {
50+
var currentEntries = this.getCacheEntries();
51+
delete currentEntries[key];
52+
this.setCacheEntries(currentEntries);
53+
}
54+
}, {
55+
key: "getCacheEntries",
56+
value: function getCacheEntries() {
57+
var decoded = window.atob(document.cookie.replace(new RegExp("(?:(?:^|.*;\\s*)" + this.CookieName + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1"));
58+
return JSON.parse(decoded || "{}");
59+
}
60+
}, {
61+
key: "setCacheEntries",
62+
value: function setCacheEntries(entries) {
63+
var encoded = window.btoa(JSON.stringify(entries));
64+
document.cookie = this.CookieName + "=" + encoded + "; expires=0; path=/";
65+
}
66+
}]);
67+
68+
return CookieCache;
69+
})();
70+
71+
exports.CookieCache = CookieCache;

dist/helpers.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.isExpired = isExpired;
7+
exports.cacheFor = cacheFor;
8+
9+
function isExpired(val) {
10+
if (val.expire == null) return false;
11+
return val.expire < Date.now();
12+
}
13+
14+
function cacheFor(n) {
15+
var seconds = n * 1000;
16+
var minutes = n * 60 * 1000;
17+
var hours = n * 60 * 60 * 1000;
18+
return { minutes: minutes, seconds: seconds, hours: hours };
19+
}

dist/index.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var _cache = require("./cache");
8+
9+
Object.defineProperty(exports, "Cache", {
10+
enumerable: true,
11+
get: function get() {
12+
return _cache.Cache;
13+
}
14+
});
15+
16+
var _helpers = require("./helpers");
17+
18+
Object.defineProperty(exports, "cacheFor", {
19+
enumerable: true,
20+
get: function get() {
21+
return _helpers.cacheFor;
22+
}
23+
});
24+
25+
var _memoryCache = require("./memory-cache");
26+
27+
Object.defineProperty(exports, "MemoryCache", {
28+
enumerable: true,
29+
get: function get() {
30+
return _memoryCache.MemoryCache;
31+
}
32+
});
33+
34+
var _cookieCache = require("./cookie-cache");
35+
36+
Object.defineProperty(exports, "CookieCache", {
37+
enumerable: true,
38+
get: function get() {
39+
return _cookieCache.CookieCache;
40+
}
41+
});
42+
43+
var _localStorageCache = require("./local-storage-cache");
44+
45+
Object.defineProperty(exports, "LocalStorageCache", {
46+
enumerable: true,
47+
get: function get() {
48+
return _localStorageCache.LocalStorageCache;
49+
}
50+
});
51+
52+
var _scopedCache = require("./scoped-cache");
53+
54+
Object.defineProperty(exports, "ScopedCache", {
55+
enumerable: true,
56+
get: function get() {
57+
return _scopedCache.ScopedCache;
58+
}
59+
});

dist/local-storage-cache.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
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; }; })();
8+
9+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
10+
11+
var _helpers = require("./helpers");
12+
13+
var LocalStorageCache = (function () {
14+
function LocalStorageCache() {
15+
_classCallCheck(this, LocalStorageCache);
16+
}
17+
18+
_createClass(LocalStorageCache, [{
19+
key: "set",
20+
value: function set(key, value, durationMS) {
21+
var expire = durationMS ? Date.now() + durationMS : null;
22+
localStorage[key] = JSON.stringify({ value: value, expire: expire });
23+
}
24+
}, {
25+
key: "get",
26+
value: function get(key) {
27+
var val = JSON.parse(localStorage[key] || "null");
28+
if (val) {
29+
if (!(0, _helpers.isExpired)(val)) {
30+
return val.value;
31+
} else {
32+
this.remove(key);
33+
}
34+
}
35+
return null;
36+
}
37+
}, {
38+
key: "remove",
39+
value: function remove(key) {
40+
delete localStorage[key];
41+
}
42+
}]);
43+
44+
return LocalStorageCache;
45+
})();
46+
47+
exports.LocalStorageCache = LocalStorageCache;

dist/memory-cache.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
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; }; })();
8+
9+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
10+
11+
var _helpers = require("./helpers");
12+
13+
var MemoryCache = (function () {
14+
function MemoryCache() {
15+
_classCallCheck(this, MemoryCache);
16+
17+
this.cache = {};
18+
}
19+
20+
_createClass(MemoryCache, [{
21+
key: "set",
22+
value: function set(key, value) {
23+
var durationMS = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];
24+
25+
var expire = durationMS ? Date.now() + durationMS : null;
26+
this.cache[key] = { value: value, expire: expire };
27+
}
28+
}, {
29+
key: "get",
30+
value: function get(key) {
31+
var val = this.cache[key];
32+
if (val) {
33+
if (!(0, _helpers.isExpired)(val)) {
34+
return val.value;
35+
} else {
36+
this.remove(key);
37+
}
38+
}
39+
return null;
40+
}
41+
}, {
42+
key: "remove",
43+
value: function remove(key) {
44+
delete this.cache[key];
45+
}
46+
}]);
47+
48+
return MemoryCache;
49+
})();
50+
51+
exports.MemoryCache = MemoryCache;

0 commit comments

Comments
 (0)