cione-comp-lib
Version:
`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`
78 lines (77 loc) • 2.49 kB
JavaScript
/* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var _a, _b;
const $retainerCount = Symbol("retainerCount");
const $recentlyUsed = Symbol("recentlyUsed");
const $evict = Symbol("evict");
const $evictionThreshold = Symbol("evictionThreshold");
const $cache = Symbol("cache");
class CacheEvictionPolicy {
constructor(cache, evictionThreshold = 5) {
this[_a] = /* @__PURE__ */ new Map();
this[_b] = [];
this[$cache] = cache;
this[$evictionThreshold] = evictionThreshold;
}
set evictionThreshold(value) {
this[$evictionThreshold] = value;
this[$evict]();
}
get evictionThreshold() {
return this[$evictionThreshold];
}
get cache() {
return this[$cache];
}
retainerCount(key) {
return this[$retainerCount].get(key) || 0;
}
reset() {
this[$retainerCount].clear();
this[$recentlyUsed] = [];
}
retain(key) {
if (!this[$retainerCount].has(key)) {
this[$retainerCount].set(key, 0);
}
this[$retainerCount].set(key, this[$retainerCount].get(key) + 1);
const recentlyUsedIndex = this[$recentlyUsed].indexOf(key);
if (recentlyUsedIndex !== -1) {
this[$recentlyUsed].splice(recentlyUsedIndex, 1);
}
this[$recentlyUsed].unshift(key);
this[$evict]();
}
release(key) {
if (this[$retainerCount].has(key)) {
this[$retainerCount].set(key, Math.max(this[$retainerCount].get(key) - 1, 0));
}
this[$evict]();
}
[(_a = $retainerCount, _b = $recentlyUsed, $evict)]() {
if (this[$recentlyUsed].length < this[$evictionThreshold]) {
return;
}
for (let i = this[$recentlyUsed].length - 1; i >= this[$evictionThreshold]; --i) {
const key = this[$recentlyUsed][i];
const retainerCount = this[$retainerCount].get(key);
if (retainerCount === 0) {
this[$cache].delete(key);
this[$recentlyUsed].splice(i, 1);
}
}
}
}
export { CacheEvictionPolicy };