@sebspark/promise-cache 4.0.1 → 4.0.3

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/dist/index.d.mts CHANGED
@@ -1,110 +1,9 @@
1
- import { createClient, RedisClientOptions, SetOptions } from 'redis';
1
+ import { SetOptions, RedisClientOptions, createClient } from 'redis';
2
2
  export { RedisClientOptions } from 'redis';
3
- import { Logger } from 'winston';
4
3
  import { UUID } from 'node:crypto';
4
+ import { Logger } from 'winston';
5
5
  import { add, sub } from 'date-fns';
6
6
 
7
- type GetType<T> = {
8
- value: T;
9
- ttl?: number;
10
- timestamp: number;
11
- };
12
- type SetParams<T> = {
13
- value: T;
14
- timestamp?: number;
15
- ttl?: number;
16
- };
17
- type PersistorConstructorType = {
18
- redis?: RedisClientOptions;
19
- clientId?: UUID;
20
- onError?: (error: string) => void;
21
- onSuccess?: () => void;
22
- logger?: Logger;
23
- };
24
- declare class Persistor {
25
- client: ReturnType<typeof createClient> | null;
26
- private readonly clientId?;
27
- private readonly onError;
28
- private readonly onSuccess;
29
- private readonly logger;
30
- private readonly redis?;
31
- constructor({ redis, clientId, onSuccess, onError, logger, }: PersistorConstructorType);
32
- startConnection(): Promise<void>;
33
- size(): Promise<number>;
34
- getClientId(): UUID | undefined;
35
- getIsClientConnected(): boolean;
36
- private createOptions;
37
- /**
38
- * Set a value in the cache.
39
- * @param key Cache key.
40
- * @param object.value Value to set in the cache.
41
- * @param object.ttl Time to live in seconds.
42
- * @param object.timestamp Timestamp
43
- */
44
- set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
45
- /**
46
- * Get a value from the cache.
47
- * @param key Cache key.
48
- * @returns GetType<T> value
49
- */
50
- get<T>(key: string): Promise<GetType<T> | null>;
51
- /**
52
- * Delete a value from the cache.
53
- * @param key Cache key
54
- */
55
- delete(key: string): Promise<void>;
56
- }
57
-
58
- type PromiseCacheOptions = {
59
- ttlInSeconds?: number;
60
- caseSensitive?: boolean;
61
- redis?: RedisClientOptions;
62
- fallbackToFunction?: boolean;
63
- onError?: (error: string) => void;
64
- onSuccess?: () => void;
65
- logger?: Logger;
66
- };
67
- declare class PromiseCache<U> {
68
- persistor: Persistor;
69
- private readonly clientId;
70
- private readonly caseSensitive;
71
- private readonly fallbackToFunction;
72
- private readonly ttl?;
73
- private readonly logger;
74
- /**
75
- * Initialize a new PromiseCache.
76
- * @param ttlInSeconds Default cache TTL.
77
- * @param caseSensitive Set to true if you want to differentiate between keys with different casing.
78
- */
79
- constructor({ ttlInSeconds, caseSensitive, redis, fallbackToFunction, onSuccess, onError, logger, }: PromiseCacheOptions);
80
- /**
81
- * Cache size.
82
- * @returns The number of entries in the cache.
83
- */
84
- size(): Promise<number>;
85
- /**
86
- * Override a value in the cache.
87
- * @param key Cache key.
88
- * @param value Cache value.
89
- * @param ttlInSeconds Time to live in seconds.
90
- */
91
- override<U>(key: string, value: U, ttlInSeconds?: number): Promise<void>;
92
- /**
93
- * Get a value from the cache.
94
- * @param key Cache key.
95
- */
96
- find<U>(key: string): Promise<U | null>;
97
- /**
98
- * A simple promise cache wrapper.
99
- * @param key Cache key.
100
- * @param delegate The function to execute if the key is not in the cache.
101
- * @param ttlInSeconds Time to live in seconds.
102
- * @param ttlKeyInSeconds The key in the response object that contains the TTL.
103
- * @returns The result of the delegate function.
104
- */
105
- wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number, ttlKeyInSeconds?: string): Promise<U>;
106
- }
107
-
108
7
  type MultiExecReturnTypes = string[] | string | number | boolean | null | undefined;
