@sebspark/promise-cache 3.4.0 → 3.5.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/README.md +13 -1
- package/dist/index.d.mts +7 -2
- package/dist/index.d.ts +7 -2
- package/dist/index.js +16 -0
- package/dist/index.mjs +17 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -173,7 +173,7 @@ The basic functionality of this new implementation is:
|
|
|
173
173
|
3. You call the cache's wrap function to wrap your original function(s)
|
|
174
174
|
4. You call the wrapped function(s) instead of the originals
|
|
175
175
|
|
|
176
|
-
**
|
|
176
|
+
**Why the change?**
|
|
177
177
|
|
|
178
178
|
When trying to add support for caching _to_ a specified time in addition to _for_ a specified time, it tuyrned out to be hard given the original implementation. Changing the implementation proved hard without breaking changes. So a new implementation is is.
|
|
179
179
|
|
|
@@ -276,6 +276,18 @@ const cachedGetById = cache.wrap(myApiCall, {
|
|
|
276
276
|
key: (id) => `getById:${id}`,
|
|
277
277
|
expires: time.seconds(100),
|
|
278
278
|
})
|
|
279
|
+
|
|
280
|
+
// Fixed at today 20:00:00 UTC
|
|
281
|
+
const cachedGetById = cache.wrap(myApiCall, {
|
|
282
|
+
key: (id) => `getById:${id}`,
|
|
283
|
+
expires: time.today(20),
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
// Fixed at tomorrow 00:00:00
|
|
287
|
+
const cachedGetById = cache.wrap(myApiCall, {
|
|
288
|
+
key: (id) => `getById:${id}`,
|
|
289
|
+
expires: time.tomorrow(),
|
|
290
|
+
})
|
|
279
291
|
```
|
|
280
292
|
|
|
281
293
|
If expiry is not set or yields an unusable value, it will default to 1 sec.
|
package/dist/index.d.mts
CHANGED
|
@@ -2,7 +2,7 @@ import { RedisClientOptions, createClient, SetOptions } from 'redis';
|
|
|
2
2
|
export { RedisClientOptions } from 'redis';
|
|
3
3
|
import { Logger } from 'winston';
|
|
4
4
|
import { UUID } from 'node:crypto';
|
|
5
|
-
import { add } from 'date-fns';
|
|
5
|
+
import { add, sub } from 'date-fns';
|
|
6
6
|
|
|
7
7
|
type GetType<T> = {
|
|
8
8
|
value: T;
|
|
@@ -304,6 +304,8 @@ declare const minutes: (num: number) => number;
|
|
|
304
304
|
declare const hours: (num: number) => number;
|
|
305
305
|
declare const days: (num: number) => number;
|
|
306
306
|
declare const weeks: (num: number) => number;
|
|
307
|
+
declare const today: (hours?: number, minutes?: number, seconds?: number) => Date;
|
|
308
|
+
declare const tomorrow: (hours?: number, minutes?: number, seconds?: number) => Date;
|
|
307
309
|
|
|
308
310
|
declare const time_DAY: typeof DAY;
|
|
309
311
|
declare const time_HOUR: typeof HOUR;
|
|
@@ -315,9 +317,12 @@ declare const time_days: typeof days;
|
|
|
315
317
|
declare const time_hours: typeof hours;
|
|
316
318
|
declare const time_minutes: typeof minutes;
|
|
317
319
|
declare const time_seconds: typeof seconds;
|
|
320
|
+
declare const time_sub: typeof sub;
|
|
321
|
+
declare const time_today: typeof today;
|
|
322
|
+
declare const time_tomorrow: typeof tomorrow;
|
|
318
323
|
declare const time_weeks: typeof weeks;
|
|
319
324
|
declare namespace time {
|
|
320
|
-
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_weeks as weeks };
|
|
325
|
+
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 };
|
|
321
326
|
}
|
|
322
327
|
|
|
323
328
|
export { type Cache, type CachingOptions, type IPersistor, InMemoryPersistor, Persistor, type PersistorConstructorType, PromiseCache, type PromiseCacheOptions, createCache, time };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { RedisClientOptions, createClient, SetOptions } from 'redis';
|
|
|
2
2
|
export { RedisClientOptions } from 'redis';
|
|
3
3
|
import { Logger } from 'winston';
|
|
4
4
|
import { UUID } from 'node:crypto';
|
|
5
|
-
import { add } from 'date-fns';
|
|
5
|
+
import { add, sub } from 'date-fns';
|
|
6
6
|
|
|
7
7
|
type GetType<T> = {
|
|
8
8
|
value: T;
|
|
@@ -304,6 +304,8 @@ declare const minutes: (num: number) => number;
|
|
|
304
304
|
declare const hours: (num: number) => number;
|
|
305
305
|
declare const days: (num: number) => number;
|
|
306
306
|
declare const weeks: (num: number) => number;
|
|
307
|
+
declare const today: (hours?: number, minutes?: number, seconds?: number) => Date;
|
|
308
|
+
declare const tomorrow: (hours?: number, minutes?: number, seconds?: number) => Date;
|
|
307
309
|
|
|
308
310
|
declare const time_DAY: typeof DAY;
|
|
309
311
|
declare const time_HOUR: typeof HOUR;
|
|
@@ -315,9 +317,12 @@ declare const time_days: typeof days;
|
|
|
315
317
|
declare const time_hours: typeof hours;
|
|
316
318
|
declare const time_minutes: typeof minutes;
|
|
317
319
|
declare const time_seconds: typeof seconds;
|
|
320
|
+
declare const time_sub: typeof sub;
|
|
321
|
+
declare const time_today: typeof today;
|
|
322
|
+
declare const time_tomorrow: typeof tomorrow;
|
|
318
323
|
declare const time_weeks: typeof weeks;
|
|
319
324
|
declare namespace time {
|
|
320
|
-
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_weeks as weeks };
|
|
325
|
+
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 };
|
|
321
326
|
}
|
|
322
327
|
|
|
323
328
|
export { type Cache, type CachingOptions, type IPersistor, InMemoryPersistor, Persistor, type PersistorConstructorType, PromiseCache, type PromiseCacheOptions, createCache, time };
|
package/dist/index.js
CHANGED
|
@@ -590,6 +590,9 @@ __export(time_exports, {
|
|
|
590
590
|
hours: () => hours,
|
|
591
591
|
minutes: () => minutes,
|
|
592
592
|
seconds: () => seconds,
|
|
593
|
+
sub: () => import_date_fns.sub,
|
|
594
|
+
today: () => today,
|
|
595
|
+
tomorrow: () => tomorrow,
|
|
593
596
|
weeks: () => weeks
|
|
594
597
|
});
|
|
595
598
|
var import_date_fns = require("date-fns");
|
|
@@ -603,6 +606,19 @@ var minutes = (num) => num * MINUTE;
|
|
|
603
606
|
var hours = (num) => num * HOUR;
|
|
604
607
|
var days = (num) => num * DAY;
|
|
605
608
|
var weeks = (num) => num * WEEK;
|
|
609
|
+
var today = (hours2 = 0, minutes2 = 0, seconds2 = 0) => {
|
|
610
|
+
const local = /* @__PURE__ */ new Date();
|
|
611
|
+
const utc = Date.UTC(
|
|
612
|
+
local.getUTCFullYear(),
|
|
613
|
+
local.getUTCMonth(),
|
|
614
|
+
local.getUTCDate(),
|
|
615
|
+
hours2,
|
|
616
|
+
minutes2,
|
|
617
|
+
seconds2
|
|
618
|
+
);
|
|
619
|
+
return new Date(utc);
|
|
620
|
+
};
|
|
621
|
+
var tomorrow = (hours2 = 0, minutes2 = 0, seconds2 = 0) => (0, import_date_fns.add)(today(hours2, minutes2, seconds2), { days: 1 });
|
|
606
622
|
// Annotate the CommonJS export names for ESM import in node:
|
|
607
623
|
0 && (module.exports = {
|
|
608
624
|
InMemoryPersistor,
|
package/dist/index.mjs
CHANGED
|
@@ -572,9 +572,12 @@ __export(time_exports, {
|
|
|
572
572
|
hours: () => hours,
|
|
573
573
|
minutes: () => minutes,
|
|
574
574
|
seconds: () => seconds,
|
|
575
|
+
sub: () => sub,
|
|
576
|
+
today: () => today,
|
|
577
|
+
tomorrow: () => tomorrow,
|
|
575
578
|
weeks: () => weeks
|
|
576
579
|
});
|
|
577
|
-
import { add } from "date-fns";
|
|
580
|
+
import { add, sub } from "date-fns";
|
|
578
581
|
var SECOND = 1e3;
|
|
579
582
|
var MINUTE = 60 * SECOND;
|
|
580
583
|
var HOUR = 60 * MINUTE;
|
|
@@ -585,6 +588,19 @@ var minutes = (num) => num * MINUTE;
|
|
|
585
588
|
var hours = (num) => num * HOUR;
|
|
586
589
|
var days = (num) => num * DAY;
|
|
587
590
|
var weeks = (num) => num * WEEK;
|
|
591
|
+
var today = (hours2 = 0, minutes2 = 0, seconds2 = 0) => {
|
|
592
|
+
const local = /* @__PURE__ */ new Date();
|
|
593
|
+
const utc = Date.UTC(
|
|
594
|
+
local.getUTCFullYear(),
|
|
595
|
+
local.getUTCMonth(),
|
|
596
|
+
local.getUTCDate(),
|
|
597
|
+
hours2,
|
|
598
|
+
minutes2,
|
|
599
|
+
seconds2
|
|
600
|
+
);
|
|
601
|
+
return new Date(utc);
|
|
602
|
+
};
|
|
603
|
+
var tomorrow = (hours2 = 0, minutes2 = 0, seconds2 = 0) => add(today(hours2, minutes2, seconds2), { days: 1 });
|
|
588
604
|
export {
|
|
589
605
|
InMemoryPersistor,
|
|
590
606
|
Persistor,
|