1/** 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { Log } from '../utils/Log'; 17import LruCache from './LruCache'; 18import DiskLruCache from './DiskLruCache'; 19 20const TAG = 'AppResourceCacheManager'; 21 22/** 23 * A Manager class that provides get/set/clear cache methods for app image data. 24 */ 25export default class AppResourceCacheManager { 26 private readonly memoryCache; 27 private readonly diskCache; 28 private KEY_ICON: string = 'icon'; 29 private DISK_CACHE_MISS: number = -1; 30 31 constructor() { 32 this.memoryCache = new LruCache(); 33 this.diskCache = new DiskLruCache(); 34 } 35 36 /** 37 * Get cache from disk or memory. 38 * 39 * @param {string} cacheKey - cacheKey of the cache map 40 * @return {object} - cache get from memory or disk 41 */ 42 getCache(cacheKey: string, cacheType: string) { 43 const cache = this.getCacheFromMemory(cacheKey, cacheType); 44 if (cache == undefined || cache == null || cache == '') { 45 if (cacheType === this.KEY_ICON) { 46 const cacheFromDisk = this.getCacheFromDisk(cacheKey, cacheType); 47 this.setCacheToMemory(cacheKey, cacheType, cacheFromDisk); 48 return cacheFromDisk; 49 } 50 return null; 51 } else { 52 return cache; 53 } 54 } 55 56 /** 57 * Set cache to disk or memory. 58 * 59 * @param {string} cacheKey - cacheKey of the cache map 60 * @param {object} value - value of the cache map 61 */ 62 setCache(cacheKey: string, cacheType: string, value: object | string) { 63 Log.showDebug(TAG, `setCache cacheKey: ${cacheKey}, cacheType: ${cacheType}`); 64 this.setCacheToMemory(cacheKey, cacheType, value); 65 if (cacheType === this.KEY_ICON) { 66 this.setCacheToDisk(cacheKey, cacheType, value); 67 } 68 } 69 70 /** 71 * Clear cache of both disk and memory. 72 */ 73 clearCache(): void { 74 Log.showDebug(TAG, 'clearCache'); 75 this.memoryCache.clear(); 76 } 77 78 deleteCache(cacheKey: string, cacheType: string): void { 79 this.memoryCache.remove(cacheKey); 80 if (cacheType === this.KEY_ICON) { 81 this.diskCache.remove(cacheKey); 82 } 83 } 84 85 private getCacheFromMemory(cacheKey: string, cacheType: string) { 86 const cache = this.memoryCache.getCache(cacheKey); 87 if (cache == undefined || cache == null || cache == '' || cache === -1) { 88 return null; 89 } else if (cache[cacheType] == undefined || cache[cacheType] == null || cache[cacheType] == '') { 90 return null; 91 } else { 92 return cache[cacheType]; 93 } 94 } 95 96 private setCacheToMemory(cacheKey: string, cacheType: string, value: object | string): void { 97 let cache = this.memoryCache.getCache(cacheKey); 98 if (cache == undefined || cache == null || cache == '' || cache === -1) { 99 cache = {}; 100 cache[cacheType] = value; 101 } else { 102 cache[cacheType] = value; 103 } 104 this.memoryCache.putCache(cacheKey, cache); 105 } 106 107 private getCacheFromDisk(cacheKey: string, cacheType: string) { 108 const data = this.diskCache.getCache(cacheKey); 109 return data !== this.DISK_CACHE_MISS ? data : null; 110 } 111 112 private setCacheToDisk(cacheKey: string, cacheType: string, value: object | string): void { 113 this.diskCache.putCache(cacheKey, value); 114 } 115}