109
8
  /**
110
9
  * Defines the expiration strategy for cached values.
@@ -854,6 +753,116 @@ declare class InMemoryPersistor implements IPersistor {
854
753
  private clearExpiration;
855
754
  }
856
755
 
756
+ type GetType<T> = {
757
+ value: T;
758
+ ttl?: number;
759
+ timestamp: number;
760
+ };
761
+ type SetParams<T> = {
762
+ value: T;
763
+ timestamp?: number;
764
+ ttl?: number;
765
+ };
766
+ type PersistorConstructorType = {
767
+ redis?: RedisClientOptions;
768
+ clientId?: UUID;
769
+ onError?: (error: string) => void;
770
+ onSuccess?: () => void;
771
+ logger?: Logger;
772
+ };
773
+ declare class Persistor {
774
+ client: ReturnType<typeof createClient> | null;
775
+ private readonly clientId?;
776
+ private readonly onError;
777
+ private readonly onSuccess;
778
+ private readonly logger;
779
+ private readonly redis?;
780
+ constructor({ redis, clientId, onSuccess, onError, logger, }: PersistorConstructorType);
781
+ startConnection(): Promise<void>;
782
+ size(): Promise<number>;
783
+ getClientId(): UUID | undefined;
784
+ getIsClientConnected(): boolean;
785
+ private createOptions;
786
+ /**
787
+ * Set a value in the cache.
788
+ * @param key Cache key.
789
+ * @param object.value Value to set in the cache.
790
+ * @param object.ttl Time to live in seconds.
791
+ * @param object.timestamp Timestamp
792
+ */
793
+ set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
794
+ /**
795
+ * Get a value from the cache.
796
+ * @param key Cache key.
797
+ * @returns GetType<T> value
798
+ */
799
+ get<T>(key: string): Promise<GetType<T> | null>;
800
+ /**
801
+ * Delete a value from the cache.
802
+ * @param key Cache key
803
+ */
804
+ delete(key: string): Promise<void>;
805
+ }
806
+
807
+ type PromiseCacheOptions = {
808
+ ttlInSeconds?: number;
809
+ caseSensitive?: boolean;
810
+ redis?: RedisClientOptions;
811
+ fallbackToFunction?: boolean;
812
+ onError?: (error: string) => void;
813
+ onSuccess?: () => void;
814
+ logger?: Logger;
815
+ };
816
+ declare class PromiseCache<U> {
817
+ persistor: Persistor;
818
+ private readonly clientId;
819
+ private readonly caseSensitive;
820
+ private readonly fallbackToFunction;
821
+ private readonly ttl?;
822
+ private readonly logger;
823
+ /**
824
+ * Initialize a new PromiseCache.
825
+ * @param ttlInSeconds Default cache TTL.
826
+ * @param caseSensitive Set to true if you want to differentiate between keys with different casing.
827
+ */
828
+ constructor({ ttlInSeconds, caseSensitive, redis, fallbackToFunction, onSuccess, onError, logger, }: PromiseCacheOptions);
829
+ /**
830
+ * Cache size.
831
+ * @returns The number of entries in the cache.
832
+ */
833
+ size(): Promise<number>;
834
+ /**
835
+ * Override a value in the cache.
836
+ * @param key Cache key.
837
+ * @param value Cache value.
838
+ * @param ttlInSeconds Time to live in seconds.
839
+ */
840
+ override<U>(key: string, value: U, ttlInSeconds?: number): Promise<void>;
841
+ /**
842
+ * Get a value from the cache.
843
+ * @param key Cache key.
844
+ */
845
+ find<U>(key: string): Promise<U | null>;
846
+ /**
847
+ * A simple promise cache wrapper.
848
+ * @param key Cache key.
849
+ * @param delegate The function to execute if the key is not in the cache.
850
+ * @param ttlInSeconds Time to live in seconds.
851
+ * @param ttlKeyInSeconds The key in the response object that contains the TTL.
852
+ * @returns The result of the delegate function.
853
+ */
854
+ wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number, ttlKeyInSeconds?: string): Promise<U>;
855
+ }
856
+
857
+ declare const serialize: <T>(data: T) => string;
858
+ declare const deserialize: <T>(serialized: string | null) => T | null;
859
+
860
+ declare const serializer_deserialize: typeof deserialize;
861
+ declare const serializer_serialize: typeof serialize;
862
+ declare namespace serializer {
863
+ export { serializer_deserialize as deserialize, serializer_serialize as serialize };
864
+ }
865
+
857
866
  declare const SECOND = 1000;
