@tramvai/tokens-common 4.36.2 → 4.38.0

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.
package/lib/cache.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Options } from '@tinkoff/lru-cache-nano';
1
+ import type { CacheServerMetricsHandlers } from './cacheMetrics';
2
2
  /**
3
3
  * @description
4
4
  * Function for creating a new cache
@@ -10,20 +10,21 @@ export declare const CREATE_CACHE_TOKEN: CacheFactory & {
10
10
  };
11
11
  /**
12
12
  * @description
13
- * Function that us called on force cache clean up in the app
13
+ * Function that is called on force cache clean up in the app
14
14
  */
15
- export declare const REGISTER_CLEAR_CACHE_TOKEN: ((type: string) => void | Promise<void>) & {
15
+ export declare const REGISTER_CLEAR_CACHE_TOKEN: ((type?: CacheType) => void | Promise<void>) & {
16
16
  __type?: "multi token" | undefined;
17
17
  };
18
18
  /**
19
19
  * @description
20
20
  * Force cleaning up all caches in the app
21
21
  */
22
- export declare const CLEAR_CACHE_TOKEN: ((type?: string) => Promise<void>) & {
22
+ export declare const CLEAR_CACHE_TOKEN: ((type?: CacheType) => Promise<void | void[]>) & {
23
23
  __type?: "base token" | undefined;
24
24
  };
25
25
  export interface Cache<T = any> {
26
26
  get(key: string): T | undefined;
27
+ peek(key: string): T | undefined;
27
28
  set(key: string, value: T, options?: {
28
29
  ttl?: number;
29
30
  }): void;
@@ -36,11 +37,88 @@ export interface Cache<T = any> {
36
37
  load(arr: Array<[string, {
37
38
  value: T;
38
39
  }]>): void;
40
+ size: number;
39
41
  }
40
- export type CacheType = 'memory' | 'memory-lfu';
41
- export interface CacheOptionsByType<T> {
42
- memory: [Options<string, T> | undefined] | [];
43
- ['memory-lfu']: [Options<string, T> | undefined] | [];
44
- }
45
- export type CacheFactory = <T, Type extends CacheType = 'memory'>(type?: Type, ...args: CacheOptionsByType<T>[Type]) => Cache<T>;
42
+ export declare const MEMORY_LRU = "memory";
43
+ export declare const MEMORY_LFU = "memory-lfu";
44
+ export type CacheType = typeof MEMORY_LRU | typeof MEMORY_LFU;
45
+ export type CacheMethod = keyof {
46
+ [M in keyof Cache as Cache[M] extends Function ? M : never]: any;
47
+ };
48
+ type CacheWithMetricsOptions = {
49
+ /** Cache metrics label */
50
+ name?: string;
51
+ };
52
+ type LRUOptions = {
53
+ /**
54
+ * The number of most recently used items to keep.
55
+ * Note that we may store fewer items than this if maxSize is hit.
56
+ */
57
+ max?: number;
58
+ /**
59
+ * Max time to live for items before they are considered stale.
60
+ * Note that stale items are NOT preemptively removed by default,
61
+ * and MAY live in the cache, contributing to its LRU max, long after
62
+ * they have expired.
63
+ *
64
+ * Also, as this cache is optimized for LRU/MRU operations, some of
65
+ * the staleness/TTL checks will reduce performance, as they will incur
66
+ * overhead by deleting items.
67
+ *
68
+ * Must be a positive integer in ms, defaults to 0, which means "no TTL"
69
+ *
70
+ * @default 0
71
+ */
72
+ ttl?: number;
73
+ /**
74
+ * Minimum amount of time in ms in which to check for staleness.
75
+ * Defaults to 1, which means that the current time is checked
76
+ * at most once per millisecond.
77
+ *
78
+ * Set to 0 to check the current time every time staleness is tested.
79
+ *
80
+ * Note that setting this to a higher value will improve performance
81
+ * somewhat while using ttl tracking, albeit at the expense of keeping
82
+ * stale items around a bit longer than intended.
83
+ *
84
+ * @default 1
85
+ */
86
+ ttlResolution?: number;
87
+ /**
88
+ * Return stale items from cache.get() before disposing of them
89
+ *
90
+ * @default false
91
+ */
92
+ allowStale?: boolean;
93
+ /**
94
+ * Update the age of items on cache.get(), renewing their TTL
95
+ *
96
+ * @default false
97
+ */
98
+ updateAgeOnGet?: boolean;
99
+ };
100
+ type LFUOptions = {
101
+ /**
102
+ * Specifies the maximum number item to accumulate in the cache. Defaults to 100
103
+ */
104
+ max?: number;
105
+ /**
106
+ * Specifies the number of items to be evicted once the cache is full.
107
+ * If options.max is specified and options.evictCount is not specified,
108
+ * then it defaults to 10% of options.max else it defaults to 1
109
+ */
110
+ evictCount?: number;
111
+ /**
112
+ * Maximum time upto which the keys would be retained in the cache even when unused.
113
+ * Note that this time is in milliseconds(ms)
114
+ */
115
+ maxAge?: number;
116
+ };
117
+ export type CacheFactoryOptions<Type extends CacheType> = CacheWithMetricsOptions & (Type extends typeof MEMORY_LRU ? LRUOptions : Type extends typeof MEMORY_LFU ? LFUOptions : never);
118
+ export type CacheOptions<Type extends CacheType> = CacheFactoryOptions<Type> & {
119
+ type: Type;
120
+ max: number;
121
+ };
122
+ export type CacheFactory = <T, Type extends CacheType = typeof MEMORY_LRU>(cacheType?: Type, options?: CacheFactoryOptions<Type>, metrics?: CacheServerMetricsHandlers) => Cache<T>;
123
+ export {};
46
124
  //# sourceMappingURL=cache.d.ts.map
package/lib/cache.es.js CHANGED
@@ -9,7 +9,7 @@ import { createToken } from '@tinkoff/dippy';
9
9
  const CREATE_CACHE_TOKEN = createToken('createCache');
10
10
  /**
11
11
  * @description
12
- * Function that us called on force cache clean up in the app
12
+ * Function that is called on force cache clean up in the app
13
13
  */
14
14
  const REGISTER_CLEAR_CACHE_TOKEN = createToken('registerClearCache', { multi: true });
15
15
  /**
@@ -17,5 +17,7 @@ const REGISTER_CLEAR_CACHE_TOKEN = createToken('registerClearCache', { multi: tr
17
17
  * Force cleaning up all caches in the app
18
18
  */
19
19
  const CLEAR_CACHE_TOKEN = createToken('clearCache');
20
+ const MEMORY_LRU = 'memory';
21
+ const MEMORY_LFU = 'memory-lfu';
20
22
 
21
- export { CLEAR_CACHE_TOKEN, CREATE_CACHE_TOKEN, REGISTER_CLEAR_CACHE_TOKEN };
23
+ export { CLEAR_CACHE_TOKEN, CREATE_CACHE_TOKEN, MEMORY_LFU, MEMORY_LRU, REGISTER_CLEAR_CACHE_TOKEN };
package/lib/cache.js CHANGED
@@ -13,7 +13,7 @@ var dippy = require('@tinkoff/dippy');
13
13
  const CREATE_CACHE_TOKEN = dippy.createToken('createCache');
14
14
  /**
15
15
  * @description
16
- * Function that us called on force cache clean up in the app
16
+ * Function that is called on force cache clean up in the app
17
17
  */
18
18
  const REGISTER_CLEAR_CACHE_TOKEN = dippy.createToken('registerClearCache', { multi: true });
19
19
  /**
@@ -21,7 +21,11 @@ const REGISTER_CLEAR_CACHE_TOKEN = dippy.createToken('registerClearCache', { mul
21
21
  * Force cleaning up all caches in the app
22
22
  */
23
23
  const CLEAR_CACHE_TOKEN = dippy.createToken('clearCache');
24
+ const MEMORY_LRU = 'memory';
25
+ const MEMORY_LFU = 'memory-lfu';
24
26
 
25
27
  exports.CLEAR_CACHE_TOKEN = CLEAR_CACHE_TOKEN;
26
28
  exports.CREATE_CACHE_TOKEN = CREATE_CACHE_TOKEN;
29
+ exports.MEMORY_LFU = MEMORY_LFU;
30
+ exports.MEMORY_LRU = MEMORY_LRU;
27
31
  exports.REGISTER_CLEAR_CACHE_TOKEN = REGISTER_CLEAR_CACHE_TOKEN;
@@ -0,0 +1,15 @@
1
+ import type { CacheMethod } from './cache';
2
+ /**
3
+ * @description
4
+ * Handlers for server cache metrics
5
+ */
6
+ export declare const CACHE_METRICS_SERVER_TOKEN: CacheServerMetricsHandlers & {
7
+ __type?: "base token" | undefined;
8
+ };
9
+ export type CacheServerMetricsHandlers = {
10
+ onHit(name: string, method: CacheMethod): void;
11
+ onMiss(name: string, method: CacheMethod): void;
12
+ onMax(name: string, size: number): void;
13
+ onSize(name: string, size: number): void;
14
+ };
15
+ //# sourceMappingURL=cacheMetrics.d.ts.map
@@ -0,0 +1,9 @@
1
+ import { createToken } from '@tinkoff/dippy';
2
+
3
+ /**
4
+ * @description
5
+ * Handlers for server cache metrics
6
+ */
7
+ const CACHE_METRICS_SERVER_TOKEN = createToken('cacheMetricsServer');
8
+
9
+ export { CACHE_METRICS_SERVER_TOKEN };
@@ -0,0 +1,13 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var dippy = require('@tinkoff/dippy');
6
+
7
+ /**
8
+ * @description
9
+ * Handlers for server cache metrics
10
+ */
11
+ const CACHE_METRICS_SERVER_TOKEN = dippy.createToken('cacheMetricsServer');
12
+
13
+ exports.CACHE_METRICS_SERVER_TOKEN = CACHE_METRICS_SERVER_TOKEN;
package/lib/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export * from './context';
3
3
  export * from './action';
4
4
  export * from './hook';
5
5
  export * from './pubsub';
6
+ export * from './cacheMetrics';
6
7
  export * from './cache';
7
8
  export * from './componentRegistry';
8
9
  export * from './state';
package/lib/index.es.js CHANGED
@@ -3,7 +3,8 @@ export { CONTEXT_TOKEN } from './context.es.js';
3
3
  export { ACTION_CONDITIONALS, ACTION_EXECUTION_TOKEN, ACTION_PAGE_RUNNER_TOKEN, ACTION_REGISTRY_TOKEN, DEFERRED_ACTIONS_MAP_TOKEN, LIMIT_ACTION_GLOBAL_TIME_RUN } from './action.es.js';
4
4
  export { HOOK_TOKEN } from './hook.es.js';
5
5
  export { PUBSUB_FACTORY_TOKEN, PUBSUB_TOKEN, ROOT_PUBSUB_TOKEN } from './pubsub.es.js';
6
- export { CLEAR_CACHE_TOKEN, CREATE_CACHE_TOKEN, REGISTER_CLEAR_CACHE_TOKEN } from './cache.es.js';
6
+ export { CACHE_METRICS_SERVER_TOKEN } from './cacheMetrics.es.js';
7
+ export { CLEAR_CACHE_TOKEN, CREATE_CACHE_TOKEN, MEMORY_LFU, MEMORY_LRU, REGISTER_CLEAR_CACHE_TOKEN } from './cache.es.js';
7
8
  export { COMPONENT_REGISTRY_TOKEN } from './componentRegistry.es.js';
8
9
  export { COMBINE_REDUCERS, DISPATCHER_CONTEXT_TOKEN, DISPATCHER_TOKEN, INITIAL_APP_STATE_TOKEN, STORE_MIDDLEWARE, STORE_TOKEN } from './state.es.js';
9
10
  export { LOGGER_INIT_HOOK, LOGGER_REMOTE_REPORTER, LOGGER_SHARED_CONTEXT, LOGGER_TOKEN } from './logger.es.js';
package/lib/index.js CHANGED
@@ -7,6 +7,7 @@ var context = require('./context.js');
7
7
  var action = require('./action.js');
8
8
  var hook = require('./hook.js');
9
9
  var pubsub = require('./pubsub.js');
10
+ var cacheMetrics = require('./cacheMetrics.js');
10
11
  var cache = require('./cache.js');
11
12
  var componentRegistry = require('./componentRegistry.js');
12
13
  var state = require('./state.js');
@@ -32,8 +33,11 @@ exports.HOOK_TOKEN = hook.HOOK_TOKEN;
32
33
  exports.PUBSUB_FACTORY_TOKEN = pubsub.PUBSUB_FACTORY_TOKEN;
33
34
  exports.PUBSUB_TOKEN = pubsub.PUBSUB_TOKEN;
34
35
  exports.ROOT_PUBSUB_TOKEN = pubsub.ROOT_PUBSUB_TOKEN;
36
+ exports.CACHE_METRICS_SERVER_TOKEN = cacheMetrics.CACHE_METRICS_SERVER_TOKEN;
35
37
  exports.CLEAR_CACHE_TOKEN = cache.CLEAR_CACHE_TOKEN;
36
38
  exports.CREATE_CACHE_TOKEN = cache.CREATE_CACHE_TOKEN;
39
+ exports.MEMORY_LFU = cache.MEMORY_LFU;
40
+ exports.MEMORY_LRU = cache.MEMORY_LRU;
37
41
  exports.REGISTER_CLEAR_CACHE_TOKEN = cache.REGISTER_CLEAR_CACHE_TOKEN;
38
42
  exports.COMPONENT_REGISTRY_TOKEN = componentRegistry.COMPONENT_REGISTRY_TOKEN;
39
43
  exports.COMBINE_REDUCERS = state.COMBINE_REDUCERS;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/tokens-common",
3
- "version": "4.36.2",
3
+ "version": "4.38.0",
4
4
  "description": "Tramvai tokens for @tramvai/module-common",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib/index.es.js",
@@ -18,15 +18,14 @@
18
18
  "watch": "tsc -w"
19
19
  },
20
20
  "dependencies": {
21
- "@tinkoff/lru-cache-nano": "^7.9.0",
22
21
  "@tinkoff/url": "0.10.1",
23
- "@tramvai/react": "4.36.2",
24
- "@tramvai/tokens-core": "4.36.2"
22
+ "@tramvai/react": "4.38.0",
23
+ "@tramvai/tokens-core": "4.38.0"
25
24
  },
26
25
  "peerDependencies": {
27
26
  "@tinkoff/dippy": "0.10.8",
28
27
  "@tinkoff/logger": "0.10.277",
29
- "@tramvai/types-actions-state-context": "4.36.2",
28
+ "@tramvai/types-actions-state-context": "4.38.0",
30
29
  "react": ">=16.8",
31
30
  "tslib": "^2.4.0"
32
31
  },