@isaacs/ttlcache 1.0.4 → 1.0.5

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 (2) hide show
  1. package/index.d.ts +169 -0
  2. package/package.json +5 -3
package/index.d.ts ADDED
@@ -0,0 +1,169 @@
1
+ // Type definitions for ttlcache 1.0.0
2
+ // Project: https://github.com/isaacs/ttlcache
3
+ // Loosely based on @isaacs/lru-cache
4
+ // https://github.com/isaacs/node-lru-cache/blob/v7.10.1/index.d.ts
5
+
6
+ declare class TTLCache<K, V> implements Iterable<[K, V]> {
7
+ constructor(options: TTLCache.Options<K, V>)
8
+
9
+ /**
10
+ * The total number of items held in the cache at the current moment.
11
+ */
12
+ public readonly size: number
13
+
14
+ /**
15
+ * Add a value to the cache.
16
+ */
17
+ public set(key: K, value: V, options?: TTLCache.SetOptions): this
18
+
19
+ /**
20
+ * Return a value from the cache.
21
+ * If the key is not found, `get()` will return `undefined`.
22
+ * This can be confusing when setting values specifically to `undefined`,
23
+ * as in `cache.set(key, undefined)`. Use `cache.has()` to determine
24
+ * whether a key is present in the cache at all.
25
+ */
26
+ public get<T = V>(
27
+ key: K,
28
+ options?: TTLCache.GetOptions
29
+ ): T | undefined
30
+
31
+ /**
32
+ * Check if a key is in the cache.
33
+ * Will return false if the item is stale, even though it is technically
34
+ * in the cache.
35
+ */
36
+ public has(key: K): boolean
37
+
38
+ /**
39
+ * Deletes a key out of the cache.
40
+ * Returns true if the key was deleted, false otherwise.
41
+ */
42
+ public delete(key: K): boolean
43
+
44
+ /**
45
+ * Clear the cache entirely, throwing away all values.
46
+ */
47
+ public clear(): void
48
+
49
+ /**
50
+ * Delete any stale entries. Returns true if anything was removed, false
51
+ * otherwise.
52
+ */
53
+ public purgeStale(): boolean
54
+
55
+ /**
56
+ * Return the remaining time before an item expires.
57
+ * Returns 0 if the item is not found in the cache or is already expired.
58
+ */
59
+ public getRemainingTTL(key: K): number
60
+
61
+ /**
62
+ * Return a generator yielding `[key, value]` pairs, from soonest expiring
63
+ * to latest expiring. (Items expiring at the same time are walked in insertion order.)
64
+ */
65
+ public entries(): Generator<[K, V]>
66
+
67
+ /**
68
+ * Return a generator yielding the keys in the cache,
69
+ * from soonest expiring to latest expiring.
70
+ */
71
+ public keys(): Generator<K>
72
+
73
+ /**
74
+ * Return a generator yielding the values in the cache,
75
+ * from soonest expiring to latest expiring.
76
+ */
77
+ public values(): Generator<V>
78
+
79
+ /**
80
+ * Iterating over the cache itself yields the same results as
81
+ * `cache.entries()`
82
+ */
83
+ public [Symbol.iterator](): Iterator<[K, V]>
84
+ }
85
+
86
+ declare namespace TTLCache {
87
+ type DisposeReason = 'evict' | 'set' | 'delete'
88
+
89
+ type Disposer<K, V> = (
90
+ value: V,
91
+ key: K,
92
+ reason: DisposeReason
93
+ ) => void
94
+
95
+ type TTLOptions = {
96
+ /**
97
+ * Max time in milliseconds for items to live in cache before they are
98
+ * considered stale. Note that stale items are NOT preemptively removed
99
+ * by default, and MAY live in the cache, contributing to max,
100
+ * long after they have expired.
101
+ *
102
+ * Must be an integer number of ms, defaults to 0, which means "no TTL"
103
+ */
104
+ ttl: number
105
+
106
+ /**
107
+ * Boolean flag to tell the cache to not update the TTL when
108
+ * setting a new value for an existing key (ie, when updating a value
109
+ * rather than inserting a new value). Note that the TTL value is
110
+ * _always_ set when adding a new entry into the cache.
111
+ *
112
+ * @default false
113
+ */
114
+ noUpdateTTL?: boolean
115
+ }
116
+
117
+ type Options<K, V> = {
118
+ /**
119
+ * The number of items to keep.
120
+ *
121
+ * @default Infinity
122
+ */
123
+ max?: number
124
+
125
+ /**
126
+ * Update the age of items on cache.get(), renewing their TTL
127
+ *
128
+ * @default false
129
+ */
130
+ updateAgeOnGet?: boolean
131
+
132
+ /**
133
+ * Function that is called on items when they are dropped from the cache.
134
+ * This can be handy if you want to close file descriptors or do other
135
+ * cleanup tasks when items are no longer accessible. Called with `key,
136
+ * value`. It's called before actually removing the item from the
137
+ * internal cache, so it is *NOT* safe to re-add them.
138
+ * Use `disposeAfter` if you wish to dispose items after they have been
139
+ * full removed, when it is safe to add them back to the cache.
140
+ */
141
+ dispose?: Disposer<K, V>
142
+ } & TTLOptions
143
+
144
+ type SetOptions = {
145
+ /**
146
+ * Set to true to suppress calling the dispose() function if the entry
147
+ * key is still accessible within the cache.
148
+ *
149
+ * @default false
150
+ */
151
+ noDisposeOnSet?: boolean
152
+ noUpdateTTL?: boolean
153
+ ttl?: number
154
+ }
155
+
156
+ type GetOptions = {
157
+ /**
158
+ * Update the age of items
159
+ */
160
+ updateAgeOnGet?: boolean
161
+
162
+ /**
163
+ * Set new TTL, applied only when `updateAgeOnGet` is true
164
+ */
165
+ ttl?: number
166
+ }
167
+ }
168
+
169
+ export = TTLCache
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@isaacs/ttlcache",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "files": [
5
- "index.js"
5
+ "index.js",
6
+ "index.d.ts"
6
7
  ],
7
8
  "main": "index.js",
8
9
  "exports": {
@@ -28,7 +29,8 @@
28
29
  "clock-mock": "^1.0.6",
29
30
  "prettier": "^2.7.0",
30
31
  "tap": "^16.0.1",
31
- "ts-node": "^10.8.1"
32
+ "ts-node": "^10.8.1",
33
+ "typescript": "^4.7.3"
32
34
  },
33
35
  "engines": {
34
36
  "node": ">=12"