858
867
  declare const MINUTE: number;
859
868
  declare const HOUR: number;
@@ -885,13 +894,4 @@ declare namespace time {
885
894
  export { time_DAY as DAY, time_HOUR as HOUR, time_MINUTE as MINUTE, time_SECOND as SECOND, time_WEEK as WEEK, time_add as add, time_days as days, time_hours as hours, time_minutes as minutes, time_seconds as seconds, time_sub as sub, time_today as today, time_tomorrow as tomorrow, time_weeks as weeks };
886
895
  }
887
896
 
888
- declare const serialize: <T>(data: T) => string;
889
- declare const deserialize: <T>(serialized: string | null) => T | null;
890
-
891
- declare const serializer_deserialize: typeof deserialize;
892
- declare const serializer_serialize: typeof serialize;
893
- declare namespace serializer {
894
- export { serializer_deserialize as deserialize, serializer_serialize as serialize };
895
- }
896
-
897
897
  export { type Cache, type CachingOptions, type IPersistor, InMemoryPersistor, Persistor, type PersistorConstructorType, PromiseCache, type PromiseCacheOptions, createCache, serializer, time };
package/dist/index.d.ts CHANGED
@@ -1,110 +1,9 @@
1
- import { createClient, RedisClientOptions, SetOptions } from 'redis';
1
+ import { SetOptions, RedisClientOptions, createClient } from 'redis';
2
2
  export { RedisClientOptions } from 'redis';
3
- import { Logger } from 'winston';
4
3
  import { UUID } from 'node:crypto';
4
+ import { Logger } from 'winston';
5
5
  import { add, sub } from 'date-fns';
6
6
 
