UNPKG

1.8 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
6
7var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
8
9function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
11var Cache = function () {
12 function Cache() {
13 (0, _classCallCheck3.default)(this, Cache);
14
15 this._root = null;
16 this._store = new Map();
17 }
18
19 Cache.prototype.empty = function empty() {
20 return this._store.size === 0;
21 };
22
23 Cache.prototype.has = function has(key) {
24 return this._store.has(key);
25 };
26
27 Cache.prototype.get = function get(key, defaultValue) {
28 var res = this.has(key) ? this._store.get(key) : this.root();
29 return typeof res === 'undefined' || res === null ? defaultValue : res;
30 };
31
32 Cache.prototype.add = function add(key, value) {
33 if (this.empty()) {
34 this._root = key;
35 }
36 this._store.set(key, value);
37 };
38
39 Cache.prototype.update = function update(key, value) {
40 if (this.has(key)) {
41 this._store.set(key, value);
42 }
43 };
44
45 Cache.prototype.remove = function remove(key) {
46 this._store.delete(key);
47
48 if (key === this._root) {
49 var maps = this._store.keys();
50 // 如果当前销毁的实例刚好是root,那么从map中取下一个加入的节点当作root
51 var nextkey = maps.next().value;
52 this._root = nextkey;
53 }
54 };
55
56 Cache.prototype.clear = function clear() {
57 this._store.clear();
58 };
59
60 Cache.prototype.root = function root() {
61 return this._store.get(this._root);
62 };
63
64 return Cache;
65}();
66
67exports.default = Cache;
68module.exports = exports['default'];
\No newline at end of file