limited-cache 2.2.0 → 2.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +4 -3
  3. package/dist/cjs/index.cjs +273 -380
  4. package/dist/cjs/index.d.cts +84 -76
  5. package/dist/cjs/index.d.cts.map +1 -0
  6. package/dist/esm/index.d.ts +99 -6
  7. package/dist/esm/index.d.ts.map +1 -1
  8. package/dist/esm/index.js +279 -6
  9. package/dist/esm/index.js.map +1 -1
  10. package/package.json +34 -53
  11. package/src/__tests__/LimitedCache.test.ts +2 -2
  12. package/src/__tests__/LimitedCacheObject.test.ts +30 -27
  13. package/src/__tests__/lowLevelFunctions.test.ts +10 -8
  14. package/src/__tests__/scenarios/keys.test.ts +2 -2
  15. package/src/__tests__/scenarios/maxCacheSize.test.ts +8 -5
  16. package/src/__tests__/scenarios/maxCacheTime.test.ts +58 -58
  17. package/src/__tests__/scenarios/values.test.ts +2 -2
  18. package/src/__tests__/typeChecks/LimitedCacheObjectTypes.test.ts +15 -15
  19. package/src/__tests__/typeChecks/LimitedCacheTypes.test.ts +1 -1
  20. package/src/__tests__/typeChecks/lowLevelFunctionsTypes.test.ts +2 -2
  21. package/src/core/LimitedCache.ts +20 -21
  22. package/src/core/LimitedCacheObject.ts +30 -19
  23. package/src/core/defaultOptions.ts +5 -4
  24. package/src/core/limitedCacheUtil.ts +15 -3
  25. package/src/core/lowLevelFunctions.ts +43 -32
  26. package/src/types.ts +4 -3
  27. package/dist/esm/core/LimitedCache.d.ts +0 -4
  28. package/dist/esm/core/LimitedCache.d.ts.map +0 -1
  29. package/dist/esm/core/LimitedCache.js +0 -29
  30. package/dist/esm/core/LimitedCache.js.map +0 -1
  31. package/dist/esm/core/LimitedCacheObject.d.ts +0 -5
  32. package/dist/esm/core/LimitedCacheObject.d.ts.map +0 -1
  33. package/dist/esm/core/LimitedCacheObject.js +0 -52
  34. package/dist/esm/core/LimitedCacheObject.js.map +0 -1
  35. package/dist/esm/core/builtIns.d.ts +0 -12
  36. package/dist/esm/core/builtIns.d.ts.map +0 -1
  37. package/dist/esm/core/builtIns.js +0 -5
  38. package/dist/esm/core/builtIns.js.map +0 -1
  39. package/dist/esm/core/defaultOptions.d.ts +0 -6
  40. package/dist/esm/core/defaultOptions.d.ts.map +0 -1
  41. package/dist/esm/core/defaultOptions.js +0 -14
  42. package/dist/esm/core/defaultOptions.js.map +0 -1
  43. package/dist/esm/core/limitedCacheUtil.d.ts +0 -13
  44. package/dist/esm/core/limitedCacheUtil.d.ts.map +0 -1
  45. package/dist/esm/core/limitedCacheUtil.js +0 -14
  46. package/dist/esm/core/limitedCacheUtil.js.map +0 -1
  47. package/dist/esm/core/lowLevelFunctions.d.ts +0 -14
  48. package/dist/esm/core/lowLevelFunctions.d.ts.map +0 -1
  49. package/dist/esm/core/lowLevelFunctions.js +0 -240
  50. package/dist/esm/core/lowLevelFunctions.js.map +0 -1
  51. package/dist/esm/types.d.ts +0 -58
  52. package/dist/esm/types.d.ts.map +0 -1
  53. package/dist/esm/types.js +0 -2
  54. package/dist/esm/types.js.map +0 -1
  55. package/legacy-types/ts3.5/dist/esm/core/LimitedCache.d.ts +0 -4
  56. package/legacy-types/ts3.5/dist/esm/core/LimitedCacheObject.d.ts +0 -5
  57. package/legacy-types/ts3.5/dist/esm/core/builtIns.d.ts +0 -12
  58. package/legacy-types/ts3.5/dist/esm/core/defaultOptions.d.ts +0 -6
  59. package/legacy-types/ts3.5/dist/esm/core/limitedCacheUtil.d.ts +0 -13
  60. package/legacy-types/ts3.5/dist/esm/core/lowLevelFunctions.d.ts +0 -14
  61. package/legacy-types/ts3.5/dist/esm/index.d.ts +0 -7
  62. package/legacy-types/ts3.5/dist/esm/types.d.ts +0 -61
  63. package/legacy-types/ts4.0/dist/esm/core/LimitedCache.d.ts +0 -4
  64. package/legacy-types/ts4.0/dist/esm/core/LimitedCacheObject.d.ts +0 -5
  65. package/legacy-types/ts4.0/dist/esm/core/builtIns.d.ts +0 -12
  66. package/legacy-types/ts4.0/dist/esm/core/defaultOptions.d.ts +0 -6
  67. package/legacy-types/ts4.0/dist/esm/core/limitedCacheUtil.d.ts +0 -13
  68. package/legacy-types/ts4.0/dist/esm/core/lowLevelFunctions.d.ts +0 -14
  69. package/legacy-types/ts4.0/dist/esm/index.d.ts +0 -7
  70. package/legacy-types/ts4.0/dist/esm/types.d.ts +0 -61
  71. package/legacy-types/ts4.5/dist/esm/core/LimitedCache.d.ts +0 -4
  72. package/legacy-types/ts4.5/dist/esm/core/LimitedCacheObject.d.ts +0 -5
  73. package/legacy-types/ts4.5/dist/esm/core/builtIns.d.ts +0 -12
  74. package/legacy-types/ts4.5/dist/esm/core/defaultOptions.d.ts +0 -6
  75. package/legacy-types/ts4.5/dist/esm/core/limitedCacheUtil.d.ts +0 -13
  76. package/legacy-types/ts4.5/dist/esm/core/lowLevelFunctions.d.ts +0 -14
  77. package/legacy-types/ts4.5/dist/esm/index.d.ts +0 -7
  78. package/legacy-types/ts4.5/dist/esm/types.d.ts +0 -61
  79. package/src/core/builtIns.ts +0 -9
