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 LruCache from './LruCache';
17import { Log } from '../utils/Log';
18import { CommonConstants } from '../constants/CommonConstants';
19
20const TAG = 'AppListInfoCacheManager';
21
22/**
23 * A Manager class that provides get/set/clear cache methods for app list data.
24 */
25export default class AppListInfoCacheManager {
26  private readonly lruCache;
27
28  constructor() {
29    this.lruCache = new LruCache();
30  }
31
32  /**
33   * Get cache from disk or memory.
34   *
35   * @param {string} key - key of the cache map
36   * @return {object} - cache get from the memory or disk
37   */
38  getCache(key: string): any {
39    Log.showDebug(TAG, `getCache key: ${key}`);
40    const cache = this.lruCache.getCache(key);
41    if (cache == undefined || cache == null || cache == '' || cache == -1) {
42      return CommonConstants.INVALID_VALUE;
43    } else {
44      return cache;
45    }
46  }
47
48  /**
49   * Set cache to disk or memory.
50   *
51   * @param {string} key - key of the cache map
52   * @param {object} value - value of the cache map
53   */
54  setCache(key, value): void {
55    Log.showDebug(TAG, `setCache key: ${key}, value: ${value}`);
56    this.lruCache.putCache(key, value);
57  }
58
59  /**
60   * Clear cache of both disk and memory.
61   */
62  clearCache(): void {
63    Log.showDebug(TAG, 'clearCache');
64    this.lruCache.clear();
65  }
66}