AppStorageHelper.ets 702 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /**
  2. * 永久化存储,存储在本地文件
  3. */
  4. export class AppStorageHelper {
  5. private constructor() {
  6. }
  7. /**
  8. * 缓存
  9. * @param key
  10. * @param value
  11. */
  12. public static save<T>(key: string, value: T | undefined) {
  13. if (value === undefined || value === null) {
  14. PersistentStorage.deleteProp(key)
  15. } else {
  16. PersistentStorage.persistProp(key, value)
  17. }
  18. }
  19. /**
  20. * 删除缓存
  21. * @param key
  22. * @param value
  23. */
  24. public static delete(key: string) {
  25. PersistentStorage.deleteProp(key)
  26. }
  27. /**
  28. * 获取已缓存的内容
  29. * @param key
  30. * @returns
  31. */
  32. public static get<T>(key: string): T | undefined {
  33. return AppStorage.get<T>(key)
  34. }
  35. }