@@ -1,92 +1,100 @@
1
+ //#region src/types.d.ts
1
2
  type DefaultItemType = any;
2
3
  interface LimitedCacheOptionsFull {
3
- /** Items will be removed to keep the cache within the maxCacheSize limit */
4
- maxCacheSize: number;
5
- /** Items will be removed and never returned if they were set more than maxCacheTime milliseconds ago */
6
- maxCacheTime: number;
7
- /** (dev only) A warning will be emitted if an item rotates out of the cache before this many milliseconds have passed, to indicate the size is too small */
8
- warnIfItemPurgedBeforeTime: number;
9
- /** (private) Internal cleanup of old keys will be performed after this many operations */
10
- opLimit: number;
11
- /** (private) Internal optimization to adjust how much searching will be done to find expired items, to avoid being O(n) */
12
- scanLimit: number;
4
+ /** Items will be removed to keep the cache within the maxCacheSize limit */
5
+ maxCacheSize: number;
6
+ /** Items will be removed and never returned if they were set more than maxCacheTime milliseconds ago */
7
+ maxCacheTime: number;
8
+ /** (dev only) A warning will be emitted if an item rotates out of the cache before this many milliseconds have passed, to indicate the size is too small */
9
+ warnIfItemPurgedBeforeTime: number;
10
+ /** (private) Internal cleanup of old keys will be performed after this many operations */
11
+ opLimit: number;
12
+ /** (private) Internal optimization to adjust how much searching will be done to find expired items, to avoid being O(n) */
13
+ scanLimit: number;
13
14
  }
14
15
  type LimitedCacheOptions = Partial<LimitedCacheOptionsFull> | null;
15
16
  type LimitedCacheOptionsReadonly = Readonly<LimitedCacheOptionsFull>;
16
17
  interface LimitedCacheInstance<ItemType = DefaultItemType> {
17
- /** Return the requested item, if it has not expired */
18
- get: (cacheKey: string) => ItemType | undefined;
19
- /** Return all non-expired items */
20
- getAll: () => Record<string, ItemType>;
21
- /** Indicate whether or not the requested item is present and has not expired */
22
- has: (cacheKey: string) => boolean;
23
- /** Add the item to the cache, or update its timestamp if it already exists */
24
- set: (cacheKey: string, item: ItemType) => ItemType;
25
- /** Remove the requested item from the cache, if necessary */
26
- remove: (cacheKey: string) => true;
27
- /** Remove all items and all timestamps from the cache */
28
- reset: () => LimitedCacheMeta<ItemType>;
29
- /** Return a serializable representation of the cache internals, suitable for long-term storage */
30
- getCacheMeta: () => LimitedCacheMeta<ItemType>;
31
- /** Return the cache's current values for all options */
32
- getOptions: () => LimitedCacheOptionsFull;
33
- /** Update one or more of the cache's options */
34
- setOptions: (newOptions: LimitedCacheOptions) => LimitedCacheOptionsReadonly;
35
- /** Reduces cache size by cleaning up old keys and expired items */
36
- doMaintenance: () => LimitedCacheMeta<ItemType>;
18
+ /** Return the requested item, if it has not expired */
19
+ get: (cacheKey: string) => ItemType | undefined;
20
+ /** Return all non-expired items */
21
+ getAll: () => Record<string, ItemType>;
22
+ /** Indicate whether or not the requested item is present and has not expired */
23
+ has: (cacheKey: string) => boolean;
24
+ /** Add the item to the cache, or update its timestamp if it already exists */
25
+ set: (cacheKey: string, item: ItemType) => ItemType;
26
+ /** Remove the requested item from the cache, if necessary */
27
+ remove: (cacheKey: string) => true;
28
+ /** Remove all items and all timestamps from the cache */
29
+ reset: () => LimitedCacheMeta<ItemType>;
30
+ /** Return a serializable representation of the cache internals, suitable for long-term storage */
31
+ getCacheMeta: () => LimitedCacheMeta<ItemType>;
32
+ /** Return the cache's current values for all options */
33
+ getOptions: () => LimitedCacheOptionsFull;
34
+ /** Update one or more of the cache's options */
35
+ setOptions: (newOptions: LimitedCacheOptions) => LimitedCacheOptionsReadonly;
36
+ /** Reduces cache size by cleaning up old keys and expired items */
37
+ doMaintenance: () => LimitedCacheMeta<ItemType>;
37
38
  }
38
39
  interface LimitedCacheObjectInstance<ItemType = DefaultItemType> {
39
- [key: string]: ItemType;
40
+ [key: string]: ItemType;
40
41
  }
