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_ciimport { Log } from '../utils/Log'; 176e80583aSopenharmony_ciimport LruCache from './LruCache'; 186e80583aSopenharmony_ciimport DiskLruCache from './DiskLruCache'; 196e80583aSopenharmony_ci 206e80583aSopenharmony_ciconst TAG = 'AppResourceCacheManager'; 216e80583aSopenharmony_ci 226e80583aSopenharmony_ci/** 236e80583aSopenharmony_ci * A Manager class that provides get/set/clear cache methods for app image data. 246e80583aSopenharmony_ci */ 256e80583aSopenharmony_ciexport default class AppResourceCacheManager { 266e80583aSopenharmony_ci private readonly memoryCache; 276e80583aSopenharmony_ci private readonly diskCache; 286e80583aSopenharmony_ci private KEY_ICON: string = 'icon'; 296e80583aSopenharmony_ci private DISK_CACHE_MISS: number = -1; 306e80583aSopenharmony_ci 316e80583aSopenharmony_ci constructor() { 326e80583aSopenharmony_ci this.memoryCache = new LruCache(); 336e80583aSopenharmony_ci this.diskCache = new DiskLruCache(); 346e80583aSopenharmony_ci } 356e80583aSopenharmony_ci 366e80583aSopenharmony_ci /** 376e80583aSopenharmony_ci * Get cache from disk or memory. 386e80583aSopenharmony_ci * 396e80583aSopenharmony_ci * @param {string} cacheKey - cacheKey of the cache map 406e80583aSopenharmony_ci * @return {object} - cache get from memory or disk 416e80583aSopenharmony_ci */ 426e80583aSopenharmony_ci getCache(cacheKey: string, cacheType: string) { 436e80583aSopenharmony_ci const cache = this.getCacheFromMemory(cacheKey, cacheType); 446e80583aSopenharmony_ci if (cache == undefined || cache == null || cache == '') { 456e80583aSopenharmony_ci if (cacheType === this.KEY_ICON) { 466e80583aSopenharmony_ci const cacheFromDisk = this.getCacheFromDisk(cacheKey, cacheType); 476e80583aSopenharmony_ci this.setCacheToMemory(cacheKey, cacheType, cacheFromDisk); 486e80583aSopenharmony_ci return cacheFromDisk; 496e80583aSopenharmony_ci } 506e80583aSopenharmony_ci return null; 516e80583aSopenharmony_ci } else { 526e80583aSopenharmony_ci return cache; 536e80583aSopenharmony_ci } 546e80583aSopenharmony_ci } 556e80583aSopenharmony_ci 566e80583aSopenharmony_ci /** 576e80583aSopenharmony_ci * Set cache to disk or memory. 586e80583aSopenharmony_ci * 596e80583aSopenharmony_ci * @param {string} cacheKey - cacheKey of the cache map 606e80583aSopenharmony_ci * @param {object} value - value of the cache map 616e80583aSopenharmony_ci */ 626e80583aSopenharmony_ci setCache(cacheKey: string, cacheType: string, value: object | string) { 636e80583aSopenharmony_ci Log.showDebug(TAG, `setCache cacheKey: ${cacheKey}, cacheType: ${cacheType}`); 646e80583aSopenharmony_ci this.setCacheToMemory(cacheKey, cacheType, value); 656e80583aSopenharmony_ci if (cacheType === this.KEY_ICON) { 666e80583aSopenharmony_ci this.setCacheToDisk(cacheKey, cacheType, value); 676e80583aSopenharmony_ci } 686e80583aSopenharmony_ci } 696e80583aSopenharmony_ci 706e80583aSopenharmony_ci /** 716e80583aSopenharmony_ci * Clear cache of both disk and memory. 726e80583aSopenharmony_ci */ 736e80583aSopenharmony_ci clearCache(): void { 746e80583aSopenharmony_ci Log.showDebug(TAG, 'clearCache'); 756e80583aSopenharmony_ci this.memoryCache.clear(); 766e80583aSopenharmony_ci } 776e80583aSopenharmony_ci 786e80583aSopenharmony_ci deleteCache(cacheKey: string, cacheType: string): void { 796e80583aSopenharmony_ci this.memoryCache.remove(cacheKey); 806e80583aSopenharmony_ci if (cacheType === this.KEY_ICON) { 816e80583aSopenharmony_ci this.diskCache.remove(cacheKey); 826e80583aSopenharmony_ci } 836e80583aSopenharmony_ci } 846e80583aSopenharmony_ci 856e80583aSopenharmony_ci private getCacheFromMemory(cacheKey: string, cacheType: string) { 866e80583aSopenharmony_ci const cache = this.memoryCache.getCache(cacheKey); 876e80583aSopenharmony_ci if (cache == undefined || cache == null || cache == '' || cache === -1) { 886e80583aSopenharmony_ci return null; 896e80583aSopenharmony_ci } else if (cache[cacheType] == undefined || cache[cacheType] == null || cache[cacheType] == '') { 906e80583aSopenharmony_ci return null; 916e80583aSopenharmony_ci } else { 926e80583aSopenharmony_ci return cache[cacheType]; 936e80583aSopenharmony_ci } 946e80583aSopenharmony_ci } 956e80583aSopenharmony_ci 966e80583aSopenharmony_ci private setCacheToMemory(cacheKey: string, cacheType: string, value: object | string): void { 976e80583aSopenharmony_ci let cache = this.memoryCache.getCache(cacheKey); 986e80583aSopenharmony_ci if (cache == undefined || cache == null || cache == '' || cache === -1) { 996e80583aSopenharmony_ci cache = {}; 1006e80583aSopenharmony_ci cache[cacheType] = value; 1016e80583aSopenharmony_ci } else { 1026e80583aSopenharmony_ci cache[cacheType] = value; 1036e80583aSopenharmony_ci } 1046e80583aSopenharmony_ci this.memoryCache.putCache(cacheKey, cache); 1056e80583aSopenharmony_ci } 1066e80583aSopenharmony_ci 1076e80583aSopenharmony_ci private getCacheFromDisk(cacheKey: string, cacheType: string) { 1086e80583aSopenharmony_ci const data = this.diskCache.getCache(cacheKey); 1096e80583aSopenharmony_ci return data !== this.DISK_CACHE_MISS ? data : null; 1106e80583aSopenharmony_ci } 1116e80583aSopenharmony_ci 1126e80583aSopenharmony_ci private setCacheToDisk(cacheKey: string, cacheType: string, value: object | string): void { 1136e80583aSopenharmony_ci this.diskCache.putCache(cacheKey, value); 1146e80583aSopenharmony_ci } 1156e80583aSopenharmony_ci}