Spaces:
Sleeping
Sleeping
File size: 1,847 Bytes
e4bf523 | 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 | 'use strict'
/**
* Copyright (c) 2017~2019, OBCon Inc.
* All rights reserved.
*/
/**
* @file
* @copyright 2017~2019, OBCon Inc.
* @author gye hyun james kim [pnuskgh@gmail.com]
*/
let moment = require('moment');
//--- 일반적으로 LRU가 LFU보다 우수함
//--- LRU (Least Recently Used, 가장 최근 사용) : 가장 오래된 것 삭제
//--- LFU (Least Frequently Used, 최소 빈도 사용) : 참조 횟수가 가장 작은 것 삭제
class Cache {
constructor(capacity, isLFU=true) {
this.isLFU = isLFU;
this.capacity = capacity;
this.size = 0;
this.cacheDataset = {}; //--- Cache 데이터 저장
// this.cacheFreq = {}; //--- Cache 사용 빈도 저장 for LFU -> To-Do: Linked List
this.cacheDatetime = {}; //--- Cache의 최근 접근일시 저장 for LRU -> To-Do: Linked List
this.size = 0;
}
getCache(key) {
if (this.existCache(key)) {
// this.cacheFreq[key] = this.cacheFreq[key] + 1;
this.cacheDatetime[key] = moment();
return this.cacheDataset[key];
}
return null;
}
setCache(key, value) {
this.cacheDataset[key] = value;
// this.cacheFreq[key] = (this.existCache(key)) ? this.cacheFreq[key] + 1:1;
this.cacheDatetime[key] = moment();
}
existCache(key) {
return this.cacheDataset.hasOwnProperty(key);
}
delCache(key) {
if (this.existCache(key)) {
delete this.cacheDataset[key];
// delete this.cacheFreq[key];
delete this.cacheDatetime[key];
this.size = this.size - 1;
}
}
}
module.exports = Cache;
|