7
- type GetType<T> = {
8
- value: T;
9
- ttl?: number;
10
- timestamp: number;
11
- };
12
- type SetParams<T> = {
13
- value: T;
14
- timestamp?: number;
15
- ttl?: number;
16
- };
17
- type PersistorConstructorType = {
18
- redis?: RedisClientOptions;
19
- clientId?: UUID;
20
- onError?: (error: string) => void;
21
- onSuccess?: () => void;
22
- logger?: Logger;
23
- };
24
- declare class Persistor {
25
- client: ReturnType<typeof createClient> | null;
26
- private readonly clientId?;
27
- private readonly onError;
28
- private readonly onSuccess;
29
- private readonly logger;
30
- private readonly redis?;
31
- constructor({ redis, clientId, onSuccess, onError, logger, }: PersistorConstructorType);
32
- startConnection(): Promise<void>;
33
- size(): Promise<number>;
34
- getClientId(): UUID | undefined;
35
- getIsClientConnected(): boolean;
36
- private createOptions;
37
- /**
38
- * Set a value in the cache.
39
- * @param key Cache key.
40
- * @param object.value Value to set in the cache.
41
- * @param object.ttl Time to live in seconds.
42
- * @param object.timestamp Timestamp
43
- */
44
- set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
45
- /**
46
- * Get a value from the cache.
47
- * @param key Cache key.
48
- * @returns GetType<T> value
49
- */
50
- get<T>(key: string): Promise<GetType<T> | null>;
51
- /**
52
- * Delete a value from the cache.
53
- * @param key Cache key
54
- */
55
- delete(key: string): Promise<void>;
56
- }
57
-
58
- type PromiseCacheOptions = {
59
- ttlInSeconds?: number;
60
- caseSensitive?: boolean;
61
- redis?: RedisClientOptions;
62
- fallbackToFunction?: boolean;
63
- onError?: (error: string) => void;
64
- onSuccess?: () => void;
65
- logger?: Logger;
66
- };
67
- declare class PromiseCache<U> {
68
- persistor: Persistor;
69
- private readonly clientId;
70
- private readonly caseSensitive;
71
- private readonly fallbackToFunction;
72
- private readonly ttl?;
73
- private readonly logger;
74
- /**
75
- * Initialize a new PromiseCache.
76
- * @param ttlInSeconds Default cache TTL.
77
- * @param caseSensitive Set to true if you want to differentiate between keys with different casing.
78
- */
79
- constructor({ ttlInSeconds, caseSensitive, redis, fallbackToFunction, onSuccess, onError, logger, }: PromiseCacheOptions);
80
- /**
81
- * Cache size.
82
- * @returns The number of entries in the cache.
83
- */
84
- size(): Promise<number>;
85
- /**
86
- * Override a value in the cache.
87
- * @param key Cache key.
88
- * @param value Cache value.
89
- * @param ttlInSeconds Time to live in seconds.
90
- */
91
- override<U>(key: string, value: U, ttlInSeconds?: number): Promise<void>;
92
- /**
93
- * Get a value from the cache.
94
- * @param key Cache key.
95
- */
96
- find<U>(key: string): Promise<U | null>;
97
- /**
98
- * A simple promise cache wrapper.
99
- * @param key Cache key.
100
- * @param delegate The function to execute if the key is not in the cache.
101
- * @param ttlInSeconds Time to live in seconds.
102
- * @param ttlKeyInSeconds The key in the response object that contains the TTL.
103
- * @returns The result of the delegate function.
104
- */
105
- wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number, ttlKeyInSeconds?: string): Promise<U>;
106
- }
107
-
108
7
  type MultiExecReturnTypes = string[] | string | number | boolean | null | undefined;
