Spaces:
Sleeping
Sleeping
| /** | |
| * 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; | |