-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.js
118 lines (94 loc) · 2.33 KB
/
entry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'use strict';
const os = require('os');
const utils = require('./utils');
const _key = Symbol('_key');
const _value = Symbol('_value');
const _ttl = Symbol('_ttl');
const _origin = Symbol('_origin');
const _expires = Symbol('_expires');
const TtlMemCacheEntry = class TtlMemCacheEntry {
constructor({
key, value = null, ttl = -1, origin = null, expires
} = {}) {
this[_key] = key;
this[_value] = value;
this[_ttl] = ttl;
this[_origin] = origin;
this[_expires] = expires || utils.calculateExpire(this.ttl);
}
/**
* Meta
*/
get [Symbol.toStringTag]() {
return 'TtlMemCacheEntry';
}
[Symbol.toPrimitive](hint) {
if (hint === 'string') return `${JSON.stringify(this)}${os.EOL}`;
return Object.prototype.toString.call(this);
}
/**
* Public keys
*/
get key() {
return this[_key];
}
set key(val) {
throw new Error('Cannot set read-only property.');
}
get value() {
return this[_value];
}
set value(val) {
throw new Error('Cannot set read-only property.');
}
get ttl() {
return this[_ttl];
}
set ttl(val) {
throw new Error('Cannot set read-only property.');
}
get origin() {
return this[_origin];
}
set origin(val) {
throw new Error('Cannot set read-only property.');
}
get expires() {
return this[_expires];
}
set expires(val) {
throw new Error('Cannot set read-only property.');
}
/**
* Public methods
*/
expired(now) {
return utils.expired(this.expires, now);
}
toJSON() {
return {
key: this.key,
value: this.value,
ttl: this.ttl,
origin: this.origin,
expires: this.expires,
type: 'TtlMemCacheEntry',
};
}
/**
* Static methods
*/
static assertLoose(obj = {}) {
if (utils.isNotEmpty(obj.key) && utils.isNotEmpty(obj.value)) {
return true;
}
return false;
}
static assertStrict(obj = {}) {
if (utils.isNotEmpty(obj.key) && utils.isNotEmpty(obj.value) && utils.isNotEmpty(obj.ttl)) {
return true;
}
return false;
}
};
module.exports = TtlMemCacheEntry;