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_ci * A class provides memory cache operation.
186e80583aSopenharmony_ci */
196e80583aSopenharmony_ciexport default class LruCache {
206e80583aSopenharmony_ci  private readonly cache: Map<string, object | string>;
216e80583aSopenharmony_ci  private readonly capacity: number;
226e80583aSopenharmony_ci
236e80583aSopenharmony_ci  constructor(capacity = 100) {
246e80583aSopenharmony_ci    this.cache = new Map();
256e80583aSopenharmony_ci    this.capacity = capacity; //the capacity of cache
266e80583aSopenharmony_ci  }
276e80583aSopenharmony_ci
286e80583aSopenharmony_ci  /**
296e80583aSopenharmony_ci   * Get cache from memory.
306e80583aSopenharmony_ci   *
316e80583aSopenharmony_ci   * @param {string} key - key of the cache map
326e80583aSopenharmony_ci   * @return {any} - cache from memory
336e80583aSopenharmony_ci   */
346e80583aSopenharmony_ci  getCache(key: string): any {
356e80583aSopenharmony_ci    if (this.cache.has(key)) {
366e80583aSopenharmony_ci      // exist and update
376e80583aSopenharmony_ci      const temp = this.cache.get(key);
386e80583aSopenharmony_ci      //delete the old cache
396e80583aSopenharmony_ci      this.cache.delete(key);
406e80583aSopenharmony_ci      //update the cache to recent use
416e80583aSopenharmony_ci      this.cache.set(key, temp);
426e80583aSopenharmony_ci      return temp;
436e80583aSopenharmony_ci    }
446e80583aSopenharmony_ci    return -1;
456e80583aSopenharmony_ci  }
466e80583aSopenharmony_ci
476e80583aSopenharmony_ci  /**
486e80583aSopenharmony_ci   * Put cache to disk.
496e80583aSopenharmony_ci   *
506e80583aSopenharmony_ci   * @param {string} key - key of the cache map
516e80583aSopenharmony_ci   * @param {any} value - value of the cache map
526e80583aSopenharmony_ci   */
536e80583aSopenharmony_ci  putCache(key: string, value: any): void {
546e80583aSopenharmony_ci    if (this.cache.has(key)) {
556e80583aSopenharmony_ci      // exist and update
566e80583aSopenharmony_ci      this.cache.delete(key);
576e80583aSopenharmony_ci    } else if (this.cache.size >= this.capacity) {
586e80583aSopenharmony_ci      // if size > capacity ,remove the old
596e80583aSopenharmony_ci      this.cache.delete(this.cache.keys().next().value);
606e80583aSopenharmony_ci    }
616e80583aSopenharmony_ci    //update the cache to recent use
626e80583aSopenharmony_ci    this.cache.set(key, value);
636e80583aSopenharmony_ci  }
646e80583aSopenharmony_ci
656e80583aSopenharmony_ci  /**
666e80583aSopenharmony_ci   * Remove cache of corresponding key.
676e80583aSopenharmony_ci   *
686e80583aSopenharmony_ci   * @param {string} key - key of the cache map
696e80583aSopenharmony_ci   */
706e80583aSopenharmony_ci  remove(key: string): void {
716e80583aSopenharmony_ci    this.cache.delete(key);
726e80583aSopenharmony_ci  }
736e80583aSopenharmony_ci
746e80583aSopenharmony_ci  /**
756e80583aSopenharmony_ci   * Clear cache of memory.
766e80583aSopenharmony_ci   */
776e80583aSopenharmony_ci  clear(): void {
786e80583aSopenharmony_ci    this.cache.clear();
796e80583aSopenharmony_ci  }
806e80583aSopenharmony_ci}