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