Skip to content

Commit

Permalink
Merge pull request #120 from VisActor/feat/lru-static-dynamic
Browse files Browse the repository at this point in the history
feat: change static class lru to object
  • Loading branch information
xile611 authored Nov 7, 2023
2 parents 8c880f9 + 77f96f6 commit 7364d54
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@visactor/vutils",
"comment": "feat: change static class lru to object",
"type": "none"
}
],
"packageName": "@visactor/vutils"
}
24 changes: 12 additions & 12 deletions packages/vutils/src/lru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ interface Threshold {
}
// LRU清除
export class LRU {
private static CLEAN_THRESHOLD = 1e3;
private static L_TIME = 1000; // 1000ms以内
private static R_COUNT = 1; // 使用次数为0
private static R_TIMESTAMP_MAX_SIZE = 20; // timestamp最大长度
private CLEAN_THRESHOLD = 1e3;
private L_TIME = 1000; // 1000ms以内
private R_COUNT = 1; // 使用次数为0
private R_TIMESTAMP_MAX_SIZE = 20; // timestamp最大长度

static clearCache<TK, TV extends { timestamp: number[] }>(cache: Map<TK, TV>, params: Threshold): number {
const { CLEAN_THRESHOLD = LRU.CLEAN_THRESHOLD, L_TIME = LRU.L_TIME, R_COUNT = LRU.R_COUNT } = params;
clearCache<TK, TV extends { timestamp: number[] }>(cache: Map<TK, TV>, params: Threshold): number {
const { CLEAN_THRESHOLD = this.CLEAN_THRESHOLD, L_TIME = this.L_TIME, R_COUNT = this.R_COUNT } = params;
if (cache.size < CLEAN_THRESHOLD) {
return 0;
}
Expand Down Expand Up @@ -46,17 +46,17 @@ export class LRU {
return clearNum;
}

static addLimitedTimestamp<T extends { timestamp: number[] }>(cacheItem: T, t: number, params: Threshold) {
const { R_TIMESTAMP_MAX_SIZE = LRU.R_TIMESTAMP_MAX_SIZE } = params;
addLimitedTimestamp<T extends { timestamp: number[] }>(cacheItem: T, t: number, params: Threshold) {
const { R_TIMESTAMP_MAX_SIZE = this.R_TIMESTAMP_MAX_SIZE } = params;
if (cacheItem.timestamp.length > R_TIMESTAMP_MAX_SIZE) {
// cacheItem.timestamp[cacheItem.timestamp.length-1] =
cacheItem.timestamp.shift();
}
cacheItem.timestamp.push(t);
}

static clearTimeStamp<TK, TV extends { timestamp: number[] }>(cache: Map<TK, TV>, params: Threshold) {
const { L_TIME = LRU.L_TIME } = params;
clearTimeStamp<TK, TV extends { timestamp: number[] }>(cache: Map<TK, TV>, params: Threshold) {
const { L_TIME = this.L_TIME } = params;
const now = Date.now();
cache.forEach(item => {
while (now - item.timestamp[0] > L_TIME) {
Expand All @@ -65,8 +65,8 @@ export class LRU {
});
}

static clearItemTimestamp<T extends { timestamp: number[] }>(cacheItem: T, params: Threshold) {
const { L_TIME = LRU.L_TIME } = params;
clearItemTimestamp<T extends { timestamp: number[] }>(cacheItem: T, params: Threshold) {
const { L_TIME = this.L_TIME } = params;
const now = Date.now();
while (now - cacheItem.timestamp[0] > L_TIME) {
cacheItem.timestamp.shift();
Expand Down

0 comments on commit 7364d54

Please sign in to comment.