109
8
  /**
110
9
  * Defines the expiration strategy for cached values.
@@ -854,6 +753,116 @@ declare class InMemoryPersistor implements IPersistor {
854
753
  private clearExpiration;
855
754
  }
856
755
 
756
+ type GetType<T> = {
757
+ value: T;
758
+ ttl?: number;
759
+ timestamp: number;
760
+ };
761
+ type SetParams<T> = {
762
+ value: T;
763
+ timestamp?: number;
764
+ ttl?: number;
765
+ };
766
+ type PersistorConstructorType = {
767
+ redis?: RedisClientOptions;
768
+ clientId?: UUID;
769
+ onError?: (error: string) => void;
770
+ onSuccess?: () => void;
771
+ logger?: Logger;
772
+ };
773
+ declare class Persistor {
774
+ client: ReturnType<typeof createClient> | null;
775
+ private readonly clientId?;
776
+ private readonly onError;
777
+ private readonly onSuccess;
778
+ private readonly logger;
779
+ private readonly redis?;
780
+ constructor({ redis, clientId, onSuccess, onError, logger, }: PersistorConstructorType);
781
+ startConnection(): Promise<void>;
782
+ size(): Promise<number>;
783
+ getClientId(): UUID | undefined;
784
+ getIsClientConnected(): boolean;
785
+ private createOptions;
786
+ /**
787
+ * Set a value in the cache.
788
+ * @param key Cache key.
789
+ * @param object.value Value to set in the cache.
790
+ * @param object.ttl Time to live in seconds.
791
+ * @param object.timestamp Timestamp
792
+ */
793
+ set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
794
+ /**
795
+ * Get a value from the cache.
796
+ * @param key Cache key.
797
+ * @returns GetType<T> value
798
+ */
799
+ get<T>(key: string): Promise<GetType<T> | null>;
800
+ /**
801
+ * Delete a value from the cache.
802
+ * @param key Cache key
803
+ */
804
+ delete(key: string): Promise<void>;
805
+ }
806
+
807
+ type PromiseCacheOptions = {
808
+ ttlInSeconds?: number;
809
+ caseSensitive?: boolean;
810
+ redis?: RedisClientOptions;
811
+ fallbackToFunction?: boolean;
812
+ onError?: (error: string) => void;
813
+ onSuccess?: () => void;
814
+ logger?: Logger;
815
+ };
816
+ declare class PromiseCache<U> {
817
+ persistor: Persistor;
818
+ private readonly clientId;
819
+ private readonly caseSensitive;
820
+ private readonly fallbackToFunction;
821
+ private readonly ttl?;
822
+ private readonly logger;
823
+ /**
824
+ * Initialize a new PromiseCache.
825
+ * @param ttlInSeconds Default cache TTL.
826
+ * @param caseSensitive Set to true if you want to differentiate between keys with different casing.
827
+ */
828
+ constructor({ ttlInSeconds, caseSensitive, redis, fallbackToFunction, onSuccess, onError, logger, }: PromiseCacheOptions);
829
+ /**
830
+ * Cache size.
831
+ * @returns The number of entries in the cache.
832
+ */
833
+ size(): Promise<number>;
834
+ /**
835
+ * Override a value in the cache.
836
+ * @param key Cache key.
837
+ * @param value Cache value.
838
+ * @param ttlInSeconds Time to live in seconds.
839
+ */
840
+ override<U>(key: string, value: U, ttlInSeconds?: number): Promise<void>;
841
+ /**
842
+ * Get a value from the cache.
843
+ * @param key Cache key.
844
+ */
845
+ find<U>(key: string): Promise<U | null>;
846
+ /**
847
+ * A simple promise cache wrapper.
848
+ * @param key Cache key.
849
+ * @param delegate The function to execute if the key is not in the cache.
850
+ * @param ttlInSeconds Time to live in seconds.
851
+ * @param ttlKeyInSeconds The key in the response object that contains the TTL.
852
+ * @returns The result of the delegate function.
853
+ */
854
+ wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number, ttlKeyInSeconds?: string): Promise<U>;
855
+ }
856
+
857
+ declare const serialize: <T>(data: T) => string;
858
+ declare const deserialize: <T>(serialized: string | null) => T | null;
859
+
860
+ declare const serializer_deserialize: typeof deserialize;
861
+ declare const serializer_serialize: typeof serialize;
862
+ declare namespace serializer {
863
+ export { serializer_deserialize as deserialize, serializer_serialize as serialize };
864
+ }
865
+
857
866
  declare const SECOND = 1000;
858
867
  declare const MINUTE: number;
859
868
  declare const HOUR: number;
@@ -885,13 +894,4 @@ declare namespace time {
885
894
  export { time_DAY as DAY, time_HOUR as HOUR, time_MINUTE as MINUTE, time_SECOND as SECOND, time_WEEK as WEEK, time_add as add, time_days as days, time_hours as hours, time_minutes as minutes, time_seconds as seconds, time_sub as sub, time_today as today, time_tomorrow as tomorrow, time_weeks as weeks };
886
895
  }
887
896
 
888
- declare const serialize: <T>(data: T) => string;
889
- declare const deserialize: <T>(serialized: string | null) => T | null;
890
-
891
- declare const serializer_deserialize: typeof deserialize;
892
- declare const serializer_serialize: typeof serialize;
893
- declare namespace serializer {
894
- export { serializer_deserialize as deserialize, serializer_serialize as serialize };
895
- }
896
-
897
897
  export { type Cache, type CachingOptions, type IPersistor, InMemoryPersistor, Persistor, type PersistorConstructorType, PromiseCache, type PromiseCacheOptions, createCache, serializer, time };