DEV Community

桃花镇童长老
桃花镇童长老

Posted on

LRUCacheUtil, LRUCache cache tool class

Introduction and description of harmony-utils


harmony-utils A HarmonyOS tool library with rich features and extremely easy to use, with the help of many practical tools, is committed to helping developers quickly build Hongmeng applications. Its encapsulated tools cover APP, device, screen, authorization, notification, inter-thread communication, pop-up frames, toast, biometric authentication, user preferences, taking photos, albums, scanning codes, files, logs, exception capture, characters, strings, numbers, collections, dates, random, base64, encryption, decryption, JSON and other functions, which can meet various development needs.

picker_utils It is a sub-store split by harmony-utils, including PickerUtil, PhotoHelper, and ScanUtil.

Download and install

ohpm i @pura/harmony-utils

ohpm i @pura/picker_utils

  //Global initialization method, initialized in the onCreate method of UIAbility AppUtil.init()
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    AppUtil.init(this.context);
  }
Enter fullscreen mode Exit fullscreen mode

API methods and usage


getInstance Gets a single case of LRUCacheUtil
 private lruCache: LRUCacheUtil = LRUCacheUtil.getInstance();
Enter fullscreen mode Exit fullscreen mode
has to determine whether the cache corresponding to the key is included
let pwd = this.lruCache.has("pwd");
ToastUtil.showToast(`缓存是否存在:${pwd}`);
Enter fullscreen mode Exit fullscreen mode
put add cache to lruCache
 this.lruCache.put("pwd", "abcd@12345");
 ToastUtil.showToast("缓存密码成功");
Enter fullscreen mode Exit fullscreen mode
get get the cache corresponding to the key
let pwd = this.lruCache.get<string>("pwd");
ToastUtil.showToast(`取值:${pwd}`);
Enter fullscreen mode Exit fullscreen mode
remove delete the cache corresponding to the key
this.lruCache.remove("pwd");
ToastUtil.showToast(`删除成功!`);
Enter fullscreen mode Exit fullscreen mode
isEmpty determines whether the lruCache cache is empty
let blEmpty = this.lruCache.isEmpty();
ToastUtil.showToast(`缓存是否为空:${blEmpty}`);
Enter fullscreen mode Exit fullscreen mode
getCapacity Gets the capacity of the current buffer
 let count = this.lruCache.getCapacity();
 ToastUtil.showToast(`当前缓冲区的容量:${count}`);
Enter fullscreen mode Exit fullscreen mode
updateCapacity Reset the capacity of lruCache
this.lruCache.updateCapacity(128);
ToastUtil.showToast(`重新设置lruCache的容量成功`);
Enter fullscreen mode Exit fullscreen mode
clear clear cache data and reset lruCache size
this.lruCache.clear();
ToastUtil.showToast(`清除缓存数据成功`);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)