41
42
  /**
42
- * A serializable representation of the cache internals, suitable for long-term storage
43
- */
43
+ * A serializable representation of the cache internals, suitable for long-term storage
44
+ */
44
45
  interface LimitedCacheMeta<ItemType = DefaultItemType> {
45
- /** Schema version: old versions will be upgraded if possible, or a warning will be emitted if not */
46
- limitedCacheMetaVersion: number;
47
- /** Options to control cache size, time, and behavior */
48
- options: LimitedCacheOptionsReadonly;
49
- /** The values in the cache, stored by key. Will include old keys not yet garbage collected */
50
- cache: Record<string, ItemType | undefined>;
51
- /** List of keys that have been set, in chronological order. Used to find cache items most likely to be expired */
52
- keyList: Array<string>;
53
- /** The [setTime, expirationTime] for each key that has been set. Removed on unset. */
54
- keyInfo: Record<string, [number, number] | undefined>;
55
- /** Number of operations remaining until internal cleanup of old keys is performed. Based on options.opLimit */
56
- opsLeft: number;
46
+ /** Schema version: old versions will be upgraded if possible, or a warning will be emitted if not */
47
+ limitedCacheMetaVersion: number;
48
+ /** Options to control cache size, time, and behavior */
49
+ options: LimitedCacheOptionsReadonly;
50
+ /** The values in the cache, stored by key. Will include old keys not yet garbage collected */
51
+ cache: Record<string, ItemType | undefined>;
52
+ /** List of keys that have been set, in chronological order. Used to find cache items most likely to be expired */
53
+ keyList: Array<string>;
54
+ /** The [setTime, expirationTime] for each key that has been set. Removed on unset. */
55
+ keyInfo: Record<string, [number, number] | undefined>;
56
+ /** Number of operations remaining until internal cleanup of old keys is performed. Based on options.opLimit */
57
+ opsLeft: number;
57
58
  }
58
-
59
- declare const CURRENT_META_VERSION = 2;
59
+ //#endregion
60
+ //#region src/core/defaultOptions.d.ts
61
+ declare const CURRENT_META_VERSION: 2;
60
62
  declare const MAXIMUM_CACHE_TIME: number;
61
63
  declare const defaultOptions: LimitedCacheOptionsReadonly;
62
-
63
- declare const LimitedCache: <ItemType = any>(options?: LimitedCacheOptions) => LimitedCacheInstance<ItemType>;
64
-
65
- declare const LimitedCacheObject: <ItemType = any>(options?: LimitedCacheOptions) => LimitedCacheObjectInstance<ItemType>;
64
+ //#endregion
65
+ //#region src/core/LimitedCache.d.ts
66
+ declare const LimitedCache: <ItemType = DefaultItemType>(options?: LimitedCacheOptions) => LimitedCacheInstance<ItemType>;
67
+ //#endregion
68
+ //#region src/core/LimitedCacheObject.d.ts
69
+ declare const LimitedCacheObject: <ItemType = DefaultItemType>(options?: LimitedCacheOptions) => LimitedCacheObjectInstance<ItemType>;
66
70
  declare const getCacheMetaFromObject: (instance: LimitedCacheObjectInstance) => LimitedCacheMeta;
67
-
68
- declare const limitedCacheUtil: {
69
- init: <ItemType = any>(optionsOrCacheMeta?: LimitedCacheOptions | LimitedCacheMeta<ItemType> | undefined) => LimitedCacheMeta<ItemType>;
70
- get: <ItemType_1 = any>(cacheMeta: LimitedCacheMeta<ItemType_1>, cacheKey: string) => ItemType_1 | undefined;
71
- getAll: <ItemType_2 = any>(cacheMeta: LimitedCacheMeta<ItemType_2>) => Record<string, ItemType_2>;
72
- has: <ItemType_3 = any>(cacheMeta: LimitedCacheMeta<ItemType_3>, cacheKey: string) => boolean;
73
- set: <ItemType_4 = any>(cacheMeta: LimitedCacheMeta<ItemType_4>, cacheKey: string, item: ItemType_4) => LimitedCacheMeta<ItemType_4>;
74
- remove: <ItemType_5 = any>(cacheMeta: LimitedCacheMeta<ItemType_5>, cacheKey: string) => LimitedCacheMeta<ItemType_5>;
75
- reset: <ItemType_6 = any>(cacheMeta: LimitedCacheMeta<ItemType_6>) => LimitedCacheMeta<ItemType_6>;
76
- doMaintenance: <ItemType_7 = any>(cacheMeta: LimitedCacheMeta<ItemType_7>) => LimitedCacheMeta<ItemType_7>;
77
- setOptions: <ItemType_8 = any>(cacheMeta: LimitedCacheMeta<ItemType_8>, options: LimitedCacheOptions) => Readonly<LimitedCacheOptionsFull>;
78
- };
79
-
80
- declare const isCacheMeta: (cacheMeta: LimitedCacheMeta) => boolean;
71
+ //#endregion
72
+ //#region src/core/lowLevelFunctions.d.ts
73
+ declare const isCacheMeta: (cacheMeta: unknown) => cacheMeta is LimitedCacheMeta;
81
74
  declare const upgradeCacheMeta: (cacheMeta: LimitedCacheMeta) => void;
