16e80583aSopenharmony_ci/** 26e80583aSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 36e80583aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 46e80583aSopenharmony_ci * you may not use this file except in compliance with the License. 56e80583aSopenharmony_ci * You may obtain a copy of the License at 66e80583aSopenharmony_ci * 76e80583aSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 86e80583aSopenharmony_ci * 96e80583aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 106e80583aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 116e80583aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 126e80583aSopenharmony_ci * See the License for the specific language governing permissions and 136e80583aSopenharmony_ci * limitations under the License. 146e80583aSopenharmony_ci */ 156e80583aSopenharmony_ci 166e80583aSopenharmony_ci 176e80583aSopenharmony_ciconst TAG = 'DiskLruCache'; 186e80583aSopenharmony_ci 196e80583aSopenharmony_ci/** 206e80583aSopenharmony_ci * A class provides persistent operation for memory cache. 216e80583aSopenharmony_ci */ 226e80583aSopenharmony_ciexport default class DiskLruCache { 236e80583aSopenharmony_ci private readonly cache; 246e80583aSopenharmony_ci private readonly capacity; 256e80583aSopenharmony_ci 266e80583aSopenharmony_ci constructor(capacity = 100) { 276e80583aSopenharmony_ci this.cache = new Map(); 286e80583aSopenharmony_ci this.capacity = capacity; 296e80583aSopenharmony_ci this.initMap(); //read cache from local 306e80583aSopenharmony_ci } 316e80583aSopenharmony_ci 326e80583aSopenharmony_ci /** 336e80583aSopenharmony_ci * Init the cache whether the file has data. 346e80583aSopenharmony_ci */ 356e80583aSopenharmony_ci initMap(): void { 366e80583aSopenharmony_ci } 376e80583aSopenharmony_ci 386e80583aSopenharmony_ci /** 396e80583aSopenharmony_ci * Get cache from disk. 406e80583aSopenharmony_ci * 416e80583aSopenharmony_ci * @param {string} key - key of the cache map 426e80583aSopenharmony_ci * @return {object} - target cache object 436e80583aSopenharmony_ci */ 446e80583aSopenharmony_ci getCache(key: string) { 456e80583aSopenharmony_ci if (this.cache.has(key)) { 466e80583aSopenharmony_ci // exist and update 476e80583aSopenharmony_ci const temp = this.cache.get(key); 486e80583aSopenharmony_ci //delete the old cache 496e80583aSopenharmony_ci this.cache.delete(key); 506e80583aSopenharmony_ci //update the cache to recent use 516e80583aSopenharmony_ci this.cache.set(key, temp); 526e80583aSopenharmony_ci //update local cache to recent use 536e80583aSopenharmony_ci return temp; 546e80583aSopenharmony_ci } 556e80583aSopenharmony_ci return -1; 566e80583aSopenharmony_ci } 576e80583aSopenharmony_ci 586e80583aSopenharmony_ci /** 596e80583aSopenharmony_ci * Put cache to disk. 606e80583aSopenharmony_ci * 616e80583aSopenharmony_ci * @param {string} key - key of the cache map 626e80583aSopenharmony_ci * @param {object} value - value of the cache map 636e80583aSopenharmony_ci */ 646e80583aSopenharmony_ci putCache(key: string, value: object | string): void { 656e80583aSopenharmony_ci if (this.cache.has(key)) { 666e80583aSopenharmony_ci // exist and update 676e80583aSopenharmony_ci this.cache.delete(key); 686e80583aSopenharmony_ci } else if (this.cache.size >= this.capacity) { 696e80583aSopenharmony_ci // if size > capacity ,remove the old 706e80583aSopenharmony_ci this.remove(this.cache.keys().next().value); 716e80583aSopenharmony_ci } 726e80583aSopenharmony_ci //update the cache to recent use 736e80583aSopenharmony_ci this.cache.set(key, value); 746e80583aSopenharmony_ci } 756e80583aSopenharmony_ci 766e80583aSopenharmony_ci /** 776e80583aSopenharmony_ci * Remove cache of corresponding key. 786e80583aSopenharmony_ci * 796e80583aSopenharmony_ci * @param {string} key - key of the cache map 806e80583aSopenharmony_ci */ 816e80583aSopenharmony_ci remove(key: string): void { 826e80583aSopenharmony_ci this.cache.delete(key); 836e80583aSopenharmony_ci } 846e80583aSopenharmony_ci 856e80583aSopenharmony_ci /** 866e80583aSopenharmony_ci * Clear cache of disk. 876e80583aSopenharmony_ci */ 886e80583aSopenharmony_ci clear(): void { 896e80583aSopenharmony_ci this.cache.clear(); 906e80583aSopenharmony_ci } 916e80583aSopenharmony_ci}