cacheManager.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const cacheManager = (function() {
  2. const STORAGE_PREFIX = 'App_cache_';
  3. function set(key, value) {
  4. const fullKey = STORAGE_PREFIX + key;
  5. if (typeof value === 'object' && value !== null) {
  6. // 如果是对象,则将其转换为字符串存储
  7. uni.setStorageSync(fullKey, JSON.stringify(value));
  8. } else {
  9. uni.setStorageSync(fullKey, value);
  10. }
  11. }
  12. // 获取缓存
  13. function get(key) {
  14. const fullKey = STORAGE_PREFIX + key;
  15. const value = uni.getStorageSync(fullKey);
  16. try {
  17. return JSON.parse(value);
  18. } catch (e) {
  19. return value;
  20. }
  21. }
  22. // 删除缓存
  23. function remove(key) {
  24. const fullKey = STORAGE_PREFIX + key;
  25. uni.removeStorageSync(fullKey);
  26. }
  27. //例子: cacheManager.updateObject('user', { www: undefined }); //删除
  28. function updateObject(key, updates) {
  29. let obj = get(key) || {};
  30. // 合并更新到对象中
  31. //Object.assign(obj, updates);
  32. for (let [keyToUpdate, value] of Object.entries(updates)) {
  33. if (value === null || value === undefined) {
  34. // 如果值为 null 或 undefined,则删除属性
  35. delete obj[keyToUpdate];
  36. } else {
  37. obj[keyToUpdate] = value;
  38. }
  39. }
  40. set(key, obj);
  41. }
  42. function clearAll() {
  43. const keys = uni.getStorageInfoSync().keys;
  44. keys.forEach(key => {
  45. if (key.startsWith(STORAGE_PREFIX)) {
  46. uni.removeStorageSync(key);
  47. }
  48. });
  49. }
  50. // 增量修改对象中的数组,支持添加、更新和删除元素
  51. function updateArrayInObject(key, arrayPath, updateFn) {
  52. let obj = get(key) || {};
  53. // 根据arrayPath找到需要修改的数组
  54. const targetArray = findArrayInObject(obj, arrayPath);
  55. if (Array.isArray(targetArray)) {
  56. // 应用更新函数到目标数组上
  57. updateFn(targetArray);
  58. // 重新设置缓存
  59. set(key, obj);
  60. } else {
  61. console.error(`${arrayPath}不指向对象中的数组。`);
  62. }
  63. }
  64. // 例子
  65. // cacheManager.updateArrayInObject('user', 'hobbies', (hobbies) => {
  66. // hobbies.push('reading');
  67. // hobbies = 'coding';
  68. // hobbies.pop();
  69. // });
  70. // 在对象中根据路径找到数组
  71. function findArrayInObject(obj, path) {
  72. return path.split('.').reduce((acc, curr) => {
  73. if (acc && acc[curr] !== undefined) {
  74. return acc[curr];
  75. } else {
  76. return undefined;
  77. }
  78. }, obj);
  79. }
  80. return {
  81. set,
  82. get,
  83. remove,
  84. updateObject,
  85. findArrayInObject,
  86. clearAll
  87. };
  88. })();
  89. export default cacheManager;
  90. // 单元测试 引导大鹅提示 缓存Key
  91. export const SHOW_UNIT_TEST_TISHI = 'SHOW_UNIT_TEST_TISHI';
  92. // 年级学科 缓存Key
  93. export const NIANJI_XUEKE = 'SHOW_UNIT_XUEKE';
  94. // 学科年级业务缓存
  95. export function useXuekeNianji() {
  96. // 学科
  97. function updateXueke(nianji, cardId) {
  98. cacheManager.set(NIANJI_XUEKE, { nianji,cardId })
  99. }
  100. // 获取学科年级
  101. function getXueke() {
  102. return cacheManager.get(NIANJI_XUEKE)
  103. }
  104. return {
  105. updateXueke,
  106. getXueke
  107. }
  108. }
  109. // 单元测试大鹅提示缓存
  110. export function useUnitTestTishi() {
  111. return {
  112. updateTishi:() => cacheManager.set(SHOW_UNIT_TEST_TISHI,'has'),
  113. getTishi: () => cacheManager.get(SHOW_UNIT_TEST_TISHI)
  114. }
  115. }