1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const moment = require("moment");
|
4 | const Cache = require("safe-memory-cache/map");
|
5 | class HttpCache {
|
6 | constructor(maxKeysSize = 500) {
|
7 | this._items = new Cache({
|
8 | limit: maxKeysSize
|
9 | });
|
10 | }
|
11 | dispose() {
|
12 | this._items.clear();
|
13 | this._items = null;
|
14 | }
|
15 | set(url, changeVector, result) {
|
16 | const httpCacheItem = new HttpCacheItem();
|
17 | httpCacheItem.changeVector = changeVector;
|
18 | httpCacheItem.payload = result;
|
19 | httpCacheItem.cache = this;
|
20 | this._items.set(url, httpCacheItem);
|
21 | }
|
22 | get(url, itemInfoCallback) {
|
23 | const item = this._items.get(url);
|
24 | if (item) {
|
25 | if (itemInfoCallback) {
|
26 | itemInfoCallback({
|
27 | changeVector: item.changeVector,
|
28 | response: item.payload
|
29 | });
|
30 | }
|
31 | return new ReleaseCacheItem(item);
|
32 | }
|
33 | if (itemInfoCallback) {
|
34 | itemInfoCallback({
|
35 | changeVector: null,
|
36 | response: null
|
37 | });
|
38 | }
|
39 | return new ReleaseCacheItem(null);
|
40 | }
|
41 | setNotFound(url) {
|
42 | const httpCacheItem = new HttpCacheItem();
|
43 | httpCacheItem.changeVector = "404 response";
|
44 | httpCacheItem.cache = this;
|
45 | this._items.set(url, httpCacheItem);
|
46 | }
|
47 | get numberOfItems() {
|
48 | return this._items["_get_buckets"]().reduce((result, next) => {
|
49 | return result + next.size;
|
50 | }, 0);
|
51 | }
|
52 | getMightHaveBeenModified() {
|
53 | return false;
|
54 | }
|
55 | }
|
56 | exports.HttpCache = HttpCache;
|
57 | class ReleaseCacheItem {
|
58 | constructor(item) {
|
59 | this.item = item;
|
60 | }
|
61 | notModified() {
|
62 | if (this.item) {
|
63 | this.item.lastServerUpdate = moment().toDate();
|
64 | }
|
65 | }
|
66 | get age() {
|
67 | if (!this.item) {
|
68 | return Number.MAX_VALUE;
|
69 | }
|
70 | return new Date().valueOf() - this.item.lastServerUpdate.valueOf();
|
71 | }
|
72 | get mightHaveBeenModified() {
|
73 | return false;
|
74 | }
|
75 | }
|
76 | exports.ReleaseCacheItem = ReleaseCacheItem;
|
77 | class HttpCacheItem {
|
78 | constructor() {
|
79 | this.lastServerUpdate = moment().toDate();
|
80 | }
|
81 | }
|
82 | exports.HttpCacheItem = HttpCacheItem;
|