82
- declare const lowLevelSetOptions: <ItemType = any>(cacheMeta: LimitedCacheMeta<ItemType>, options: LimitedCacheOptions) => LimitedCacheOptionsReadonly;
83
- declare const lowLevelInit: <ItemType = any>(optionsOrCacheMeta?: LimitedCacheOptions | LimitedCacheMeta<ItemType> | undefined) => LimitedCacheMeta<ItemType>;
84
- declare const lowLevelDoMaintenance: <ItemType = any>(cacheMeta: LimitedCacheMeta<ItemType>) => LimitedCacheMeta<ItemType>;
85
- declare const lowLevelHas: <ItemType = any>(cacheMeta: LimitedCacheMeta<ItemType>, cacheKey: string) => boolean;
86
- declare const lowLevelGetOne: <ItemType = any>(cacheMeta: LimitedCacheMeta<ItemType>, cacheKey: string) => ItemType | undefined;
87
- declare const lowLevelGetAll: <ItemType = any>(cacheMeta: LimitedCacheMeta<ItemType>) => Record<string, ItemType>;
88
- declare const lowLevelSet: <ItemType = any>(cacheMeta: LimitedCacheMeta<ItemType>, cacheKey: string, item: ItemType) => LimitedCacheMeta<ItemType>;
89
- declare const lowLevelRemove: <ItemType = any>(cacheMeta: LimitedCacheMeta<ItemType>, cacheKey: string) => LimitedCacheMeta<ItemType>;
90
- declare const lowLevelReset: <ItemType = any>(cacheMeta: LimitedCacheMeta<ItemType>) => LimitedCacheMeta<ItemType>;
91
-
92
- export { CURRENT_META_VERSION, type DefaultItemType, LimitedCache, type LimitedCacheInstance, type LimitedCacheMeta, LimitedCacheObject, type LimitedCacheObjectInstance, type LimitedCacheOptions, type LimitedCacheOptionsFull, type LimitedCacheOptionsReadonly, MAXIMUM_CACHE_TIME, defaultOptions, getCacheMetaFromObject, isCacheMeta, limitedCacheUtil, lowLevelDoMaintenance, lowLevelGetAll, lowLevelGetOne, lowLevelHas, lowLevelInit, lowLevelRemove, lowLevelReset, lowLevelSet, lowLevelSetOptions, upgradeCacheMeta };
75
+ declare const lowLevelSetOptions: <ItemType = DefaultItemType>(cacheMeta: LimitedCacheMeta<ItemType>, options: LimitedCacheOptions) => LimitedCacheOptionsReadonly;
76
+ declare const lowLevelInit: <ItemType = DefaultItemType>(optionsOrCacheMeta?: LimitedCacheOptions | LimitedCacheMeta<ItemType>) => LimitedCacheMeta<ItemType>;
77
+ declare const lowLevelDoMaintenance: <ItemType = DefaultItemType>(cacheMeta: LimitedCacheMeta<ItemType>) => LimitedCacheMeta<ItemType>;
78
+ declare const lowLevelHas: <ItemType = DefaultItemType>(cacheMeta: LimitedCacheMeta<ItemType>, cacheKey: string) => boolean;
79
+ declare const lowLevelGetOne: <ItemType = DefaultItemType>(cacheMeta: LimitedCacheMeta<ItemType>, cacheKey: string) => ItemType | undefined;
80
+ declare const lowLevelGetAll: <ItemType = DefaultItemType>(cacheMeta: LimitedCacheMeta<ItemType>) => Record<string, ItemType>;
81
+ declare const lowLevelSet: <ItemType = DefaultItemType>(cacheMeta: LimitedCacheMeta<ItemType>, cacheKey: string, item: ItemType) => LimitedCacheMeta<ItemType>;
82
+ declare const lowLevelRemove: <ItemType = DefaultItemType>(cacheMeta: LimitedCacheMeta<ItemType>, cacheKey: string) => LimitedCacheMeta<ItemType>;
83
+ declare const lowLevelReset: <ItemType = DefaultItemType>(cacheMeta: LimitedCacheMeta<ItemType>) => LimitedCacheMeta<ItemType>;
84
+ //#endregion
85
+ //#region src/core/limitedCacheUtil.d.ts
86
+ type LimitedCacheUtil = {
87
+ init: typeof lowLevelInit;
88
+ get: typeof lowLevelGetOne;
89
+ getAll: typeof lowLevelGetAll;
90
+ has: typeof lowLevelHas;
91
+ set: typeof lowLevelSet;
92
+ remove: typeof lowLevelRemove;
93
+ reset: typeof lowLevelReset;
94
+ doMaintenance: typeof lowLevelDoMaintenance;
95
+ setOptions: typeof lowLevelSetOptions;
96
+ };
97
+ declare const limitedCacheUtil: LimitedCacheUtil;
98
+ //#endregion
99
+ export { CURRENT_META_VERSION, DefaultItemType, LimitedCache, LimitedCacheInstance, LimitedCacheMeta, LimitedCacheObject, LimitedCacheObjectInstance, LimitedCacheOptions, LimitedCacheOptionsFull, LimitedCacheOptionsReadonly, MAXIMUM_CACHE_TIME, defaultOptions, getCacheMetaFromObject, isCacheMeta, limitedCacheUtil, lowLevelDoMaintenance, lowLevelGetAll, lowLevelGetOne, lowLevelHas, lowLevelInit, lowLevelRemove, lowLevelReset, lowLevelSet, lowLevelSetOptions, upgradeCacheMeta };
100
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../../src/types.ts","../../src/core/defaultOptions.ts","../../src/core/LimitedCache.ts","../../src/core/LimitedCacheObject.ts","../../src/core/lowLevelFunctions.ts","../../src/core/limitedCacheUtil.ts"],"mappings":"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"}
@@ -1,7 +1,100 @@
1
- export * from './core/defaultOptions.js';
2
- export * from './core/LimitedCache.js';
3
- export * from './core/LimitedCacheObject.js';
4
- export * from './core/limitedCacheUtil.js';
5
- export * from './core/lowLevelFunctions.js';
6
- export * from './types.js';
1
+ //#region src/types.d.ts
2
+ type DefaultItemType = any;
3
+ interface LimitedCacheOptionsFull {
4
+ /** Items will be removed to keep the cache within the maxCacheSize limit */
5
+ maxCacheSize: number;
6
+ /** Items will be removed and never returned if they were set more than maxCacheTime milliseconds ago */
7
+ maxCacheTime: number;
8
+ /** (dev only) A warning will be emitted if an item rotates out of the cache before this many milliseconds have passed, to indicate the size is too small */
9
+ warnIfItemPurgedBeforeTime: number;
10
+ /** (private) Internal cleanup of old keys will be performed after this many operations */
11
+ opLimit: number;
12
+ /** (private) Internal optimization to adjust how much searching will be done to find expired items, to avoid being O(n) */
13
+ scanLimit: number;
14
+ }
15
+ type LimitedCacheOptions = Partial<LimitedCacheOptionsFull> | null;
16
+ type LimitedCacheOptionsReadonly = Readonly<LimitedCacheOptionsFull>;
17
+ interface LimitedCacheInstance<ItemType = DefaultItemType> {
18
+ /** Return the requested item, if it has not expired */
19
+ get: (cacheKey: string) => ItemType | undefined;
20
+ /** Return all non-expired items */
21
+ getAll: () => Record<string, ItemType>;
22
+ /** Indicate whether or not the requested item is present and has not expired */
23
+ has: (cacheKey: string) => boolean;
24
+ /** Add the item to the cache, or update its timestamp if it already exists */
25
+ set: (cacheKey: string, item: ItemType) => ItemType;
26
+ /** Remove the requested item from the cache, if necessary */
27
+ remove: (cacheKey: string) => true;
28
+ /** Remove all items and all timestamps from the cache */
29
+ reset: () => LimitedCacheMeta<ItemType>;
30
+ /** Return a serializable representation of the cache internals, suitable for long-term storage */
31
+ getCacheMeta: () => LimitedCacheMeta<ItemType>;
32
+ /** Return the cache's current values for all options */
33
+ getOptions: () => LimitedCacheOptionsFull;
34
+ /** Update one or more of the cache's options */
35
+ setOptions: (newOptions: LimitedCacheOptions) => LimitedCacheOptionsReadonly;
36
+ /** Reduces cache size by cleaning up old keys and expired items */
37
+ doMaintenance: () => LimitedCacheMeta<ItemType>;
38
+ }
39
+ interface LimitedCacheObjectInstance<ItemType = DefaultItemType> {
40
+ [key: string]: ItemType;
41
+ }
42
+ /**
43
+ * A serializable representation of the cache internals, suitable for long-term storage
44
+ */
45
+ interface LimitedCacheMeta<ItemType = DefaultItemType> {
46
+ /** Schema version: old versions will be upgraded if possible, or a warning will be emitted if not */
47
+ limitedCacheMetaVersion: number;
48
+ /** Options to control cache size, time, and behavior */
49
+ options: LimitedCacheOptionsReadonly;
50
+ /** The values in the cache, stored by key. Will include old keys not yet garbage collected */
51
+ cache: Record<string, ItemType | undefined>;
52
+ /** List of keys that have been set, in chronological order. Used to find cache items most likely to be expired */
53
+ keyList: Array<string>;
54
+ /** The [setTime, expirationTime] for each key that has been set. Removed on unset. */
55
+ keyInfo: Record<string, [number, number] | undefined>;
56
+ /** Number of operations remaining until internal cleanup of old keys is performed. Based on options.opLimit */
57
+ opsLeft: number;
58
+ }
59
+ //#endregion
60
+ //#region src/core/defaultOptions.d.ts
61
+ declare const CURRENT_META_VERSION: 2;
62
+ declare const MAXIMUM_CACHE_TIME: number;
63
+ declare const defaultOptions: LimitedCacheOptionsReadonly;
64
+ //#endregion
65
+ //#region src/core/LimitedCache.d.ts
66
+ declare const LimitedCache: <ItemType = DefaultItemType>(options?: LimitedCacheOptions) => LimitedCacheInstance<ItemType>;
67
+ //#endregion
68
+ //#region src/core/LimitedCacheObject.d.ts
69
+ declare const LimitedCacheObject: <ItemType = DefaultItemType>(options?: LimitedCacheOptions) => LimitedCacheObjectInstance<ItemType>;
70
+ declare const getCacheMetaFromObject: (instance: LimitedCacheObjectInstance) => LimitedCacheMeta;
71
+ //#endregion
72
+ //#region src/core/lowLevelFunctions.d.ts
73
+ declare const isCacheMeta: (cacheMeta: unknown) => cacheMeta is LimitedCacheMeta;
74
+ declare const upgradeCacheMeta: (cacheMeta: LimitedCacheMeta) => void;
75
+ declare const lowLevelSetOptions: <ItemType = DefaultItemType>(cacheMeta: LimitedCacheMeta<ItemType>, options: LimitedCacheOptions) => LimitedCacheOptionsReadonly;
76
+ declare const lowLevelInit: <ItemType = DefaultItemType>(optionsOrCacheMeta?: LimitedCacheOptions | LimitedCacheMeta<ItemType>) => LimitedCacheMeta<ItemType>;
77
+ declare const lowLevelDoMaintenance: <ItemType = DefaultItemType>(cacheMeta: LimitedCacheMeta<ItemType>) => LimitedCacheMeta<ItemType>;
78
+ declare const lowLevelHas: <ItemType = DefaultItemType>(cacheMeta: LimitedCacheMeta<ItemType>, cacheKey: string) => boolean;
79
+ declare const lowLevelGetOne: <ItemType = DefaultItemType>(cacheMeta: LimitedCacheMeta<ItemType>, cacheKey: string) => ItemType | undefined;
80
+ declare const lowLevelGetAll: <ItemType = DefaultItemType>(cacheMeta: LimitedCacheMeta<ItemType>) => Record<string, ItemType>;
81
+ declare const lowLevelSet: <ItemType = DefaultItemType>(cacheMeta: LimitedCacheMeta<ItemType>, cacheKey: string, item: ItemType) => LimitedCacheMeta<ItemType>;
82
+ declare const lowLevelRemove: <ItemType = DefaultItemType>(cacheMeta: LimitedCacheMeta<ItemType>, cacheKey: string) => LimitedCacheMeta<ItemType>;
83
+ declare const lowLevelReset: <ItemType = DefaultItemType>(cacheMeta: LimitedCacheMeta<ItemType>) => LimitedCacheMeta<ItemType>;
84
+ //#endregion
85
+ //#region src/core/limitedCacheUtil.d.ts
86
+ type LimitedCacheUtil = {
87
+ init: typeof lowLevelInit;
88
+ get: typeof lowLevelGetOne;
89
+ getAll: typeof lowLevelGetAll;
90
+ has: typeof lowLevelHas;
91
+ set: typeof lowLevelSet;
92
+ remove: typeof lowLevelRemove;
93
+ reset: typeof lowLevelReset;
94
+ doMaintenance: typeof lowLevelDoMaintenance;
95
+ setOptions: typeof lowLevelSetOptions;
96
+ };
97
+ declare const limitedCacheUtil: LimitedCacheUtil;
98
+ //#endregion
99
+ export { CURRENT_META_VERSION, DefaultItemType, LimitedCache, LimitedCacheInstance, LimitedCacheMeta, LimitedCacheObject, LimitedCacheObjectInstance, LimitedCacheOptions, LimitedCacheOptionsFull, LimitedCacheOptionsReadonly, MAXIMUM_CACHE_TIME, defaultOptions, getCacheMetaFromObject, isCacheMeta, limitedCacheUtil, lowLevelDoMaintenance, lowLevelGetAll, lowLevelGetOne, lowLevelHas, lowLevelInit, lowLevelRemove, lowLevelReset, lowLevelSet, lowLevelSetOptions, upgradeCacheMeta };
7
100
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC;AACzC,cAAc,wBAAwB,CAAC;AACvC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,4BAA4B,CAAC;AAE3C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../../src/types.ts","../../src/core/defaultOptions.ts","../../src/core/LimitedCache.ts","../../src/core/LimitedCacheObject.ts","../../src/core/lowLevelFunctions.ts","../../src/core/limitedCacheUtil.ts"],"mappings":"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"}
package/dist/esm/index.js CHANGED
@@ -1,7 +1,280 @@
1
- export * from './core/defaultOptions.js';
2
- export * from './core/LimitedCache.js';
3
- export * from './core/LimitedCacheObject.js';
4
- export * from './core/limitedCacheUtil.js';
5
- export * from './core/lowLevelFunctions.js';
6
- export * from './types.js';
1
+ //#region src/core/defaultOptions.ts
2
+ const CURRENT_META_VERSION = 2;
3
+ const MAXIMUM_CACHE_TIME = 365 * 86400 * 1e3;
4
+ const defaultOptions = {
5
+ maxCacheSize: 100,
6
+ maxCacheTime: 86400 * 1e3,
7
+ warnIfItemPurgedBeforeTime: 5e3,
8
+ opLimit: 200,
9
+ scanLimit: 50
10
+ };
11
+ //#endregion
12
+ //#region src/core/lowLevelFunctions.ts
13
+ const positiveNumberOrZero = (value) => Math.max(value, 0) || 0;
14
+ const normalizeOptions = (cacheMetaOptions) => {
15
+ Object.assign(cacheMetaOptions, {
16
+ maxCacheSize: positiveNumberOrZero(cacheMetaOptions.maxCacheSize),
17
+ maxCacheTime: positiveNumberOrZero(cacheMetaOptions.maxCacheTime),
18
+ opLimit: positiveNumberOrZero(cacheMetaOptions.opLimit)
19
+ });
20
+ if (process.env.NODE_ENV !== "production") cacheMetaOptions.warnIfItemPurgedBeforeTime = positiveNumberOrZero(cacheMetaOptions.warnIfItemPurgedBeforeTime);
21
+ return cacheMetaOptions;
22
+ };
23
+ const isCacheMeta = (cacheMeta) => {
24
+ return !!cacheMeta?.limitedCacheMetaVersion;
25
+ };
26
+ const upgradeCacheMeta = (cacheMeta) => {
27
+ if (!isCacheMeta(cacheMeta)) throw new Error("Limited-cache metadata is missing: please check your usage");
28
+ if (cacheMeta.limitedCacheMetaVersion !== 2) {
29
+ console.warn("Limited-cache metadata is from an incompatible version (1). It must be reset.");
30
+ cacheMeta.limitedCacheMetaVersion = 2;
31
+ lowLevelReset(cacheMeta);
32
+ }
33
+ };
34
+ const lowLevelSetOptions = (cacheMeta, options) => {
35
+ upgradeCacheMeta(cacheMeta);
36
+ return normalizeOptions(Object.assign(cacheMeta.options, options));
37
+ };
38
+ const lowLevelInit = (optionsOrCacheMeta) => {
39
+ if (isCacheMeta(optionsOrCacheMeta)) {
40
+ const existingCacheMeta = optionsOrCacheMeta;
41
+ upgradeCacheMeta(existingCacheMeta);
42
+ return existingCacheMeta;
43
+ }
44
+ return lowLevelReset({
45
+ limitedCacheMetaVersion: 2,
46
+ options: normalizeOptions({
47
+ ...defaultOptions,
48
+ ...optionsOrCacheMeta
49
+ })
50
+ });
51
+ };
52
+ const _getExpireTime = (cacheMeta, cacheKey) => {
53
+ const { options: { maxCacheTime }, keyInfo: { [cacheKey]: keyInfo } } = cacheMeta;
54
+ if (!keyInfo) return 0;
55
+ const [setTime, expireTime] = keyInfo;
56
+ return expireTime || setTime + (maxCacheTime || 31536e6);
57
+ };
58
+ const _cacheKeyHasExpired = (cacheMeta, cacheKey, now) => {
59
+ return _getExpireTime(cacheMeta, cacheKey) < now;
60
+ };
61
+ const lowLevelDoMaintenance = (cacheMeta) => {
62
+ upgradeCacheMeta(cacheMeta);
63
+ const { cache, keyList, keyInfo } = cacheMeta;
64
+ const now = Date.now();
65
+ const [newCache, newKeyList, newKeyInfo] = keyList.reduce((acc, cacheKey) => {
66
+ const [accCache, accKeyList, accKeyInfo] = acc;
67
+ if (!_cacheKeyHasExpired(cacheMeta, cacheKey, now)) {
68
+ accCache[cacheKey] = cache[cacheKey];
69
+ accKeyList.push(cacheKey);
70
+ accKeyInfo[cacheKey] = keyInfo[cacheKey];
71
+ }
72
+ return acc;
73
+ }, [
74
+ {},
75
+ [],
76
+ Object.create(null)
77
+ ]);
78
+ return Object.assign(cacheMeta, {
79
+ cache: newCache,
80
+ keyList: newKeyList,
81
+ keyInfo: newKeyInfo,
82
+ opsLeft: cacheMeta.options.opLimit
83
+ });
84
+ };
85
+ const _removeFromIndex = (cacheMeta, startIndex, now) => {
86
+ const { cache, keyList, keyInfo } = cacheMeta;
87
+ let nextIndex = startIndex;
88
+ let nextCacheKey = keyList[startIndex];
89
+ const keyListLength = keyList.length;
90
+ do {
91
+ cache[nextCacheKey] = keyInfo[nextCacheKey] = void 0;
92
+ nextIndex++;
93
+ nextCacheKey = keyList[nextIndex];
94
+ } while (nextIndex < keyListLength && _cacheKeyHasExpired(cacheMeta, nextCacheKey, now));
95
+ keyList.splice(startIndex, nextIndex - startIndex);
96
+ };
97
+ const _removeItemsToMakeRoom = (cacheMeta, now) => {
98
+ const { options: { scanLimit, warnIfItemPurgedBeforeTime }, cache, keyList, keyInfo } = cacheMeta;
99
+ let oldestItemIndex = 0;
100
+ let oldestExpireTime = _getExpireTime(cacheMeta, keyList[0]);
101
+ if (oldestExpireTime > now) {
102
+ let indexToCheck = 0;
103
+ const maxIndexToCheck = Math.min(keyList.length, scanLimit);
104
+ while (indexToCheck < maxIndexToCheck) {
105
+ const cacheKeyForIndex = keyList[indexToCheck];
106
+ const expireTimeForIndex = _getExpireTime(cacheMeta, cacheKeyForIndex);
107
+ if (expireTimeForIndex < now) {
108
+ oldestItemIndex = indexToCheck;
109
+ oldestExpireTime = 0;
110
+ break;
111
+ }
112
+ if (expireTimeForIndex < oldestExpireTime) {
113
+ oldestItemIndex = indexToCheck;
114
+ oldestExpireTime = expireTimeForIndex;
115
+ }
116
+ indexToCheck += 1;
117
+ }
118
+ }
119
+ if (process.env.NODE_ENV !== "production" && warnIfItemPurgedBeforeTime && oldestExpireTime > now) {
120
+ const oldestItemKey = keyList[oldestItemIndex];
121
+ const [oldestItemSetTime, oldestItemExpireTime] = keyInfo[oldestItemKey];
122
+ if (now - oldestItemSetTime < warnIfItemPurgedBeforeTime) console.warn("Purged an item from cache while it was still fresh: you may want to increase maxCacheSize", {
123
+ currentTime: now,
124
+ key: oldestItemKey,
125
+ item: cache[oldestItemKey],
126
+ setTime: oldestItemSetTime,
127
+ expireTime: oldestItemExpireTime,
128
+ timeInCache: now - oldestItemSetTime
129
+ });
130
+ }
131
+ _removeFromIndex(cacheMeta, oldestItemIndex, now);
132
+ };
133
+ const lowLevelHas = (cacheMeta, cacheKey) => {
134
+ upgradeCacheMeta(cacheMeta);
135
+ const { cache } = cacheMeta;
136
+ if (Object.prototype.hasOwnProperty.call(cache, cacheKey) && cache[cacheKey] !== void 0) {
137
+ if (!_cacheKeyHasExpired(cacheMeta, cacheKey, Date.now())) return true;
138
+ cache[cacheKey] = void 0;
139
+ }
140
+ return false;
141
+ };
142
+ const lowLevelGetOne = (cacheMeta, cacheKey) => {
143
+ upgradeCacheMeta(cacheMeta);
144
+ if (lowLevelHas(cacheMeta, cacheKey)) return cacheMeta.cache[cacheKey];
145
+ };
146
+ const lowLevelGetAll = (cacheMeta) => {
147
+ upgradeCacheMeta(cacheMeta);
148
+ lowLevelDoMaintenance(cacheMeta);
149
+ return cacheMeta.cache;
150
+ };
151
+ const lowLevelSet = (cacheMeta, cacheKey, item) => {
152
+ upgradeCacheMeta(cacheMeta);
153
+ const { options: { maxCacheSize }, keyList, keyInfo } = cacheMeta;
154
+ const now = Date.now();
155
+ const isNew = !keyInfo[cacheKey];
156
+ if (cacheMeta.cache[cacheKey] !== item) cacheMeta.cache = {
157
+ ...cacheMeta.cache,
158
+ [cacheKey]: item
159
+ };
160
+ keyInfo[cacheKey] = [now, 0];
161
+ if (isNew) {
162
+ keyList.push(cacheKey);
163
+ cacheMeta.opsLeft--;
164
+ if (cacheMeta.opsLeft <= 0) lowLevelDoMaintenance(cacheMeta);
165
+ if (maxCacheSize && cacheMeta.keyList.length > maxCacheSize) _removeItemsToMakeRoom(cacheMeta, now);
166
+ }
167
+ if (_cacheKeyHasExpired(cacheMeta, keyList[0], now)) _removeFromIndex(cacheMeta, 0, now);
168
+ return cacheMeta;
169
+ };
170
+ const lowLevelRemove = (cacheMeta, cacheKey) => {
171
+ upgradeCacheMeta(cacheMeta);
172
+ const { cache, keyInfo } = cacheMeta;
173
+ if (keyInfo[cacheKey]) {
174
+ if (cache[cacheKey] !== void 0) cacheMeta.cache = {
175
+ ...cache,
176
+ [cacheKey]: void 0
177
+ };
178
+ keyInfo[cacheKey] = void 0;
179
+ }
180
+ return cacheMeta;
181
+ };
182
+ const lowLevelReset = (cacheMeta) => {
183
+ upgradeCacheMeta(cacheMeta);
184
+ return Object.assign(cacheMeta, {
185
+ cache: {},
186
+ keyList: [],
187
+ keyInfo: Object.create(null),
188
+ opsLeft: cacheMeta.options.opLimit
189
+ });
190
+ };
191
+ //#endregion
192
+ //#region src/core/LimitedCache.ts
193
+ const bindFunctionToCacheMeta = (fn, cacheMeta) => fn.bind(null, cacheMeta);
194
+ const LimitedCache = (options) => {
195
+ const cacheMeta = lowLevelInit(options);
196
+ return {
197
+ get: bindFunctionToCacheMeta(lowLevelGetOne, cacheMeta),
198
+ getAll: bindFunctionToCacheMeta(lowLevelGetAll, cacheMeta),
199
+ has: bindFunctionToCacheMeta(lowLevelHas, cacheMeta),
200
+ set: (cacheKey, item) => {
201
+ lowLevelSet(cacheMeta, cacheKey, item);
202
+ return item;
203
+ },
204
+ remove: (cacheKey) => {
205
+ lowLevelRemove(cacheMeta, cacheKey);
206
+ return true;
207
+ },
208
+ reset: bindFunctionToCacheMeta(lowLevelReset, cacheMeta),
209
+ getCacheMeta: () => cacheMeta,
210
+ getOptions: () => cacheMeta.options,
211
+ setOptions: bindFunctionToCacheMeta(lowLevelSetOptions, cacheMeta),
212
+ doMaintenance: bindFunctionToCacheMeta(lowLevelDoMaintenance, cacheMeta)
213
+ };
214
+ };
215
+ //#endregion
216
+ //#region src/core/LimitedCacheObject.ts
217
+ const proxyHandler = {
218
+ get: (cacheMeta, cacheKey) => {
219
+ if (cacheKey === "hasOwnProperty") return Object.prototype.hasOwnProperty;
220
+ return lowLevelGetOne(cacheMeta, cacheKey);
221
+ },
222
+ getOwnPropertyDescriptor: (cacheMeta, cacheKey) => {
223
+ const hasResult = lowLevelHas(cacheMeta, cacheKey);
224
+ const getResult = lowLevelGetOne(cacheMeta, cacheKey);
225
+ if (hasResult) return {
226
+ configurable: true,
227
+ enumerable: hasResult,
228
+ value: getResult,
229
+ writable: true
230
+ };
231
+ },
232
+ has: lowLevelHas,
233
+ set: (cacheMeta, cacheKey, item) => {
234
+ lowLevelSet(cacheMeta, cacheKey, item);
235
+ return item;
236
+ },
237
+ deleteProperty: (cacheMeta, cacheKey) => {
238
+ lowLevelRemove(cacheMeta, cacheKey);
239
+ return true;
240
+ },
241
+ ownKeys: (cacheMeta) => Object.keys(lowLevelGetAll(cacheMeta))
242
+ };
243
+ /**
244
+ * TypeScript's Proxy type models the runtime target, but LimitedCacheObject intentionally returns
245
+ * a facade with a different surface from the internal cache metadata target.
246
+ */
247
+ const internal_createLimitedCacheObjectProxy = (cacheMeta) => {
248
+ return new Proxy(cacheMeta, proxyHandler);
249
+ };
250
+ /**
251
+ * So that we can retrieve the cacheMeta for a LimitedCacheObject, without polluting its properties, each proxy
252
+ * is associated back to its internal cacheMeta here.
253
+ */
254
+ const cacheMetasForProxies = /* @__PURE__ */ new WeakMap();
255
+ const LimitedCacheObject = (options) => {
256
+ const cacheMeta = lowLevelInit(options);
257
+ const limitedCacheObject = internal_createLimitedCacheObjectProxy(cacheMeta);
258
+ cacheMetasForProxies.set(limitedCacheObject, cacheMeta);
259
+ return limitedCacheObject;
260
+ };
261
+ const getCacheMetaFromObject = (instance) => {
262
+ return cacheMetasForProxies.get(instance);
263
+ };
264
+ //#endregion
265
+ //#region src/core/limitedCacheUtil.ts
266
+ const limitedCacheUtil = {
267
+ init: lowLevelInit,
268
+ get: lowLevelGetOne,
269
+ getAll: lowLevelGetAll,
270
+ has: lowLevelHas,
271
+ set: lowLevelSet,
272
+ remove: lowLevelRemove,
273
+ reset: lowLevelReset,
274
+ doMaintenance: lowLevelDoMaintenance,
275
+ setOptions: lowLevelSetOptions
276
+ };
277
+ //#endregion
278
+ export { CURRENT_META_VERSION, LimitedCache, LimitedCacheObject, MAXIMUM_CACHE_TIME, defaultOptions, getCacheMetaFromObject, isCacheMeta, limitedCacheUtil, lowLevelDoMaintenance, lowLevelGetAll, lowLevelGetOne, lowLevelHas, lowLevelInit, lowLevelRemove, lowLevelReset, lowLevelSet, lowLevelSetOptions, upgradeCacheMeta };
279
+
7
280
  //# sourceMappingURL=index.js.map