memory.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { TOKEN_KEY, ROLES_KEY, USER_INFO_KEY, DB_DICT_DATA_KEY, TENANT_ID, LOGIN_INFO_KEY, PROJ_CFG_KEY } from '/@/enums/cacheEnum';
  2. import { omit } from 'lodash-es';
  3. export interface Cache<V = any> {
  4. value?: V;
  5. timeoutId?: ReturnType<typeof setTimeout>;
  6. time?: number;
  7. alive?: number;
  8. }
  9. const NOT_ALIVE = 0;
  10. export class Memory<T = any, V = any> {
  11. private cache: { [key in keyof T]?: Cache<V> } = {};
  12. private alive: number;
  13. constructor(alive = NOT_ALIVE) {
  14. // Unit second
  15. this.alive = alive * 1000;
  16. }
  17. get getCache() {
  18. return this.cache;
  19. }
  20. setCache(cache) {
  21. this.cache = cache;
  22. }
  23. // get<K extends keyof T>(key: K) {
  24. // const item = this.getItem(key);
  25. // const time = item?.time;
  26. // if (!isNullOrUnDef(time) && time < new Date().getTime()) {
  27. // this.remove(key);
  28. // }
  29. // return item?.value ?? undefined;
  30. // }
  31. get<K extends keyof T>(key: K) {
  32. return this.cache[key];
  33. }
  34. set<K extends keyof T>(key: K, value: V, expires?: number) {
  35. let item = this.get(key);
  36. if (!expires || (expires as number) <= 0) {
  37. expires = this.alive;
  38. }
  39. if (item) {
  40. if (item.timeoutId) {
  41. clearTimeout(item.timeoutId);
  42. item.timeoutId = undefined;
  43. }
  44. item.value = value;
  45. } else {
  46. item = { value, alive: expires };
  47. this.cache[key] = item;
  48. }
  49. if (!expires) {
  50. return value;
  51. }
  52. const now = new Date().getTime();
  53. item.time = now + this.alive;
  54. item.timeoutId = setTimeout(
  55. () => {
  56. this.remove(key);
  57. },
  58. expires > now ? expires - now : expires
  59. );
  60. return value;
  61. }
  62. remove<K extends keyof T>(key: K) {
  63. const item = this.get(key);
  64. Reflect.deleteProperty(this.cache, key);
  65. if (item) {
  66. clearTimeout(item.timeoutId!);
  67. return item.value;
  68. }
  69. }
  70. resetCache(cache: { [K in keyof T]: Cache }) {
  71. Object.keys(cache).forEach((key) => {
  72. const k = key as any as keyof T;
  73. const item = cache[k];
  74. if (item && item.time) {
  75. const now = new Date().getTime();
  76. const expire = item.time;
  77. if (expire > now) {
  78. this.set(k, item.value, expire);
  79. }
  80. }
  81. });
  82. }
  83. clear() {
  84. console.log('------clear------进入clear方法');
  85. Object.keys(this.cache).forEach((key) => {
  86. const item = this.cache[key];
  87. item.timeoutId && clearTimeout(item.timeoutId);
  88. });
  89. //update-begin---author:liusq Date:20220108 for:不删除登录用户的租户id,其他缓存信息都清除----
  90. this.cache = {
  91. ...omit(this.cache, [TOKEN_KEY, USER_INFO_KEY, ROLES_KEY, DB_DICT_DATA_KEY, TENANT_ID, LOGIN_INFO_KEY, PROJ_CFG_KEY]),
  92. };
  93. //update-end---author:liusq Date:20220108 for:不删除登录用户的租户id,其他缓存信息都清除----
  94. }
  95. }