@daiso-tech/core 0.26.1 → 0.28.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 -3
- package/dist/async/utilities/delay/delay.d.ts +1 -12
- package/dist/async/utilities/delay/delay.js +1 -12
- package/dist/async/utilities/delay/delay.js.map +1 -1
- package/dist/async/utilities/lazy-promise/lazy-promise.d.ts +143 -71
- package/dist/async/utilities/lazy-promise/lazy-promise.js +223 -142
- package/dist/async/utilities/lazy-promise/lazy-promise.js.map +1 -1
- package/dist/async/utilities/retry/retry-or-fail.d.ts +2 -0
- package/dist/async/utilities/retry/retry-or-fail.js.map +1 -1
- package/dist/cache/contracts/_module-exports.d.ts +1 -0
- package/dist/cache/contracts/_module-exports.js +1 -0
- package/dist/cache/contracts/_module-exports.js.map +1 -1
- package/dist/cache/contracts/cache-adapter.contract.d.ts +19 -21
- package/dist/cache/contracts/cache-adapter.contract.js +1 -4
- package/dist/cache/contracts/cache-adapter.contract.js.map +1 -1
- package/dist/cache/contracts/cache-factory.contract.d.ts +1 -1
- package/dist/cache/contracts/cache.contract.d.ts +144 -96
- package/dist/cache/contracts/cache.contract.js +1 -2
- package/dist/cache/contracts/cache.contract.js.map +1 -1
- package/dist/cache/contracts/cache.errors.js +2 -5
- package/dist/cache/contracts/cache.errors.js.map +1 -1
- package/dist/cache/contracts/cache.events.d.ts +10 -10
- package/dist/cache/contracts/cache.events.js +2 -5
- package/dist/cache/contracts/cache.events.js.map +1 -1
- package/dist/cache/contracts/database-cache-adapter.contract.d.ts +89 -0
- package/dist/cache/contracts/database-cache-adapter.contract.js +5 -0
- package/dist/cache/contracts/database-cache-adapter.contract.js.map +1 -0
- package/dist/cache/implementations/adapters/{kysely-sqlite-cache-adapter → kysely-cache-adapter}/_module.d.ts +1 -1
- package/dist/cache/implementations/adapters/kysely-cache-adapter/_module.js +2 -0
- package/dist/cache/implementations/adapters/kysely-cache-adapter/_module.js.map +1 -0
- package/dist/cache/implementations/adapters/kysely-cache-adapter/kysely-cache-adapter.d.ts +73 -0
- package/dist/cache/implementations/adapters/kysely-cache-adapter/kysely-cache-adapter.js +224 -0
- package/dist/cache/implementations/adapters/kysely-cache-adapter/kysely-cache-adapter.js.map +1 -0
- package/dist/cache/implementations/adapters/libsql-cache-adapter/libsql-cache-adapter.d.ts +29 -39
- package/dist/cache/implementations/adapters/libsql-cache-adapter/libsql-cache-adapter.js +45 -54
- package/dist/cache/implementations/adapters/libsql-cache-adapter/libsql-cache-adapter.js.map +1 -1
- package/dist/cache/implementations/adapters/memory-cache-adapter/memory-cache-adapter.d.ts +17 -33
- package/dist/cache/implementations/adapters/memory-cache-adapter/memory-cache-adapter.js +43 -52
- package/dist/cache/implementations/adapters/memory-cache-adapter/memory-cache-adapter.js.map +1 -1
- package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter-serde.d.ts +13 -0
- package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter-serde.js +38 -0
- package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter-serde.js.map +1 -0
- package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter.d.ts +27 -33
- package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter.js +145 -197
- package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter.js.map +1 -1
- package/dist/cache/implementations/adapters/no-op-cache-adapter/no-op-cache-adapter.d.ts +14 -13
- package/dist/cache/implementations/adapters/no-op-cache-adapter/no-op-cache-adapter.js +19 -17
- package/dist/cache/implementations/adapters/no-op-cache-adapter/no-op-cache-adapter.js.map +1 -1
- package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter-serde.d.ts +13 -0
- package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter-serde.js +39 -0
- package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter-serde.js.map +1 -0
- package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter.d.ts +16 -22
- package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter.js +51 -132
- package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter.js.map +1 -1
- package/dist/cache/implementations/adapters/redis-cache-adapter/utilities.d.ts +13 -0
- package/dist/cache/implementations/adapters/redis-cache-adapter/utilities.js +69 -0
- package/dist/cache/implementations/adapters/redis-cache-adapter/utilities.js.map +1 -0
- package/dist/cache/implementations/adapters/sqlite-cache-adapter/sqlite-cache-adapter.d.ts +29 -41
- package/dist/cache/implementations/adapters/sqlite-cache-adapter/sqlite-cache-adapter.js +46 -55
- package/dist/cache/implementations/adapters/sqlite-cache-adapter/sqlite-cache-adapter.js.map +1 -1
- package/dist/cache/implementations/derivables/_module-exports.d.ts +1 -1
- package/dist/cache/implementations/derivables/_module-exports.js +1 -1
- package/dist/cache/implementations/derivables/_module-exports.js.map +1 -1
- package/dist/cache/implementations/derivables/cache/_module.d.ts +1 -0
- package/dist/cache/implementations/derivables/cache/_module.js +1 -0
- package/dist/cache/implementations/derivables/cache/_module.js.map +1 -1
- package/dist/cache/implementations/derivables/cache/cache.d.ts +269 -507
- package/dist/cache/implementations/derivables/cache/cache.js +538 -822
- package/dist/cache/implementations/derivables/cache/cache.js.map +1 -1
- package/dist/cache/implementations/derivables/cache/database-cache-adapter.d.ts +21 -0
- package/dist/cache/implementations/derivables/cache/database-cache-adapter.js +110 -0
- package/dist/cache/implementations/derivables/cache/database-cache-adapter.js.map +1 -0
- package/dist/cache/implementations/derivables/cache/is-database-cache-adapter.d.ts +9 -0
- package/dist/cache/implementations/derivables/cache/is-database-cache-adapter.js +20 -0
- package/dist/cache/implementations/derivables/cache/is-database-cache-adapter.js.map +1 -0
- package/dist/cache/implementations/derivables/cache-factory/cache-factory.d.ts +69 -105
- package/dist/cache/implementations/derivables/cache-factory/cache-factory.js +117 -87
- package/dist/cache/implementations/derivables/cache-factory/cache-factory.js.map +1 -1
- package/dist/cache/implementations/test-utilities/_module-exports.d.ts +1 -0
- package/dist/cache/implementations/test-utilities/_module-exports.js +1 -0
- package/dist/cache/implementations/test-utilities/_module-exports.js.map +1 -1
- package/dist/cache/implementations/test-utilities/cache-adapter.test-suite.d.ts +9 -11
- package/dist/cache/implementations/test-utilities/cache-adapter.test-suite.js +246 -247
- package/dist/cache/implementations/test-utilities/cache-adapter.test-suite.js.map +1 -1
- package/dist/cache/implementations/test-utilities/cache.test-suite.d.ts +16 -30
- package/dist/cache/implementations/test-utilities/cache.test-suite.js +271 -996
- package/dist/cache/implementations/test-utilities/cache.test-suite.js.map +1 -1
- package/dist/cache/implementations/test-utilities/database-cache-dapter.test-suite.d.ts +60 -0
- package/dist/cache/implementations/test-utilities/database-cache-dapter.test-suite.js +698 -0
- package/dist/cache/implementations/test-utilities/database-cache-dapter.test-suite.js.map +1 -0
- package/dist/collection/contracts/async-collection.contract.d.ts +2 -3
- package/dist/collection/contracts/async-collection.contract.js +1 -12
- package/dist/collection/contracts/async-collection.contract.js.map +1 -1
- package/dist/collection/contracts/collection.contract.d.ts +1 -1
- package/dist/collection/contracts/collection.errors.d.ts +1 -1
- package/dist/collection/contracts/collection.errors.js +3 -6
- package/dist/collection/contracts/collection.errors.js.map +1 -1
- package/dist/collection/implementations/_shared.d.ts +4 -4
- package/dist/collection/implementations/_shared.js +6 -9
- package/dist/collection/implementations/_shared.js.map +1 -1
- package/dist/collection/implementations/async-iterable-collection/_shared/async-delay-iterable.js +2 -2
- package/dist/collection/implementations/async-iterable-collection/_shared/async-delay-iterable.js.map +1 -1
- package/dist/collection/implementations/async-iterable-collection/async-iterable-collection.d.ts +5 -21
- package/dist/collection/implementations/async-iterable-collection/async-iterable-collection.js +12 -8
- package/dist/collection/implementations/async-iterable-collection/async-iterable-collection.js.map +1 -1
- package/dist/collection/implementations/iterable-collection/iterable-collection.d.ts +1 -1
- package/dist/collection/implementations/iterable-collection/iterable-collection.js +1 -1
- package/dist/collection/implementations/list-collection/list-collection.d.ts +1 -1
- package/dist/collection/implementations/list-collection/list-collection.js +1 -1
- package/dist/event-bus/contracts/event-bus-adapter.contract.d.ts +0 -9
- package/dist/event-bus/contracts/event-bus.contract.d.ts +5 -5
- package/dist/event-bus/contracts/event-bus.contract.js.map +1 -1
- package/dist/event-bus/contracts/event-bus.errors.js +2 -5
- package/dist/event-bus/contracts/event-bus.errors.js.map +1 -1
- package/dist/event-bus/implementations/adapters/memory-event-bus-adapter/memory-event-bus-adapter.d.ts +9 -29
- package/dist/event-bus/implementations/adapters/memory-event-bus-adapter/memory-event-bus-adapter.js +14 -35
- package/dist/event-bus/implementations/adapters/memory-event-bus-adapter/memory-event-bus-adapter.js.map +1 -1
- package/dist/event-bus/implementations/adapters/no-op-event-bus-adapter/no-op-event-bus-adapter.d.ts +2 -4
- package/dist/event-bus/implementations/adapters/no-op-event-bus-adapter/no-op-event-bus-adapter.js +2 -8
- package/dist/event-bus/implementations/adapters/no-op-event-bus-adapter/no-op-event-bus-adapter.js.map +1 -1
- package/dist/event-bus/implementations/adapters/redis-pub-sub-event-bus-adapter/redis-pub-sub-event-bus-adapter.d.ts +8 -15
- package/dist/event-bus/implementations/adapters/redis-pub-sub-event-bus-adapter/redis-pub-sub-event-bus-adapter.js +15 -34
- package/dist/event-bus/implementations/adapters/redis-pub-sub-event-bus-adapter/redis-pub-sub-event-bus-adapter.js.map +1 -1
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.d.ts +85 -289
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.js +128 -325
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.js.map +1 -1
- package/dist/event-bus/implementations/derivables/event-bus/listener-store.d.ts +13 -0
- package/dist/event-bus/implementations/derivables/event-bus/listener-store.js +42 -0
- package/dist/event-bus/implementations/derivables/event-bus/listener-store.js.map +1 -0
- package/dist/event-bus/implementations/derivables/event-bus-factory/event-bus-factory.d.ts +103 -86
- package/dist/event-bus/implementations/derivables/event-bus-factory/event-bus-factory.js +135 -77
- package/dist/event-bus/implementations/derivables/event-bus-factory/event-bus-factory.js.map +1 -1
- package/dist/event-bus/implementations/test-utilities/event-bus-adapter.test-suite.d.ts +2 -48
- package/dist/event-bus/implementations/test-utilities/event-bus-adapter.test-suite.js +31 -122
- package/dist/event-bus/implementations/test-utilities/event-bus-adapter.test-suite.js.map +1 -1
- package/dist/event-bus/implementations/test-utilities/event-bus.test-suite.d.ts +2 -50
- package/dist/event-bus/implementations/test-utilities/event-bus.test-suite.js +41 -89
- package/dist/event-bus/implementations/test-utilities/event-bus.test-suite.js.map +1 -1
- package/dist/lock/contracts/database-lock-adapter.contract.d.ts +0 -9
- package/dist/lock/contracts/lock-adapter.contract.d.ts +0 -9
- package/dist/lock/contracts/lock-provider.contract.d.ts +20 -6
- package/dist/lock/contracts/lock.contract.d.ts +37 -8
- package/dist/lock/contracts/lock.errors.js +2 -5
- package/dist/lock/contracts/lock.errors.js.map +1 -1
- package/dist/lock/contracts/lock.events.js +2 -5
- package/dist/lock/contracts/lock.events.js.map +1 -1
- package/dist/lock/implementations/adapters/_module-exports.d.ts +1 -0
- package/dist/lock/implementations/adapters/_module-exports.js +1 -0
- package/dist/lock/implementations/adapters/_module-exports.js.map +1 -1
- package/dist/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.d.ts +0 -5
- package/dist/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.js +2 -21
- package/dist/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/adapters/libsql-lock-adapter/libsql-lock-adapter.d.ts +11 -16
- package/dist/lock/implementations/adapters/libsql-lock-adapter/libsql-lock-adapter.js +11 -20
- package/dist/lock/implementations/adapters/libsql-lock-adapter/libsql-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.d.ts +7 -26
- package/dist/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.js +7 -35
- package/dist/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.d.ts +13 -20
- package/dist/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.js +13 -37
- package/dist/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/adapters/no-op-lock-adapter/_module.d.ts +1 -0
- package/dist/lock/implementations/adapters/no-op-lock-adapter/_module.js +2 -0
- package/dist/lock/implementations/adapters/no-op-lock-adapter/_module.js.map +1 -0
- package/dist/lock/implementations/adapters/no-op-lock-adapter/no-op-lock-adapter.d.ts +17 -0
- package/dist/lock/implementations/adapters/no-op-lock-adapter/no-op-lock-adapter.js +24 -0
- package/dist/lock/implementations/adapters/no-op-lock-adapter/no-op-lock-adapter.js.map +1 -0
- package/dist/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.d.ts +4 -22
- package/dist/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.js +4 -31
- package/dist/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/adapters/sqlite-lock-adapter/sqlite-lock-adapter.d.ts +14 -20
- package/dist/lock/implementations/adapters/sqlite-lock-adapter/sqlite-lock-adapter.js +12 -21
- package/dist/lock/implementations/adapters/sqlite-lock-adapter/sqlite-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/database-lock-adapter.d.ts +0 -2
- package/dist/lock/implementations/derivables/lock-provider/database-lock-adapter.js +2 -8
- package/dist/lock/implementations/derivables/lock-provider/database-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/is-database-lock-adapter.js +1 -5
- package/dist/lock/implementations/derivables/lock-provider/is-database-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock-provider.d.ts +181 -369
- package/dist/lock/implementations/derivables/lock-provider/lock-provider.js +259 -407
- package/dist/lock/implementations/derivables/lock-provider/lock-provider.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock-serde-transformer.d.ts +15 -17
- package/dist/lock/implementations/derivables/lock-provider/lock-serde-transformer.js +42 -40
- package/dist/lock/implementations/derivables/lock-provider/lock-serde-transformer.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock-state.d.ts +3 -3
- package/dist/lock/implementations/derivables/lock-provider/lock-state.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock.d.ts +194 -680
- package/dist/lock/implementations/derivables/lock-provider/lock.js +284 -744
- package/dist/lock/implementations/derivables/lock-provider/lock.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.d.ts +106 -143
- package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.js +173 -101
- package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.js.map +1 -1
- package/dist/lock/implementations/test-utilities/database-lock-adapter.test-suite.d.ts +4 -5
- package/dist/lock/implementations/test-utilities/database-lock-adapter.test-suite.js +172 -247
- package/dist/lock/implementations/test-utilities/database-lock-adapter.test-suite.js.map +1 -1
- package/dist/lock/implementations/test-utilities/lock-adapter.test-suite.d.ts +5 -8
- package/dist/lock/implementations/test-utilities/lock-adapter.test-suite.js +93 -98
- package/dist/lock/implementations/test-utilities/lock-adapter.test-suite.js.map +1 -1
- package/dist/lock/implementations/test-utilities/lock-provider.test-suite.d.ts +15 -20
- package/dist/lock/implementations/test-utilities/lock-provider.test-suite.js +295 -145
- package/dist/lock/implementations/test-utilities/lock-provider.test-suite.js.map +1 -1
- package/dist/serde/implementations/adapters/mongodb-serde/mongodb-serde.js +1 -2
- package/dist/serde/implementations/adapters/mongodb-serde/mongodb-serde.js.map +1 -1
- package/dist/serde/implementations/adapters/no-op-serde-adapter/no-op-serde-adapter.d.ts +3 -2
- package/dist/serde/implementations/adapters/no-op-serde-adapter/no-op-serde-adapter.js +2 -1
- package/dist/serde/implementations/adapters/no-op-serde-adapter/no-op-serde-adapter.js.map +1 -1
- package/dist/serde/implementations/adapters/redis-serde/redis-serde.js +1 -2
- package/dist/serde/implementations/adapters/redis-serde/redis-serde.js.map +1 -1
- package/dist/serde/implementations/adapters/sql-serde/sql-serde.js +1 -2
- package/dist/serde/implementations/adapters/sql-serde/sql-serde.js.map +1 -1
- package/dist/serde/implementations/adapters/super-json-serde-adapter/super-json-serde-adapter.d.ts +2 -2
- package/dist/serde/implementations/adapters/super-json-serde-adapter/super-json-serde-adapter.js +2 -2
- package/dist/serde/implementations/adapters/super-json-serde-adapter/super-json-serde-adapter.js.map +1 -1
- package/dist/serde/implementations/derivables/serde.d.ts +14 -14
- package/dist/serde/implementations/derivables/serde.js +14 -15
- package/dist/serde/implementations/derivables/serde.js.map +1 -1
- package/dist/serde/implementations/test-utilities/flexible-serde-adapter.test-suite.d.ts +4 -4
- package/dist/serde/implementations/test-utilities/flexible-serde-adapter.test-suite.js +3 -3
- package/dist/serde/implementations/test-utilities/flexible-serde.test-suite.d.ts +5 -5
- package/dist/serde/implementations/test-utilities/flexible-serde.test-suite.js +4 -4
- package/dist/utilities/_module-exports.d.ts +2 -3
- package/dist/utilities/_module-exports.js +2 -3
- package/dist/utilities/_module-exports.js.map +1 -1
- package/dist/utilities/classes/_module.d.ts +4 -0
- package/dist/utilities/classes/_module.js +5 -0
- package/dist/utilities/classes/_module.js.map +1 -0
- package/dist/utilities/classes/key-prefixer/_module.d.ts +2 -0
- package/dist/utilities/classes/key-prefixer/_module.js +3 -0
- package/dist/utilities/classes/key-prefixer/_module.js.map +1 -0
- package/dist/utilities/classes/key-prefixer/key-prefixer.contract.d.ts +28 -0
- package/dist/utilities/classes/key-prefixer/key-prefixer.contract.js +5 -0
- package/dist/utilities/classes/key-prefixer/key-prefixer.contract.js.map +1 -0
- package/dist/utilities/classes/key-prefixer/key-prefixer.d.ts +73 -0
- package/dist/utilities/classes/key-prefixer/key-prefixer.js +135 -0
- package/dist/utilities/classes/key-prefixer/key-prefixer.js.map +1 -0
- package/dist/utilities/classes/kysely-table-name-transformer-plugin/_module.d.ts +1 -0
- package/dist/utilities/classes/kysely-table-name-transformer-plugin/_module.js +2 -0
- package/dist/utilities/classes/kysely-table-name-transformer-plugin/_module.js.map +1 -0
- package/dist/utilities/classes/kysely-table-name-transformer-plugin/kysely-table-name-transformer-plugin.js.map +1 -0
- package/dist/utilities/classes/pipeline/_module.d.ts +1 -0
- package/dist/utilities/classes/pipeline/_module.js +2 -0
- package/dist/utilities/classes/pipeline/_module.js.map +1 -0
- package/dist/utilities/{pipeline → classes/pipeline}/pipeline.d.ts +3 -2
- package/dist/utilities/{pipeline → classes/pipeline}/pipeline.js +3 -2
- package/dist/utilities/classes/pipeline/pipeline.js.map +1 -0
- package/dist/utilities/classes/time-span/_module.d.ts +1 -0
- package/dist/utilities/classes/time-span/_module.js +2 -0
- package/dist/utilities/classes/time-span/_module.js.map +1 -0
- package/dist/utilities/{time-span → classes/time-span}/time-span.d.ts +2 -1
- package/dist/utilities/{time-span → classes/time-span}/time-span.js +1 -0
- package/dist/utilities/classes/time-span/time-span.js.map +1 -0
- package/dist/utilities/{_constants.d.ts → constants.d.ts} +0 -3
- package/dist/utilities/constants.js +5 -0
- package/dist/utilities/constants.js.map +1 -0
- package/dist/utilities/contracts/_module.d.ts +3 -1
- package/dist/utilities/contracts/_module.js +3 -1
- package/dist/utilities/contracts/_module.js.map +1 -1
- package/dist/utilities/contracts/prunable.contract.d.ts +11 -0
- package/dist/utilities/contracts/prunable.contract.js +5 -0
- package/dist/utilities/contracts/prunable.contract.js.map +1 -0
- package/dist/utilities/contracts/sqlite-database.contract.d.ts +26 -0
- package/dist/utilities/contracts/sqlite-database.contract.js +5 -0
- package/dist/utilities/contracts/sqlite-database.contract.js.map +1 -0
- package/dist/utilities/functions.d.ts +34 -2
- package/dist/utilities/functions.js +72 -4
- package/dist/utilities/functions.js.map +1 -1
- package/dist/utilities/types.d.ts +49 -16
- package/package.json +14 -13
- package/dist/cache/implementations/adapters/kysely-sqlite-cache-adapter/_module.js +0 -2
- package/dist/cache/implementations/adapters/kysely-sqlite-cache-adapter/_module.js.map +0 -1
- package/dist/cache/implementations/adapters/kysely-sqlite-cache-adapter/kysely-sqlite-cache-adapter.d.ts +0 -61
- package/dist/cache/implementations/adapters/kysely-sqlite-cache-adapter/kysely-sqlite-cache-adapter.js +0 -318
- package/dist/cache/implementations/adapters/kysely-sqlite-cache-adapter/kysely-sqlite-cache-adapter.js.map +0 -1
- package/dist/utilities/_constants.js +0 -8
- package/dist/utilities/_constants.js.map +0 -1
- package/dist/utilities/kysely/_module.d.ts +0 -1
- package/dist/utilities/kysely/_module.js +0 -2
- package/dist/utilities/kysely/_module.js.map +0 -1
- package/dist/utilities/kysely/kysely-table-name-transformer-plugin.js.map +0 -1
- package/dist/utilities/pipeline/_module.d.ts +0 -1
- package/dist/utilities/pipeline/_module.js +0 -2
- package/dist/utilities/pipeline/_module.js.map +0 -1
- package/dist/utilities/pipeline/pipeline.js.map +0 -1
- package/dist/utilities/time-span/_module.d.ts +0 -1
- package/dist/utilities/time-span/_module.js +0 -2
- package/dist/utilities/time-span/_module.js.map +0 -1
- package/dist/utilities/time-span/time-span.js.map +0 -1
- /package/dist/utilities/{kysely → classes/kysely-table-name-transformer-plugin}/kysely-table-name-transformer-plugin.d.ts +0 -0
- /package/dist/utilities/{kysely → classes/kysely-table-name-transformer-plugin}/kysely-table-name-transformer-plugin.js +0 -0
|
@@ -2,369 +2,520 @@
|
|
|
2
2
|
* @module Cache
|
|
3
3
|
*/
|
|
4
4
|
import { KeyFoundCacheEvent, KeyNotFoundCacheEvent, KeyAddedCacheEvent, KeyUpdatedCacheEvent, KeyRemovedCacheEvent, KeyIncrementedCacheEvent, KeyDecrementedCacheEvent, KeysClearedCacheEvent, UnexpectedCacheErrorEvent, } from "../../../../cache/contracts/_module-exports.js";
|
|
5
|
-
import { KeyNotFoundCacheError, TypeCacheError,
|
|
5
|
+
import { KeyNotFoundCacheError, TypeCacheError, } from "../../../../cache/contracts/_module-exports.js";
|
|
6
6
|
import {} from "../../../../cache/contracts/_module-exports.js";
|
|
7
|
-
import {
|
|
7
|
+
import { isFactory, resolveAsyncLazyable, resolveFactoryable, } from "../../../../utilities/_module-exports.js";
|
|
8
|
+
import {} from "../../../../utilities/_module-exports.js";
|
|
9
|
+
import { KeyPrefixer, } from "../../../../utilities/_module-exports.js";
|
|
8
10
|
import { LazyPromise } from "../../../../async/_module-exports.js";
|
|
9
11
|
import { EventBus } from "../../../../event-bus/implementations/derivables/_module-exports.js";
|
|
10
12
|
import { MemoryEventBusAdapter } from "../../../../event-bus/implementations/adapters/_module-exports.js";
|
|
13
|
+
import { isDatabaseCacheAdapter } from "../../../../cache/implementations/derivables/cache/is-database-cache-adapter.js";
|
|
14
|
+
import { DatabaseCacheAdapter } from "../../../../cache/implementations/derivables/cache/database-cache-adapter.js";
|
|
11
15
|
/**
|
|
12
|
-
* <i>Cache</i> class can be derived from any <i>{@link ICacheAdapter}</i>.
|
|
13
16
|
*
|
|
14
|
-
* IMPORT_PATH: ```"@daiso-tech/core/cache
|
|
17
|
+
* IMPORT_PATH: ```"@daiso-tech/core/cache"```
|
|
15
18
|
* @group Derivables
|
|
16
19
|
*/
|
|
17
20
|
export class Cache {
|
|
21
|
+
static resolveCacheAdapter(adapter) {
|
|
22
|
+
if (isDatabaseCacheAdapter(adapter)) {
|
|
23
|
+
return new DatabaseCacheAdapter(adapter);
|
|
24
|
+
}
|
|
25
|
+
return adapter;
|
|
26
|
+
}
|
|
27
|
+
static async resolveCacheAdapterFactoryable(factoryable, rootPrefix) {
|
|
28
|
+
const adapter = await resolveFactoryable(factoryable, rootPrefix);
|
|
29
|
+
return Cache.resolveCacheAdapter(adapter);
|
|
30
|
+
}
|
|
18
31
|
static defaultRetryPolicy = (error) => {
|
|
19
32
|
return !(error instanceof TypeCacheError ||
|
|
20
33
|
error instanceof KeyNotFoundCacheError);
|
|
21
34
|
};
|
|
22
35
|
groupdEventBus;
|
|
23
36
|
eventBus;
|
|
24
|
-
|
|
37
|
+
adapterFactoryable;
|
|
38
|
+
adapterPromise;
|
|
25
39
|
defaultTtl;
|
|
26
40
|
retryAttempts;
|
|
27
41
|
backoffPolicy;
|
|
28
42
|
retryPolicy;
|
|
29
|
-
|
|
43
|
+
retryTimeout;
|
|
44
|
+
totalTimeout;
|
|
45
|
+
keyPrefixer;
|
|
30
46
|
/**
|
|
47
|
+
*
|
|
31
48
|
* @example
|
|
32
49
|
* ```ts
|
|
33
|
-
* import
|
|
34
|
-
* import {
|
|
35
|
-
* import {
|
|
50
|
+
* import { SqliteCacheAdapter } from "@daiso-tech/core/cache/adapters";
|
|
51
|
+
* import { Serde } from "@daiso-tech/core/serde";
|
|
52
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/adapters"
|
|
53
|
+
* import Sqlite from "better-sqlite3";
|
|
54
|
+
* import { Cache } from "@daiso-tech/core/cache";
|
|
55
|
+
* import { KeyPrefixer } from "@daiso-tech/core/utilities";
|
|
56
|
+
*
|
|
57
|
+
* const database = new Sqlite("local.db");
|
|
58
|
+
* const serde = new Serde(new SuperJsonSerdeAdapter());
|
|
59
|
+
* const cacheAdapter = new SqliteCacheAdapter({
|
|
60
|
+
* database,
|
|
61
|
+
* serde,
|
|
62
|
+
* });
|
|
63
|
+
* // You need initialize the adapter once before using it.
|
|
64
|
+
* await cacheAdapter.init();
|
|
36
65
|
*
|
|
37
|
-
* const cache
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
66
|
+
* const cache = new Cache({
|
|
67
|
+
* keyPrefixer: new KeyPrefixer("cache"),
|
|
68
|
+
* adapter: cacheAdapter,
|
|
69
|
+
* });
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* You can pass factory function that will create an adapter for every group.
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* import { SqliteCacheAdapter } from "@daiso-tech/core/cache/adapters";
|
|
76
|
+
* import type { ICacheAdapter } from "@daiso-tech/core/cache/contracts";
|
|
77
|
+
* import { Serde } from "@daiso-tech/core/serde";
|
|
78
|
+
* import type { ISerde } from "@daiso-tech/core/serde/contracts";
|
|
79
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/adapters"
|
|
80
|
+
* import Sqlite from "better-sqlite3";
|
|
81
|
+
* import { Cache } from "@daiso-tech/core/cache";
|
|
82
|
+
* import { KeyPrefixer, type ISqliteDatabase, type FactoryFn } from "@daiso-tech/core/utilities";
|
|
83
|
+
*
|
|
84
|
+
* function cahceAdapterFactory(database: ISqliteDatabase, serde: ISerde<string>): FactoryFn<string, ICacheAdapter> {
|
|
85
|
+
* return async (prefix) => {
|
|
86
|
+
* const cacheAdapter = new SqliteCacheAdapter({
|
|
87
|
+
* database,
|
|
88
|
+
* serde,
|
|
89
|
+
* tableName: `cache_${prefix}`
|
|
90
|
+
* });
|
|
91
|
+
* await cacheAdapter.init();
|
|
92
|
+
* return cacheAdapter;
|
|
93
|
+
* }
|
|
94
|
+
* }
|
|
95
|
+
*
|
|
96
|
+
* const database = new Sqlite("local.db");
|
|
97
|
+
* const serde = new Serde(new SuperJsonSerdeAdapter());
|
|
98
|
+
* const cache = new Cache({
|
|
99
|
+
* keyPrefixer: new KeyPrefixer("cache"),
|
|
100
|
+
* adapter: cahceAdapterFactory(database, serde),
|
|
101
|
+
* });
|
|
102
|
+
* ```
|
|
103
|
+
*
|
|
104
|
+
* You can also pass factory object that implements <i>{@link IFactoryObject}</i> contract. This useful for depedency injection libraries.
|
|
105
|
+
* @example
|
|
106
|
+
* ```ts
|
|
107
|
+
* import { SqliteCacheAdapter } from "@daiso-tech/core/cache/adapters";
|
|
108
|
+
* import type { ICacheAdapter } from "@daiso-tech/core/cache/contracts";
|
|
109
|
+
* import { Serde } from "@daiso-tech/core/serde";
|
|
110
|
+
* import type { ISerde } from "@daiso-tech/core/serde/contracts";
|
|
111
|
+
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/adapters"
|
|
112
|
+
* import Sqlite from "better-sqlite3";
|
|
113
|
+
* import { Cache } from "@daiso-tech/core/cache";
|
|
114
|
+
* import { KeyPrefixer, type ISqliteDatabase, type IFactoryObject, type Promiseable } from "@daiso-tech/core/utilities";
|
|
115
|
+
*
|
|
116
|
+
* class CahceAdapterFactory implements IFactoryObject<string, ICacheAdapter> {
|
|
117
|
+
* constructor(private readonly database: ISqliteDatabase, private readonly serde: ISerde<string>) {}
|
|
118
|
+
*
|
|
119
|
+
* async use(prefix: string): Promiseable<ICacheAdapter> {
|
|
120
|
+
* const cacheAdapter = new SqliteCacheAdapter({
|
|
121
|
+
* database: this.database,
|
|
122
|
+
* serde: this.serde,
|
|
123
|
+
* tableName: `cache_${prefix}`
|
|
124
|
+
* });
|
|
125
|
+
* await cacheAdapter.init();
|
|
126
|
+
* return cacheAdapter;
|
|
127
|
+
* }
|
|
128
|
+
* }
|
|
129
|
+
*
|
|
130
|
+
* const database = new Sqlite("local.db");
|
|
131
|
+
* const serde = new Serde(new SuperJsonSerdeAdapter());
|
|
132
|
+
* const cache = new Cache({
|
|
133
|
+
* keyPrefixer: new KeyPrefixer("cache"),
|
|
134
|
+
* adapter: new CahceAdapterFactory(database, serde),
|
|
41
135
|
* });
|
|
42
136
|
* ```
|
|
43
137
|
*/
|
|
44
138
|
constructor(settings) {
|
|
45
|
-
const { adapter, eventBus: groupdEventBus = new EventBus({
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
139
|
+
const { keyPrefixer, adapter, eventBus: groupdEventBus = new EventBus({
|
|
140
|
+
keyPrefixer: new KeyPrefixer("event-bus"),
|
|
141
|
+
adapter: new MemoryEventBusAdapter(),
|
|
142
|
+
}), defaultTtl = null, retryAttempts = null, backoffPolicy = null, retryPolicy = Cache.defaultRetryPolicy, retryTimeout = null, totalTimeout = null, } = settings;
|
|
143
|
+
this.keyPrefixer = keyPrefixer;
|
|
50
144
|
this.groupdEventBus = groupdEventBus;
|
|
51
|
-
this.
|
|
52
|
-
this.adapter = adapter;
|
|
145
|
+
this.adapterFactoryable = adapter;
|
|
53
146
|
this.defaultTtl = defaultTtl;
|
|
54
147
|
this.retryAttempts = retryAttempts;
|
|
55
148
|
this.backoffPolicy = backoffPolicy;
|
|
56
149
|
this.retryPolicy = retryPolicy;
|
|
57
|
-
this.
|
|
150
|
+
this.retryTimeout = retryTimeout;
|
|
151
|
+
this.totalTimeout = totalTimeout;
|
|
152
|
+
this.eventBus = this.groupdEventBus.withGroup(this.keyPrefixer.resolvedRootPrefix);
|
|
153
|
+
if (this.keyPrefixer.resolvedGroup) {
|
|
154
|
+
this.eventBus = this.groupdEventBus.withGroup([
|
|
155
|
+
this.keyPrefixer.resolvedRootPrefix,
|
|
156
|
+
this.keyPrefixer.resolvedGroup,
|
|
157
|
+
]);
|
|
158
|
+
}
|
|
159
|
+
this.adapterPromise = new LazyPromise(() => Cache.resolveCacheAdapterFactoryable(this.adapterFactoryable, this.keyPrefixer.keyPrefix));
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* You can listen to the following <i>{@link CacheEvents}</i> of the <i>{@link ICache}</i> instance.
|
|
163
|
+
* To understand how this method works, refer to <i>{@link IEventListenable}</i>.
|
|
164
|
+
*/
|
|
165
|
+
addListener(event, listener) {
|
|
166
|
+
return this.eventBus.addListener(event, listener);
|
|
58
167
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
168
|
+
/**
|
|
169
|
+
* You can listen to the following <i>{@link CacheEvents}</i> of the <i>{@link ICache}</i> instance.
|
|
170
|
+
* To understand how this method works, refer to <i>{@link IEventListenable}</i>.
|
|
171
|
+
*/
|
|
172
|
+
addListenerMany(events, listener) {
|
|
173
|
+
return this.eventBus.addListenerMany(events, listener);
|
|
65
174
|
}
|
|
66
175
|
/**
|
|
67
|
-
* You can listen to
|
|
68
|
-
*
|
|
69
|
-
* Refer to <i>{@link CacheEvents}</i>, to se all events dispatched by <i>Cache</i> class instance.
|
|
70
|
-
* Refer to <i>{@link IEventListenable}</i> for details on how the method works.
|
|
71
|
-
* @example
|
|
72
|
-
* ```ts
|
|
73
|
-
* import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
|
|
74
|
-
* import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
|
|
75
|
-
* import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
|
|
76
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
77
|
-
* import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
|
|
78
|
-
* import { KeyAddedCacheEvent, type CacheEvents } from "@daiso-tech/core/cache/contracts";
|
|
79
|
-
*
|
|
80
|
-
* const cache: IGroupableCache = new Cache({
|
|
81
|
-
* adapter: new MemoryCacheAdapter({
|
|
82
|
-
* rootGroup: "@global"
|
|
83
|
-
* }),
|
|
84
|
-
* });
|
|
85
|
-
*
|
|
86
|
-
* const listener: Invokable<CacheEvents> = event => {
|
|
87
|
-
* console.log(event);
|
|
88
|
-
* };
|
|
89
|
-
* await cache.addListener(KeyAddedCacheEvent, listener);
|
|
90
|
-
* await cache.add("a", 1);
|
|
91
|
-
* ```
|
|
176
|
+
* You can listen to the following <i>{@link CacheEvents}</i> of the <i>{@link ICache}</i> instance.
|
|
177
|
+
* To understand how this method works, refer to <i>{@link IEventListenable}</i>.
|
|
92
178
|
*/
|
|
93
|
-
|
|
94
|
-
return this.eventBus.
|
|
179
|
+
removeListener(event, listener) {
|
|
180
|
+
return this.eventBus.removeListener(event, listener);
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* You can listen to the following <i>{@link CacheEvents}</i> of the <i>{@link ICache}</i> instance.
|
|
184
|
+
* To understand how this method works, refer to <i>{@link IEventListenable}</i>.
|
|
185
|
+
*/
|
|
186
|
+
removeListenerMany(events, listener) {
|
|
187
|
+
return this.eventBus.removeListenerMany(events, listener);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* You can listen to the following <i>{@link CacheEvents}</i> of the <i>{@link ICache}</i> instance.
|
|
191
|
+
* To understand how this method works, refer to <i>{@link IEventListenable}</i>.
|
|
192
|
+
*/
|
|
193
|
+
listenOnce(event, listener) {
|
|
194
|
+
return this.eventBus.listenOnce(event, listener);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* You can listen to the following <i>{@link CacheEvents}</i> of the <i>{@link ICache}</i> instance.
|
|
198
|
+
* To understand how this method works, refer to <i>{@link IEventListenable}</i>.
|
|
199
|
+
*/
|
|
200
|
+
asPromise(event) {
|
|
201
|
+
return this.eventBus.asPromise(event);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* You can listen to the following <i>{@link CacheEvents}</i> of the <i>{@link ICache}</i> instance.
|
|
205
|
+
* To understand how this method works, refer to <i>{@link IEventListenable}</i>.
|
|
206
|
+
*/
|
|
207
|
+
subscribe(event, listener) {
|
|
208
|
+
return this.eventBus.subscribe(event, listener);
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* You can listen to the following <i>{@link CacheEvents}</i> of the <i>{@link ICache}</i> instance.
|
|
212
|
+
* To understand how this method works, refer to <i>{@link IEventListenable}</i>.
|
|
213
|
+
*/
|
|
214
|
+
subscribeMany(events, listener) {
|
|
215
|
+
return this.eventBus.subscribeMany(events, listener);
|
|
216
|
+
}
|
|
217
|
+
createLazyPromise(asyncFn) {
|
|
218
|
+
return new LazyPromise(asyncFn, {
|
|
219
|
+
retryAttempts: this.retryAttempts,
|
|
220
|
+
backoffPolicy: this.backoffPolicy,
|
|
221
|
+
retryPolicy: this.retryPolicy,
|
|
222
|
+
retryTimeout: this.retryTimeout,
|
|
223
|
+
totalTimeout: this.totalTimeout,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
exists(key) {
|
|
227
|
+
return new LazyPromise(async () => {
|
|
228
|
+
const value = await this.get(key);
|
|
229
|
+
return value !== null;
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
missing(key) {
|
|
233
|
+
return this.createLazyPromise(async () => {
|
|
234
|
+
const hasKey = await this.exists(key);
|
|
235
|
+
return !hasKey;
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
get(key) {
|
|
239
|
+
return this.createLazyPromise(async () => {
|
|
240
|
+
const keyObj = this.keyPrefixer.create(key);
|
|
241
|
+
try {
|
|
242
|
+
const adapter = await this.adapterPromise;
|
|
243
|
+
const value = await adapter.get(keyObj.prefixed);
|
|
244
|
+
if (value === null) {
|
|
245
|
+
this.eventBus
|
|
246
|
+
.dispatch(new KeyNotFoundCacheEvent({
|
|
247
|
+
group: this.getGroup(),
|
|
248
|
+
key: keyObj.resolved,
|
|
249
|
+
}))
|
|
250
|
+
.defer();
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
this.eventBus
|
|
254
|
+
.dispatch(new KeyFoundCacheEvent({
|
|
255
|
+
group: this.getGroup(),
|
|
256
|
+
key: keyObj.resolved,
|
|
257
|
+
value,
|
|
258
|
+
}))
|
|
259
|
+
.defer();
|
|
260
|
+
}
|
|
261
|
+
return value;
|
|
262
|
+
}
|
|
263
|
+
catch (error) {
|
|
264
|
+
this.eventBus
|
|
265
|
+
.dispatch(new UnexpectedCacheErrorEvent({
|
|
266
|
+
group: this.getGroup(),
|
|
267
|
+
keys: [keyObj.resolved],
|
|
268
|
+
method: this.get.name,
|
|
269
|
+
error,
|
|
270
|
+
}))
|
|
271
|
+
.defer();
|
|
272
|
+
throw error;
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
getOrFail(key) {
|
|
277
|
+
return this.createLazyPromise(async () => {
|
|
278
|
+
const value = await this.get(key);
|
|
279
|
+
if (value === null) {
|
|
280
|
+
throw new KeyNotFoundCacheError(`Key "${this.keyPrefixer.create(key).resolved}" is not found`);
|
|
281
|
+
}
|
|
282
|
+
return value;
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
getAndRemove(key) {
|
|
286
|
+
return this.createLazyPromise(async () => {
|
|
287
|
+
const keyObj = this.keyPrefixer.create(key);
|
|
288
|
+
try {
|
|
289
|
+
const adapter = await this.adapterPromise;
|
|
290
|
+
const value = await adapter.getAndRemove(keyObj.prefixed);
|
|
291
|
+
if (value === null) {
|
|
292
|
+
this.eventBus
|
|
293
|
+
.dispatch(new KeyNotFoundCacheEvent({
|
|
294
|
+
group: this.getGroup(),
|
|
295
|
+
key: keyObj.resolved,
|
|
296
|
+
}))
|
|
297
|
+
.defer();
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
this.eventBus
|
|
301
|
+
.dispatch(new KeyFoundCacheEvent({
|
|
302
|
+
group: this.getGroup(),
|
|
303
|
+
key: keyObj.resolved,
|
|
304
|
+
value,
|
|
305
|
+
}))
|
|
306
|
+
.defer();
|
|
307
|
+
this.eventBus
|
|
308
|
+
.dispatch(new KeyRemovedCacheEvent({
|
|
309
|
+
group: this.getGroup(),
|
|
310
|
+
key: keyObj.resolved,
|
|
311
|
+
}))
|
|
312
|
+
.defer();
|
|
313
|
+
}
|
|
314
|
+
return value;
|
|
315
|
+
}
|
|
316
|
+
catch (error) {
|
|
317
|
+
this.eventBus
|
|
318
|
+
.dispatch(new UnexpectedCacheErrorEvent({
|
|
319
|
+
group: this.getGroup(),
|
|
320
|
+
keys: [keyObj.resolved],
|
|
321
|
+
method: this.get.name,
|
|
322
|
+
error,
|
|
323
|
+
}))
|
|
324
|
+
.defer();
|
|
325
|
+
throw error;
|
|
326
|
+
}
|
|
327
|
+
});
|
|
95
328
|
}
|
|
96
329
|
/**
|
|
97
|
-
* You can listen to different events of <i>Cache</i> class instance.
|
|
98
|
-
*
|
|
99
|
-
* Refer to <i>{@link CacheEvents}</i>, to se all events dispatched by <i>Cache</i> class instance.
|
|
100
|
-
* Refer to <i>{@link IEventListenable}</i> for details on how the method works.
|
|
101
330
|
* @example
|
|
102
331
|
* ```ts
|
|
103
|
-
* import
|
|
104
|
-
* import
|
|
105
|
-
* import {
|
|
106
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
107
|
-
* import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
|
|
108
|
-
* import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
|
|
109
|
-
* import { KeyAddedCacheEvent, type CacheEvents } from "@daiso-tech/core/cache/contracts";
|
|
332
|
+
* import { Cache } from "@daiso-tech/core/cache";
|
|
333
|
+
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/adapters";
|
|
334
|
+
* import { KeyPrefixer } from "@daiso-tech/core/utilities";
|
|
110
335
|
*
|
|
111
|
-
* const cache
|
|
112
|
-
* adapter: new MemoryCacheAdapter(
|
|
113
|
-
*
|
|
114
|
-
* }),
|
|
336
|
+
* const cache = new Cache({
|
|
337
|
+
* adapter: new MemoryCacheAdapter(),
|
|
338
|
+
* keyPrefixer: new KeyPrefixer("cache")
|
|
115
339
|
* });
|
|
116
340
|
*
|
|
117
|
-
* const
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
* await cache.add("a", 1);
|
|
341
|
+
* const value = await cache.getOr("a", 1);
|
|
342
|
+
*
|
|
343
|
+
* // Will be 1
|
|
344
|
+
* console.log(value);
|
|
122
345
|
* ```
|
|
123
|
-
*/
|
|
124
|
-
addListenerMany(eventNames, listener) {
|
|
125
|
-
return this.eventBus.addListenerMany(eventNames, listener);
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* You can listen to different events of <i>Cache</i> class instance.
|
|
129
346
|
*
|
|
130
|
-
*
|
|
131
|
-
* Refer to <i>{@link IEventListenable}</i> for details on how the method works.
|
|
347
|
+
* You can also pass in a function
|
|
132
348
|
* @example
|
|
133
349
|
* ```ts
|
|
134
|
-
* import
|
|
135
|
-
* import
|
|
136
|
-
* import {
|
|
137
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
138
|
-
* import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
|
|
139
|
-
* import { KeyAddedCacheEvent, type CacheEvents } from "@daiso-tech/core/cache/contracts";
|
|
350
|
+
* import { Cache } from "@daiso-tech/core/cache";
|
|
351
|
+
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/adapters";
|
|
352
|
+
* import { KeyPrefixer } from "@daiso-tech/core/utilities";
|
|
140
353
|
*
|
|
141
|
-
* const cache
|
|
142
|
-
* adapter: new MemoryCacheAdapter(
|
|
143
|
-
*
|
|
144
|
-
* }),
|
|
354
|
+
* const cache = new Cache({
|
|
355
|
+
* adapter: new MemoryCacheAdapter(),
|
|
356
|
+
* keyPrefixer: new KeyPrefixer("cache")
|
|
145
357
|
* });
|
|
146
358
|
*
|
|
147
|
-
* const
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
* await cache.removeListener(KeyAddedCacheEvent, listener);
|
|
152
|
-
* await cache.add("a", 1);
|
|
359
|
+
* const value = await cache.getOr("a", () => 1);
|
|
360
|
+
*
|
|
361
|
+
* // Will be 1
|
|
362
|
+
* console.log(value);
|
|
153
363
|
* ```
|
|
154
|
-
*/
|
|
155
|
-
removeListener(eventName, listener) {
|
|
156
|
-
return this.eventBus.removeListener(eventName, listener);
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* You can listen to different events of <i>Cache</i> class instance.
|
|
160
364
|
*
|
|
161
|
-
*
|
|
162
|
-
* Refer to <i>{@link IEventListenable}</i> for details on how the method works.
|
|
365
|
+
* You can also pass in a async function. This is useful because it allows for retrieval of external data if the key doesnt't.
|
|
163
366
|
* @example
|
|
164
367
|
* ```ts
|
|
165
|
-
* import
|
|
166
|
-
* import
|
|
167
|
-
* import {
|
|
168
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
169
|
-
* import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
|
|
170
|
-
* import { KeyAddedCacheEvent, type CacheEvents } from "@daiso-tech/core/cache/contracts";
|
|
368
|
+
* import { Cache } from "@daiso-tech/core/cache";
|
|
369
|
+
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/adapters";
|
|
370
|
+
* import { KeyPrefixer } from "@daiso-tech/core/utilities";
|
|
171
371
|
*
|
|
172
|
-
* const cache
|
|
173
|
-
* adapter: new MemoryCacheAdapter(
|
|
174
|
-
*
|
|
175
|
-
* }),
|
|
372
|
+
* const cache = new Cache({
|
|
373
|
+
* adapter: new MemoryCacheAdapter(),
|
|
374
|
+
* keyPrefixer: new KeyPrefixer("cache")
|
|
176
375
|
* });
|
|
177
376
|
*
|
|
178
|
-
* const
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
* await cache.removeListenerMany(KeyAddedCacheEvent, listener);
|
|
183
|
-
* await cache.add("a", 1);
|
|
377
|
+
* const value = await cache.getOr("a", async () => 1);
|
|
378
|
+
*
|
|
379
|
+
* // Will be 1
|
|
380
|
+
* console.log(value);
|
|
184
381
|
* ```
|
|
185
|
-
*/
|
|
186
|
-
removeListenerMany(eventNames, listener) {
|
|
187
|
-
return this.eventBus.removeListenerMany(eventNames, listener);
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* You can listen to different events of <i>Cache</i> class instance.
|
|
191
382
|
*
|
|
192
|
-
*
|
|
193
|
-
* Refer to <i>{@link IEventListenable}</i> for details on how the method works.
|
|
383
|
+
* You can also pass in a <i>{@link LazyPromise}</i>. This is useful because all other components in this library returns <i>{@link LazyPromise}</i>.
|
|
194
384
|
* @example
|
|
195
385
|
* ```ts
|
|
196
|
-
* import
|
|
197
|
-
* import
|
|
198
|
-
* import {
|
|
199
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
200
|
-
* import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
|
|
201
|
-
* import { KeyAddedCacheEvent, type CacheEvents } from "@daiso-tech/core/cache/contracts";
|
|
386
|
+
* import { Cache } from "@daiso-tech/core/cache";
|
|
387
|
+
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/adapters";
|
|
388
|
+
* import { KeyPrefixer, LazyPromise } from "@daiso-tech/core/utilities";
|
|
202
389
|
*
|
|
203
|
-
* const cache
|
|
204
|
-
* adapter: new MemoryCacheAdapter(
|
|
205
|
-
*
|
|
206
|
-
* }),
|
|
390
|
+
* const cache = new Cache({
|
|
391
|
+
* adapter: new MemoryCacheAdapter(),
|
|
392
|
+
* keyPrefixer: new KeyPrefixer("cache")
|
|
207
393
|
* });
|
|
208
394
|
*
|
|
209
|
-
* const
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
* await cache.add("a", 1);
|
|
395
|
+
* const value = await cache.getOr("a", new LazyPromise(async () => 1));
|
|
396
|
+
*
|
|
397
|
+
* // Will be 1
|
|
398
|
+
* console.log(value);
|
|
214
399
|
* ```
|
|
215
400
|
*/
|
|
216
|
-
|
|
217
|
-
return this.
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
401
|
+
getOr(key, defaultValue) {
|
|
402
|
+
return this.createLazyPromise(async () => {
|
|
403
|
+
const value = await this.get(key);
|
|
404
|
+
if (value === null) {
|
|
405
|
+
const simplifiedValueToAdd = await resolveAsyncLazyable(defaultValue);
|
|
406
|
+
return simplifiedValueToAdd;
|
|
407
|
+
}
|
|
408
|
+
return value;
|
|
409
|
+
});
|
|
221
410
|
}
|
|
222
411
|
/**
|
|
223
|
-
* You can listen to different events of <i>Cache</i> class instance.
|
|
224
|
-
*
|
|
225
|
-
* Refer to <i>{@link CacheEvents}</i>, to se all events dispatched by <i>Cache</i> class instance.
|
|
226
|
-
* Refer to <i>{@link IEventListenable}</i> for details on how the method works.
|
|
227
412
|
* @example
|
|
228
413
|
* ```ts
|
|
229
|
-
* import
|
|
230
|
-
* import
|
|
231
|
-
* import {
|
|
232
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
233
|
-
* import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
|
|
234
|
-
* import { KeyAddedCacheEvent, type CacheEvents } from "@daiso-tech/core/cache/contracts";
|
|
414
|
+
* import { Cache } from "@daiso-tech/core/cache";
|
|
415
|
+
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/adapters";
|
|
416
|
+
* import { KeyPrefixer } from "@daiso-tech/core/utilities";
|
|
235
417
|
*
|
|
236
|
-
* const cache
|
|
237
|
-
* adapter: new MemoryCacheAdapter(
|
|
238
|
-
*
|
|
239
|
-
* }),
|
|
418
|
+
* const cache = new Cache({
|
|
419
|
+
* adapter: new MemoryCacheAdapter(),
|
|
420
|
+
* keyPrefixer: new KeyPrefixer("cache")
|
|
240
421
|
* });
|
|
241
422
|
*
|
|
242
|
-
* const
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
* await cache.add("a", 1);
|
|
247
|
-
* await unsubscribe();
|
|
423
|
+
* const value = await cache.getOrAdd("a", 1);
|
|
424
|
+
*
|
|
425
|
+
* // Will be 1
|
|
426
|
+
* console.log(value);
|
|
248
427
|
* ```
|
|
249
|
-
*/
|
|
250
|
-
subscribe(eventName, listener) {
|
|
251
|
-
return this.eventBus.subscribe(eventName, listener);
|
|
252
|
-
}
|
|
253
|
-
/**
|
|
254
|
-
* You can listen to different events of <i>Cache</i> class instance.
|
|
255
428
|
*
|
|
256
|
-
*
|
|
257
|
-
* Refer to <i>{@link IEventListenable}</i> for details on how the method works.
|
|
429
|
+
* You can also pass in a function
|
|
258
430
|
* @example
|
|
259
431
|
* ```ts
|
|
260
|
-
* import
|
|
261
|
-
* import
|
|
262
|
-
* import {
|
|
263
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
264
|
-
* import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
|
|
265
|
-
* import { KeyAddedCacheEvent, type CacheEvents } from "@daiso-tech/core/cache/contracts";
|
|
432
|
+
* import { Cache } from "@daiso-tech/core/cache";
|
|
433
|
+
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/adapters";
|
|
434
|
+
* import { KeyPrefixer } from "@daiso-tech/core/utilities";
|
|
266
435
|
*
|
|
267
|
-
* const cache
|
|
268
|
-
* adapter: new MemoryCacheAdapter(
|
|
269
|
-
*
|
|
270
|
-
* }),
|
|
436
|
+
* const cache = new Cache({
|
|
437
|
+
* adapter: new MemoryCacheAdapter(),
|
|
438
|
+
* keyPrefixer: new KeyPrefixer("cache")
|
|
271
439
|
* });
|
|
272
440
|
*
|
|
273
|
-
* const
|
|
274
|
-
*
|
|
275
|
-
*
|
|
276
|
-
*
|
|
277
|
-
* await cache.add("a", 1);
|
|
278
|
-
* await unsubscribe();
|
|
441
|
+
* const value = await cache.getOrAdd("a", () => 1);
|
|
442
|
+
*
|
|
443
|
+
* // Will be 1
|
|
444
|
+
* console.log(value);
|
|
279
445
|
* ```
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
return this.eventBus.subscribeMany(eventNames, listener);
|
|
283
|
-
}
|
|
284
|
-
/**
|
|
446
|
+
*
|
|
447
|
+
* You can also pass in a async function. This is useful because it allows for retrieval of external data if the key doesnt't.
|
|
285
448
|
* @example
|
|
286
449
|
* ```ts
|
|
287
|
-
* import
|
|
288
|
-
* import
|
|
289
|
-
* import {
|
|
290
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
450
|
+
* import { Cache } from "@daiso-tech/core/cache";
|
|
451
|
+
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/adapters";
|
|
452
|
+
* import { KeyPrefixer } from "@daiso-tech/core/utilities";
|
|
291
453
|
*
|
|
292
|
-
* const cache
|
|
293
|
-
* adapter: new MemoryCacheAdapter(
|
|
294
|
-
*
|
|
295
|
-
* }),
|
|
454
|
+
* const cache = new Cache({
|
|
455
|
+
* adapter: new MemoryCacheAdapter(),
|
|
456
|
+
* keyPrefixer: new KeyPrefixer("cache")
|
|
296
457
|
* });
|
|
297
458
|
*
|
|
298
|
-
*
|
|
299
|
-
* console.log(cache.getGroup());
|
|
459
|
+
* const value = await cache.getOrAdd("a", async () => 1);
|
|
300
460
|
*
|
|
301
|
-
*
|
|
302
|
-
*
|
|
303
|
-
* // Will print "@global/company-1"
|
|
304
|
-
* console.log(groupedCache.getGroup());
|
|
461
|
+
* // Will be 1
|
|
462
|
+
* console.log(value);
|
|
305
463
|
* ```
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
return new Cache({
|
|
309
|
-
adapter: this.adapter.withGroup(resolveOneOrMoreStr(group)),
|
|
310
|
-
defaultTtl: this.defaultTtl,
|
|
311
|
-
eventBus: this.groupdEventBus,
|
|
312
|
-
retryAttempts: this.retryAttempts,
|
|
313
|
-
backoffPolicy: this.backoffPolicy,
|
|
314
|
-
retryPolicy: this.retryPolicy,
|
|
315
|
-
timeout: this.timeout,
|
|
316
|
-
});
|
|
317
|
-
}
|
|
318
|
-
/**
|
|
464
|
+
*
|
|
465
|
+
* You can also pass in a <i>{@link LazyPromise}</i>. This is useful because all other components in this library returns <i>{@link LazyPromise}</i>.
|
|
319
466
|
* @example
|
|
320
467
|
* ```ts
|
|
321
|
-
* import
|
|
322
|
-
* import
|
|
323
|
-
* import {
|
|
324
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
468
|
+
* import { Cache } from "@daiso-tech/core/cache";
|
|
469
|
+
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/adapters";
|
|
470
|
+
* import { KeyPrefixer, LazyPromise } from "@daiso-tech/core/utilities";
|
|
325
471
|
*
|
|
326
|
-
* const cache
|
|
327
|
-
* adapter: new MemoryCacheAdapter(
|
|
328
|
-
*
|
|
329
|
-
* }),
|
|
472
|
+
* const cache = new Cache({
|
|
473
|
+
* adapter: new MemoryCacheAdapter(),
|
|
474
|
+
* keyPrefixer: new KeyPrefixer("cache")
|
|
330
475
|
* });
|
|
331
476
|
*
|
|
332
|
-
*
|
|
333
|
-
*
|
|
477
|
+
* const value = await cache.getOrAdd("a", new LazyPromise(async () => 1));
|
|
478
|
+
*
|
|
479
|
+
* // Will be 1
|
|
480
|
+
* console.log(value);
|
|
334
481
|
* ```
|
|
335
482
|
*/
|
|
336
|
-
|
|
337
|
-
return this.
|
|
483
|
+
getOrAdd(key, valueToAdd, ttl) {
|
|
484
|
+
return this.createLazyPromise(async () => {
|
|
485
|
+
const value = await this.get(key);
|
|
486
|
+
if (value === null) {
|
|
487
|
+
const simplifiedValueToAdd = await resolveAsyncLazyable(valueToAdd);
|
|
488
|
+
await this.add(key, simplifiedValueToAdd, ttl);
|
|
489
|
+
return simplifiedValueToAdd;
|
|
490
|
+
}
|
|
491
|
+
return value;
|
|
492
|
+
});
|
|
338
493
|
}
|
|
339
|
-
|
|
340
|
-
return this.
|
|
494
|
+
add(key, value, ttl = this.defaultTtl) {
|
|
495
|
+
return this.createLazyPromise(async () => {
|
|
496
|
+
const keyObj = this.keyPrefixer.create(key);
|
|
341
497
|
try {
|
|
342
|
-
const
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
.dispatch(new KeyNotFoundCacheEvent({
|
|
346
|
-
group: this.getGroup(),
|
|
347
|
-
key,
|
|
348
|
-
}))
|
|
349
|
-
.defer();
|
|
350
|
-
}
|
|
351
|
-
else {
|
|
498
|
+
const adapter = await this.adapterPromise;
|
|
499
|
+
const hasAdded = await adapter.add(keyObj.prefixed, value, ttl);
|
|
500
|
+
if (hasAdded) {
|
|
352
501
|
this.eventBus
|
|
353
|
-
.dispatch(new
|
|
502
|
+
.dispatch(new KeyAddedCacheEvent({
|
|
354
503
|
group: this.getGroup(),
|
|
355
|
-
key,
|
|
504
|
+
key: keyObj.resolved,
|
|
356
505
|
value,
|
|
506
|
+
ttl,
|
|
357
507
|
}))
|
|
358
508
|
.defer();
|
|
359
509
|
}
|
|
360
|
-
return
|
|
510
|
+
return hasAdded;
|
|
361
511
|
}
|
|
362
512
|
catch (error) {
|
|
363
513
|
this.eventBus
|
|
364
514
|
.dispatch(new UnexpectedCacheErrorEvent({
|
|
365
515
|
group: this.getGroup(),
|
|
366
|
-
|
|
367
|
-
|
|
516
|
+
keys: [keyObj.resolved],
|
|
517
|
+
value,
|
|
518
|
+
method: this.add.name,
|
|
368
519
|
error,
|
|
369
520
|
}))
|
|
370
521
|
.defer();
|
|
@@ -372,33 +523,40 @@ export class Cache {
|
|
|
372
523
|
}
|
|
373
524
|
});
|
|
374
525
|
}
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
*/
|
|
379
|
-
ttl = this.defaultTtl) {
|
|
380
|
-
return this.createLayPromise(async () => {
|
|
526
|
+
put(key, value, ttl = this.defaultTtl) {
|
|
527
|
+
return this.createLazyPromise(async () => {
|
|
528
|
+
const keyObj = this.keyPrefixer.create(key);
|
|
381
529
|
try {
|
|
382
|
-
const
|
|
383
|
-
|
|
530
|
+
const adapter = await this.adapterPromise;
|
|
531
|
+
const hasUpdated = await adapter.put(keyObj.prefixed, value, ttl);
|
|
532
|
+
if (hasUpdated) {
|
|
533
|
+
this.eventBus
|
|
534
|
+
.dispatch(new KeyUpdatedCacheEvent({
|
|
535
|
+
group: this.getGroup(),
|
|
536
|
+
key: keyObj.resolved,
|
|
537
|
+
value,
|
|
538
|
+
}))
|
|
539
|
+
.defer();
|
|
540
|
+
}
|
|
541
|
+
else {
|
|
384
542
|
this.eventBus
|
|
385
543
|
.dispatch(new KeyAddedCacheEvent({
|
|
386
544
|
group: this.getGroup(),
|
|
387
|
-
key,
|
|
545
|
+
key: keyObj.resolved,
|
|
388
546
|
value,
|
|
389
547
|
ttl,
|
|
390
548
|
}))
|
|
391
549
|
.defer();
|
|
392
550
|
}
|
|
393
|
-
return
|
|
551
|
+
return hasUpdated;
|
|
394
552
|
}
|
|
395
553
|
catch (error) {
|
|
396
554
|
this.eventBus
|
|
397
555
|
.dispatch(new UnexpectedCacheErrorEvent({
|
|
398
556
|
group: this.getGroup(),
|
|
399
|
-
|
|
557
|
+
keys: [keyObj.resolved],
|
|
400
558
|
value,
|
|
401
|
-
method: this.
|
|
559
|
+
method: this.put.name,
|
|
402
560
|
error,
|
|
403
561
|
}))
|
|
404
562
|
.defer();
|
|
@@ -407,14 +565,16 @@ export class Cache {
|
|
|
407
565
|
});
|
|
408
566
|
}
|
|
409
567
|
update(key, value) {
|
|
410
|
-
return this.
|
|
568
|
+
return this.createLazyPromise(async () => {
|
|
569
|
+
const keyObj = this.keyPrefixer.create(key);
|
|
411
570
|
try {
|
|
412
|
-
const
|
|
571
|
+
const adapter = await this.adapterPromise;
|
|
572
|
+
const hasUpdated = await adapter.update(keyObj.prefixed, value);
|
|
413
573
|
if (hasUpdated) {
|
|
414
574
|
this.eventBus
|
|
415
575
|
.dispatch(new KeyUpdatedCacheEvent({
|
|
416
576
|
group: this.getGroup(),
|
|
417
|
-
key,
|
|
577
|
+
key: keyObj.resolved,
|
|
418
578
|
value,
|
|
419
579
|
}))
|
|
420
580
|
.defer();
|
|
@@ -423,7 +583,7 @@ export class Cache {
|
|
|
423
583
|
this.eventBus
|
|
424
584
|
.dispatch(new KeyNotFoundCacheEvent({
|
|
425
585
|
group: this.getGroup(),
|
|
426
|
-
key,
|
|
586
|
+
key: keyObj.resolved,
|
|
427
587
|
}))
|
|
428
588
|
.defer();
|
|
429
589
|
}
|
|
@@ -433,7 +593,7 @@ export class Cache {
|
|
|
433
593
|
this.eventBus
|
|
434
594
|
.dispatch(new UnexpectedCacheErrorEvent({
|
|
435
595
|
group: this.getGroup(),
|
|
436
|
-
|
|
596
|
+
keys: [keyObj.resolved],
|
|
437
597
|
value,
|
|
438
598
|
method: this.update.name,
|
|
439
599
|
error,
|
|
@@ -443,30 +603,35 @@ export class Cache {
|
|
|
443
603
|
}
|
|
444
604
|
});
|
|
445
605
|
}
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
*/
|
|
450
|
-
ttl = this.defaultTtl) {
|
|
451
|
-
return this.createLayPromise(async () => {
|
|
606
|
+
increment(key, value = 0) {
|
|
607
|
+
return this.createLazyPromise(async () => {
|
|
608
|
+
const keyObj = this.keyPrefixer.create(key);
|
|
452
609
|
try {
|
|
453
|
-
const
|
|
454
|
-
|
|
610
|
+
const adapter = await this.adapterPromise;
|
|
611
|
+
const hasUpdated = await adapter.increment(keyObj.prefixed, value);
|
|
612
|
+
if (hasUpdated && value > 0) {
|
|
455
613
|
this.eventBus
|
|
456
|
-
.dispatch(new
|
|
614
|
+
.dispatch(new KeyIncrementedCacheEvent({
|
|
457
615
|
group: this.getGroup(),
|
|
458
|
-
key,
|
|
616
|
+
key: keyObj.resolved,
|
|
459
617
|
value,
|
|
460
618
|
}))
|
|
461
619
|
.defer();
|
|
462
620
|
}
|
|
463
|
-
|
|
621
|
+
if (hasUpdated && value < 0) {
|
|
464
622
|
this.eventBus
|
|
465
|
-
.dispatch(new
|
|
623
|
+
.dispatch(new KeyDecrementedCacheEvent({
|
|
466
624
|
group: this.getGroup(),
|
|
467
|
-
key,
|
|
468
|
-
value,
|
|
469
|
-
|
|
625
|
+
key: keyObj.resolved,
|
|
626
|
+
value: -value,
|
|
627
|
+
}))
|
|
628
|
+
.defer();
|
|
629
|
+
}
|
|
630
|
+
if (!hasUpdated) {
|
|
631
|
+
this.eventBus
|
|
632
|
+
.dispatch(new KeyNotFoundCacheEvent({
|
|
633
|
+
group: this.getGroup(),
|
|
634
|
+
key: keyObj.resolved,
|
|
470
635
|
}))
|
|
471
636
|
.defer();
|
|
472
637
|
}
|
|
@@ -476,9 +641,9 @@ export class Cache {
|
|
|
476
641
|
this.eventBus
|
|
477
642
|
.dispatch(new UnexpectedCacheErrorEvent({
|
|
478
643
|
group: this.getGroup(),
|
|
479
|
-
|
|
644
|
+
keys: [keyObj.resolved],
|
|
480
645
|
value,
|
|
481
|
-
method: this.
|
|
646
|
+
method: this.increment.name,
|
|
482
647
|
error,
|
|
483
648
|
}))
|
|
484
649
|
.defer();
|
|
@@ -486,15 +651,22 @@ export class Cache {
|
|
|
486
651
|
}
|
|
487
652
|
});
|
|
488
653
|
}
|
|
654
|
+
decrement(key, value = 0) {
|
|
655
|
+
return this.createLazyPromise(async () => {
|
|
656
|
+
return await this.increment(key, -value);
|
|
657
|
+
});
|
|
658
|
+
}
|
|
489
659
|
remove(key) {
|
|
490
|
-
return this.
|
|
660
|
+
return this.createLazyPromise(async () => {
|
|
661
|
+
const keyObj = this.keyPrefixer.create(key);
|
|
491
662
|
try {
|
|
492
|
-
const
|
|
663
|
+
const adapter = await this.adapterPromise;
|
|
664
|
+
const hasRemoved = await adapter.removeMany([keyObj.prefixed]);
|
|
493
665
|
if (hasRemoved) {
|
|
494
666
|
this.eventBus
|
|
495
667
|
.dispatch(new KeyRemovedCacheEvent({
|
|
496
668
|
group: this.getGroup(),
|
|
497
|
-
key,
|
|
669
|
+
key: keyObj.resolved,
|
|
498
670
|
}))
|
|
499
671
|
.defer();
|
|
500
672
|
}
|
|
@@ -502,7 +674,7 @@ export class Cache {
|
|
|
502
674
|
this.eventBus
|
|
503
675
|
.dispatch(new KeyNotFoundCacheEvent({
|
|
504
676
|
group: this.getGroup(),
|
|
505
|
-
key,
|
|
677
|
+
key: keyObj.resolved,
|
|
506
678
|
}))
|
|
507
679
|
.defer();
|
|
508
680
|
}
|
|
@@ -512,7 +684,7 @@ export class Cache {
|
|
|
512
684
|
this.eventBus
|
|
513
685
|
.dispatch(new UnexpectedCacheErrorEvent({
|
|
514
686
|
group: this.getGroup(),
|
|
515
|
-
|
|
687
|
+
keys: [keyObj.resolved],
|
|
516
688
|
method: this.remove.name,
|
|
517
689
|
error,
|
|
518
690
|
}))
|
|
@@ -521,45 +693,38 @@ export class Cache {
|
|
|
521
693
|
}
|
|
522
694
|
});
|
|
523
695
|
}
|
|
524
|
-
|
|
525
|
-
return this.
|
|
696
|
+
removeMany(keys) {
|
|
697
|
+
return this.createLazyPromise(async () => {
|
|
698
|
+
const keysArr = [...keys];
|
|
699
|
+
if (keysArr.length === 0) {
|
|
700
|
+
return true;
|
|
701
|
+
}
|
|
702
|
+
const keyObjArr = keysArr.map((key) => this.keyPrefixer.create(key));
|
|
526
703
|
try {
|
|
527
|
-
const
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
704
|
+
const adapter = await this.adapterPromise;
|
|
705
|
+
const hasRemovedAtLeastOne = await adapter.removeMany(keyObjArr.map((keyObj) => keyObj.prefixed));
|
|
706
|
+
if (hasRemovedAtLeastOne) {
|
|
707
|
+
const events = keyObjArr.map((keyObj) => new KeyRemovedCacheEvent({
|
|
531
708
|
group: this.getGroup(),
|
|
532
|
-
key,
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
.defer();
|
|
709
|
+
key: keyObj.resolved,
|
|
710
|
+
}));
|
|
711
|
+
this.eventBus.dispatchMany(events).defer();
|
|
536
712
|
}
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
.dispatch(new KeyDecrementedCacheEvent({
|
|
540
|
-
group: this.getGroup(),
|
|
541
|
-
key,
|
|
542
|
-
value: -value,
|
|
543
|
-
}))
|
|
544
|
-
.defer();
|
|
545
|
-
}
|
|
546
|
-
if (!hasUpdated) {
|
|
547
|
-
this.eventBus
|
|
548
|
-
.dispatch(new KeyNotFoundCacheEvent({
|
|
713
|
+
else {
|
|
714
|
+
const events = keyObjArr.map((keyObj) => new KeyNotFoundCacheEvent({
|
|
549
715
|
group: this.getGroup(),
|
|
550
|
-
key,
|
|
551
|
-
}))
|
|
552
|
-
|
|
716
|
+
key: keyObj.resolved,
|
|
717
|
+
}));
|
|
718
|
+
this.eventBus.dispatchMany(events).defer();
|
|
553
719
|
}
|
|
554
|
-
return
|
|
720
|
+
return hasRemovedAtLeastOne;
|
|
555
721
|
}
|
|
556
722
|
catch (error) {
|
|
557
723
|
this.eventBus
|
|
558
724
|
.dispatch(new UnexpectedCacheErrorEvent({
|
|
559
725
|
group: this.getGroup(),
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
method: this.increment.name,
|
|
726
|
+
keys: keyObjArr.map((keyObj) => keyObj.resolved),
|
|
727
|
+
method: this.remove.name,
|
|
563
728
|
error,
|
|
564
729
|
}))
|
|
565
730
|
.defer();
|
|
@@ -568,14 +733,19 @@ export class Cache {
|
|
|
568
733
|
});
|
|
569
734
|
}
|
|
570
735
|
clear() {
|
|
571
|
-
return this.
|
|
736
|
+
return this.createLazyPromise(async () => {
|
|
572
737
|
try {
|
|
573
|
-
await this.
|
|
574
|
-
this.eventBus
|
|
575
|
-
.dispatch(new KeysClearedCacheEvent({
|
|
738
|
+
const adapter = await this.adapterPromise;
|
|
739
|
+
const promise = this.eventBus.dispatch(new KeysClearedCacheEvent({
|
|
576
740
|
group: this.getGroup(),
|
|
577
|
-
}))
|
|
578
|
-
|
|
741
|
+
}));
|
|
742
|
+
if (isFactory(this.adapterFactoryable)) {
|
|
743
|
+
await adapter.removeAll();
|
|
744
|
+
promise.defer();
|
|
745
|
+
return;
|
|
746
|
+
}
|
|
747
|
+
await adapter.removeByKeyPrefix(this.keyPrefixer.keyPrefix);
|
|
748
|
+
promise.defer();
|
|
579
749
|
}
|
|
580
750
|
catch (error) {
|
|
581
751
|
this.eventBus
|
|
@@ -589,521 +759,67 @@ export class Cache {
|
|
|
589
759
|
}
|
|
590
760
|
});
|
|
591
761
|
}
|
|
592
|
-
exists(key) {
|
|
593
|
-
return this.createLayPromise(async () => {
|
|
594
|
-
const value = await this.get(key);
|
|
595
|
-
return value !== null;
|
|
596
|
-
});
|
|
597
|
-
}
|
|
598
|
-
existsMany(keys) {
|
|
599
|
-
return this.createLayPromise(async () => {
|
|
600
|
-
if (keys.length === 0) {
|
|
601
|
-
return {};
|
|
602
|
-
}
|
|
603
|
-
const valuePromises = [];
|
|
604
|
-
for (const key of keys) {
|
|
605
|
-
valuePromises.push(this.exists(key));
|
|
606
|
-
}
|
|
607
|
-
const values = await Promise.all(valuePromises);
|
|
608
|
-
const result = {};
|
|
609
|
-
for (const [index, key] of keys.entries()) {
|
|
610
|
-
const value = values[index];
|
|
611
|
-
if (value === undefined) {
|
|
612
|
-
throw new UnexpectedCacheError(`Item "values[${String(index)}]" is undefined`);
|
|
613
|
-
}
|
|
614
|
-
result[key] = value;
|
|
615
|
-
}
|
|
616
|
-
return result;
|
|
617
|
-
});
|
|
618
|
-
}
|
|
619
|
-
missing(key) {
|
|
620
|
-
return this.createLayPromise(async () => {
|
|
621
|
-
const value = await this.get(key);
|
|
622
|
-
return value === null;
|
|
623
|
-
});
|
|
624
|
-
}
|
|
625
|
-
missingMany(keys) {
|
|
626
|
-
return this.createLayPromise(async () => {
|
|
627
|
-
if (keys.length === 0) {
|
|
628
|
-
return {};
|
|
629
|
-
}
|
|
630
|
-
const valuePromises = [];
|
|
631
|
-
for (const key of keys) {
|
|
632
|
-
valuePromises.push(this.missing(key));
|
|
633
|
-
}
|
|
634
|
-
const values = await Promise.all(valuePromises);
|
|
635
|
-
const result = {};
|
|
636
|
-
for (const [index, key] of keys.entries()) {
|
|
637
|
-
const value = values[index];
|
|
638
|
-
if (value === undefined) {
|
|
639
|
-
throw new UnexpectedCacheError(`Item "values[${String(index)}]" is undefined`);
|
|
640
|
-
}
|
|
641
|
-
result[key] = value;
|
|
642
|
-
}
|
|
643
|
-
return result;
|
|
644
|
-
});
|
|
645
|
-
}
|
|
646
|
-
getMany(keys) {
|
|
647
|
-
return this.createLayPromise(async () => {
|
|
648
|
-
if (keys.length === 0) {
|
|
649
|
-
return {};
|
|
650
|
-
}
|
|
651
|
-
const valuePromises = [];
|
|
652
|
-
for (const key of keys) {
|
|
653
|
-
valuePromises.push(this.get(key));
|
|
654
|
-
}
|
|
655
|
-
const values = await Promise.all(valuePromises);
|
|
656
|
-
const result = {};
|
|
657
|
-
for (const [index, key] of keys.entries()) {
|
|
658
|
-
const value = values[index];
|
|
659
|
-
if (value === undefined) {
|
|
660
|
-
throw new UnexpectedCacheError(`Item "values[${String(index)}]" is undefined`);
|
|
661
|
-
}
|
|
662
|
-
result[key] = value;
|
|
663
|
-
}
|
|
664
|
-
return result;
|
|
665
|
-
});
|
|
666
|
-
}
|
|
667
|
-
/**
|
|
668
|
-
* @example
|
|
669
|
-
* ```ts
|
|
670
|
-
* import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
|
|
671
|
-
* import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
|
|
672
|
-
* import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
|
|
673
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
674
|
-
*
|
|
675
|
-
* const cache: IGroupableCache = new Cache({
|
|
676
|
-
* adapter: new MemoryCacheAdapter({
|
|
677
|
-
* rootGroup: "@global"
|
|
678
|
-
* }),
|
|
679
|
-
* });
|
|
680
|
-
*
|
|
681
|
-
* const result = await cache.getOr("a", -1);
|
|
682
|
-
* // -1
|
|
683
|
-
* console.log(result);
|
|
684
|
-
* ```
|
|
685
|
-
*
|
|
686
|
-
* You can pass function as default value.
|
|
687
|
-
* @example
|
|
688
|
-
* ```ts
|
|
689
|
-
* import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
|
|
690
|
-
* import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
|
|
691
|
-
* import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
|
|
692
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
693
|
-
*
|
|
694
|
-
* const cache: IGroupableCache = new Cache({
|
|
695
|
-
* adapter: new MemoryCacheAdapter({
|
|
696
|
-
* rootGroup: "@global"
|
|
697
|
-
* }),
|
|
698
|
-
* });
|
|
699
|
-
*
|
|
700
|
-
* const result = await cache.getOr("a", () => -1);
|
|
701
|
-
* // -1
|
|
702
|
-
* console.log(result);
|
|
703
|
-
* ```
|
|
704
|
-
*
|
|
705
|
-
* You can pass async function as default value.
|
|
706
|
-
* @example
|
|
707
|
-
* ```ts
|
|
708
|
-
* import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
|
|
709
|
-
* import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
|
|
710
|
-
* import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
|
|
711
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
712
|
-
*
|
|
713
|
-
* const cache: IGroupableCache = new Cache({
|
|
714
|
-
* adapter: new MemoryCacheAdapter({
|
|
715
|
-
* rootGroup: "@global"
|
|
716
|
-
* }),
|
|
717
|
-
* });
|
|
718
|
-
*
|
|
719
|
-
* const result = await cache.getOr("a", async () => -1);
|
|
720
|
-
* // -1
|
|
721
|
-
* console.log(result);
|
|
722
|
-
* ```
|
|
723
|
-
*
|
|
724
|
-
* You can pass <i>{@link LazyPromise}</i> as default value.
|
|
725
|
-
* @example
|
|
726
|
-
* ```ts
|
|
727
|
-
* import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
|
|
728
|
-
* import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
|
|
729
|
-
* import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
|
|
730
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
731
|
-
*
|
|
732
|
-
* const cache: IGroupableCache = new Cache({
|
|
733
|
-
* adapter: new MemoryCacheAdapter({
|
|
734
|
-
* rootGroup: "@global"
|
|
735
|
-
* }),
|
|
736
|
-
* });
|
|
737
|
-
*
|
|
738
|
-
* const result = await cache.getOr("a", new LazyPromise(async () => -1));
|
|
739
|
-
* // -1
|
|
740
|
-
* console.log(result);
|
|
741
|
-
* ```
|
|
742
|
-
*/
|
|
743
|
-
getOr(key, defaultValue) {
|
|
744
|
-
return this.createLayPromise(async () => {
|
|
745
|
-
const value = await this.get(key);
|
|
746
|
-
if (value === null) {
|
|
747
|
-
return await resolveAsyncLazyable(defaultValue);
|
|
748
|
-
}
|
|
749
|
-
return value;
|
|
750
|
-
});
|
|
751
|
-
}
|
|
752
762
|
/**
|
|
753
763
|
* @example
|
|
754
764
|
* ```ts
|
|
755
|
-
* import
|
|
756
|
-
* import
|
|
757
|
-
* import {
|
|
758
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
759
|
-
*
|
|
760
|
-
* const cache: IGroupableCache = new Cache({
|
|
761
|
-
* adapter: new MemoryCacheAdapter({
|
|
762
|
-
* rootGroup: "@global"
|
|
763
|
-
* }),
|
|
764
|
-
* });
|
|
765
|
-
*
|
|
766
|
-
* const result = await cache.getOrMany({ a: -1 });
|
|
767
|
-
* // { a: -1 }
|
|
768
|
-
* console.log(result);
|
|
769
|
-
* ```
|
|
770
|
-
*
|
|
771
|
-
* You can pass function as default value.
|
|
772
|
-
* @example
|
|
773
|
-
* ```ts
|
|
774
|
-
* import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
|
|
775
|
-
* import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
|
|
776
|
-
* import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
|
|
777
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
765
|
+
* import { Cache } from "@daiso-tech/core/cache";
|
|
766
|
+
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/adapters";
|
|
767
|
+
* import { KeyPrefixer } from "@daiso-tech/core/utilities";
|
|
778
768
|
*
|
|
779
|
-
* const cache
|
|
780
|
-
* adapter: new MemoryCacheAdapter(
|
|
781
|
-
*
|
|
782
|
-
* }),
|
|
769
|
+
* const cache = new Cache({
|
|
770
|
+
* adapter: new MemoryCacheAdapter(),
|
|
771
|
+
* keyPrefixer: new KeyPrefixer("cache")
|
|
783
772
|
* });
|
|
784
773
|
*
|
|
785
|
-
*
|
|
786
|
-
*
|
|
787
|
-
* console.log(result);
|
|
788
|
-
* ```
|
|
789
|
-
*
|
|
790
|
-
* You can pass async function as default value.
|
|
791
|
-
* @example
|
|
792
|
-
* ```ts
|
|
793
|
-
* import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
|
|
794
|
-
* import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
|
|
795
|
-
* import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
|
|
796
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
797
|
-
*
|
|
798
|
-
* const cache: IGroupableCache = new Cache({
|
|
799
|
-
* adapter: new MemoryCacheAdapter({
|
|
800
|
-
* rootGroup: "@global"
|
|
801
|
-
* }),
|
|
802
|
-
* });
|
|
803
|
-
*
|
|
804
|
-
* const result = await cache.getOrMany({ a: async () => -1 });
|
|
805
|
-
* // { a: -1 }
|
|
806
|
-
* console.log(result);
|
|
807
|
-
* ```
|
|
808
|
-
*
|
|
809
|
-
* You can pass <i>{@link LazyPromise}</i> as default value.
|
|
810
|
-
* @example
|
|
811
|
-
* ```ts
|
|
812
|
-
* import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
|
|
813
|
-
* import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
|
|
814
|
-
* import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
|
|
815
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
816
|
-
*
|
|
817
|
-
* const cache: IGroupableCache = new Cache({
|
|
818
|
-
* adapter: new MemoryCacheAdapter({
|
|
819
|
-
* rootGroup: "@global"
|
|
820
|
-
* }),
|
|
821
|
-
* });
|
|
822
|
-
*
|
|
823
|
-
* const result = await cache.getOrMany({ a: new LazyPromise(async () => - 1) });
|
|
824
|
-
* // { a: -1 }
|
|
825
|
-
* console.log(result);
|
|
826
|
-
* ```
|
|
827
|
-
*/
|
|
828
|
-
getOrMany(keysWithDefaults) {
|
|
829
|
-
return this.createLayPromise(async () => {
|
|
830
|
-
const keys = Object.keys(keysWithDefaults);
|
|
831
|
-
if (keys.length === 0) {
|
|
832
|
-
return {};
|
|
833
|
-
}
|
|
834
|
-
const valuePromises = [];
|
|
835
|
-
for (const key of keys) {
|
|
836
|
-
const defaultValue = keysWithDefaults[key];
|
|
837
|
-
valuePromises.push(this.getOr(key, defaultValue));
|
|
838
|
-
}
|
|
839
|
-
const values = await Promise.all(valuePromises);
|
|
840
|
-
const result = {};
|
|
841
|
-
for (const [index, key] of keys.entries()) {
|
|
842
|
-
const value = values[index];
|
|
843
|
-
if (value === undefined) {
|
|
844
|
-
throw new UnexpectedCacheError(`Item "values[${String(index)}]" is undefined`);
|
|
845
|
-
}
|
|
846
|
-
result[key] = value;
|
|
847
|
-
}
|
|
848
|
-
return result;
|
|
849
|
-
});
|
|
850
|
-
}
|
|
851
|
-
/**
|
|
852
|
-
* @example
|
|
853
|
-
* ```ts
|
|
854
|
-
* import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
|
|
855
|
-
* import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
|
|
856
|
-
* import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
|
|
857
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
858
|
-
*
|
|
859
|
-
* const cache: IGroupableCache = new Cache({
|
|
860
|
-
* adapter: new MemoryCacheAdapter({
|
|
861
|
-
* rootGroup: "@global"
|
|
862
|
-
* }),
|
|
863
|
-
* });
|
|
864
|
-
*
|
|
865
|
-
* await cache.add("a", 1);
|
|
866
|
-
*
|
|
867
|
-
* const result1 = await cache.getOrFail("a");
|
|
868
|
-
* // Will print 1
|
|
869
|
-
* console.log(result1);
|
|
870
|
-
*
|
|
871
|
-
* await cache.remove("a");
|
|
872
|
-
*
|
|
873
|
-
* // Will throw an error
|
|
874
|
-
* await cache.getOrFail("a");
|
|
875
|
-
* ```
|
|
876
|
-
*/
|
|
877
|
-
getOrFail(key) {
|
|
878
|
-
return this.createLayPromise(async () => {
|
|
879
|
-
const value = await this.get(key);
|
|
880
|
-
if (value === null) {
|
|
881
|
-
throw new KeyNotFoundCacheError(`Key "${key}" is not found`);
|
|
882
|
-
}
|
|
883
|
-
return value;
|
|
884
|
-
});
|
|
885
|
-
}
|
|
886
|
-
addMany(values) {
|
|
887
|
-
return this.createLayPromise(async () => {
|
|
888
|
-
if (isObjectEmpty(values)) {
|
|
889
|
-
return {};
|
|
890
|
-
}
|
|
891
|
-
const valuePromises = [];
|
|
892
|
-
for (const key in values) {
|
|
893
|
-
const { value, ttl } = values[key];
|
|
894
|
-
valuePromises.push(this.add(key, value, ttl));
|
|
895
|
-
}
|
|
896
|
-
const returnValues = await Promise.all(valuePromises);
|
|
897
|
-
const result = {};
|
|
898
|
-
for (const [index, key] of Object.keys(values).entries()) {
|
|
899
|
-
const value = returnValues[index];
|
|
900
|
-
if (value === undefined) {
|
|
901
|
-
throw new UnexpectedCacheError(`Item "values[${String(index)}]" is undefined`);
|
|
902
|
-
}
|
|
903
|
-
result[key] = value;
|
|
904
|
-
}
|
|
905
|
-
return result;
|
|
906
|
-
});
|
|
907
|
-
}
|
|
908
|
-
updateMany(values) {
|
|
909
|
-
return this.createLayPromise(async () => {
|
|
910
|
-
if (isObjectEmpty(values)) {
|
|
911
|
-
return {};
|
|
912
|
-
}
|
|
913
|
-
const valuePromises = [];
|
|
914
|
-
for (const key in values) {
|
|
915
|
-
const value = values[key];
|
|
916
|
-
valuePromises.push(this.update(key, value));
|
|
917
|
-
}
|
|
918
|
-
const returnValues = await Promise.all(valuePromises);
|
|
919
|
-
const result = {};
|
|
920
|
-
for (const [index, key] of Object.keys(values).entries()) {
|
|
921
|
-
const value = returnValues[index];
|
|
922
|
-
if (value === undefined) {
|
|
923
|
-
throw new UnexpectedCacheError(`Item "values[${String(index)}]" is undefined`);
|
|
924
|
-
}
|
|
925
|
-
result[key] = value;
|
|
926
|
-
}
|
|
927
|
-
return result;
|
|
928
|
-
});
|
|
929
|
-
}
|
|
930
|
-
putMany(values) {
|
|
931
|
-
return this.createLayPromise(async () => {
|
|
932
|
-
if (isObjectEmpty(values)) {
|
|
933
|
-
return {};
|
|
934
|
-
}
|
|
935
|
-
const valuePromises = [];
|
|
936
|
-
for (const key in values) {
|
|
937
|
-
const { value, ttl } = values[key];
|
|
938
|
-
valuePromises.push(this.put(key, value, ttl));
|
|
939
|
-
}
|
|
940
|
-
const returnValues = await Promise.all(valuePromises);
|
|
941
|
-
const result = {};
|
|
942
|
-
for (const [index, key] of Object.keys(values).entries()) {
|
|
943
|
-
const value = returnValues[index];
|
|
944
|
-
if (value === undefined) {
|
|
945
|
-
throw new UnexpectedCacheError(`Item "values[${String(index)}]" is undefined`);
|
|
946
|
-
}
|
|
947
|
-
result[key] = value;
|
|
948
|
-
}
|
|
949
|
-
return result;
|
|
950
|
-
});
|
|
951
|
-
}
|
|
952
|
-
removeMany(keys) {
|
|
953
|
-
return this.createLayPromise(async () => {
|
|
954
|
-
if (keys.length === 0) {
|
|
955
|
-
return {};
|
|
956
|
-
}
|
|
957
|
-
const valuePromises = [];
|
|
958
|
-
for (const key of keys) {
|
|
959
|
-
valuePromises.push(this.remove(key));
|
|
960
|
-
}
|
|
961
|
-
const values = await Promise.all(valuePromises);
|
|
962
|
-
const result = {};
|
|
963
|
-
for (const [index, key] of keys.entries()) {
|
|
964
|
-
const value = values[index];
|
|
965
|
-
if (value === undefined) {
|
|
966
|
-
throw new UnexpectedCacheError(`Item "values[${String(index)}]" is undefined`);
|
|
967
|
-
}
|
|
968
|
-
result[key] = value;
|
|
969
|
-
}
|
|
970
|
-
return result;
|
|
971
|
-
});
|
|
972
|
-
}
|
|
973
|
-
/**
|
|
974
|
-
* @example
|
|
975
|
-
* ```ts
|
|
976
|
-
* import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
|
|
977
|
-
* import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
|
|
978
|
-
* import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
|
|
979
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
980
|
-
*
|
|
981
|
-
* const cache: IGroupableCache = new Cache({
|
|
982
|
-
* adapter: new MemoryCacheAdapter({
|
|
983
|
-
* rootGroup: "@global"
|
|
984
|
-
* }),
|
|
985
|
-
* });
|
|
986
|
-
*
|
|
987
|
-
* const result1 = await cache.getAndRemove("a");
|
|
988
|
-
* // Will print null
|
|
989
|
-
* console.log(result1);
|
|
990
|
-
*
|
|
991
|
-
* await cache.add("a", 2)
|
|
774
|
+
* // Will log null because the cache is not in a group
|
|
775
|
+
* console.log(cache.getGroup());
|
|
992
776
|
*
|
|
993
|
-
* const
|
|
994
|
-
* // Will print 2
|
|
995
|
-
* console.log(result2);
|
|
777
|
+
* const groupedCache = cache.withGroup("group-a");
|
|
996
778
|
*
|
|
997
|
-
*
|
|
998
|
-
*
|
|
999
|
-
* console.log(result3);
|
|
779
|
+
* // Will log "group-a" because the groupedCache is in a group
|
|
780
|
+
* console.log(groupedCache.getGroup());
|
|
1000
781
|
* ```
|
|
1001
782
|
*/
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
}
|
|
1008
|
-
await this.remove(key);
|
|
1009
|
-
return value;
|
|
1010
|
-
});
|
|
783
|
+
getGroup() {
|
|
784
|
+
if (this.keyPrefixer.resolvedGroup) {
|
|
785
|
+
return this.keyPrefixer.resolvedGroup;
|
|
786
|
+
}
|
|
787
|
+
return null;
|
|
1011
788
|
}
|
|
1012
789
|
/**
|
|
1013
790
|
* @example
|
|
1014
791
|
* ```ts
|
|
1015
|
-
* import
|
|
1016
|
-
* import
|
|
1017
|
-
* import {
|
|
1018
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
1019
|
-
*
|
|
1020
|
-
* const cache: IGroupableCache = new Cache({
|
|
1021
|
-
* adapter: new MemoryCacheAdapter({
|
|
1022
|
-
* rootGroup: "@global"
|
|
1023
|
-
* }),
|
|
1024
|
-
* });
|
|
1025
|
-
*
|
|
1026
|
-
* const result = await cache.getOrAdd("a", -1);
|
|
1027
|
-
* // -1
|
|
1028
|
-
* console.log(result);
|
|
1029
|
-
* ```
|
|
1030
|
-
*
|
|
1031
|
-
* You can pass function as default value.
|
|
1032
|
-
* @example
|
|
1033
|
-
* ```ts
|
|
1034
|
-
* import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
|
|
1035
|
-
* import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
|
|
1036
|
-
* import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
|
|
1037
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
1038
|
-
*
|
|
1039
|
-
* const cache: IGroupableCache = new Cache({
|
|
1040
|
-
* adapter: new MemoryCacheAdapter({
|
|
1041
|
-
* rootGroup: "@global"
|
|
1042
|
-
* }),
|
|
1043
|
-
* });
|
|
1044
|
-
*
|
|
1045
|
-
* const result = await cache.getOrAdd("a", () => -1);
|
|
1046
|
-
* // -1
|
|
1047
|
-
* console.log(result);
|
|
1048
|
-
* ```
|
|
1049
|
-
*
|
|
1050
|
-
* You can pass async function as default value.
|
|
1051
|
-
* @example
|
|
1052
|
-
* ```ts
|
|
1053
|
-
* import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
|
|
1054
|
-
* import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
|
|
1055
|
-
* import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
|
|
1056
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
792
|
+
* import { Cache } from "@daiso-tech/core/cache";
|
|
793
|
+
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/adapters";
|
|
794
|
+
* import { KeyPrefixer } from "@daiso-tech/core/utilities";
|
|
1057
795
|
*
|
|
1058
|
-
* const cache
|
|
1059
|
-
* adapter: new MemoryCacheAdapter(
|
|
1060
|
-
*
|
|
1061
|
-
* }),
|
|
796
|
+
* const cache = new Cache({
|
|
797
|
+
* adapter: new MemoryCacheAdapter(),
|
|
798
|
+
* keyPrefixer: new KeyPrefixer("cache")
|
|
1062
799
|
* });
|
|
1063
800
|
*
|
|
1064
|
-
* const
|
|
1065
|
-
*
|
|
1066
|
-
* console.log(result);
|
|
1067
|
-
* ```
|
|
801
|
+
* const groupedCache = cache.withGroup("group-a");
|
|
802
|
+
* await groupedCache.add("a", 1);
|
|
1068
803
|
*
|
|
1069
|
-
*
|
|
1070
|
-
*
|
|
1071
|
-
* ```ts
|
|
1072
|
-
* import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
|
|
1073
|
-
* import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
|
|
1074
|
-
* import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
|
|
1075
|
-
* import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
|
|
1076
|
-
*
|
|
1077
|
-
* const cache: IGroupableCache = new Cache({
|
|
1078
|
-
* adapter: new MemoryCacheAdapter({
|
|
1079
|
-
* rootGroup: "@global"
|
|
1080
|
-
* }),
|
|
1081
|
-
* });
|
|
804
|
+
* // Will log "a".
|
|
805
|
+
* console.log(await groupedCache.get("a"));
|
|
1082
806
|
*
|
|
1083
|
-
*
|
|
1084
|
-
*
|
|
1085
|
-
* console.log(result);
|
|
807
|
+
* // Will log null because the caches are in different groups.
|
|
808
|
+
* console.log(await cache.get("a"));
|
|
1086
809
|
* ```
|
|
1087
810
|
*/
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
return simplifiedValueToAdd;
|
|
1099
|
-
}
|
|
1100
|
-
return value;
|
|
1101
|
-
});
|
|
1102
|
-
}
|
|
1103
|
-
decrement(key, value = 1) {
|
|
1104
|
-
return this.createLayPromise(async () => {
|
|
1105
|
-
return await this.increment(key, -value);
|
|
811
|
+
withGroup(group) {
|
|
812
|
+
const cache = new Cache({
|
|
813
|
+
keyPrefixer: this.keyPrefixer.withGroup(group),
|
|
814
|
+
adapter: this.adapterFactoryable,
|
|
815
|
+
eventBus: this.groupdEventBus,
|
|
816
|
+
defaultTtl: this.defaultTtl,
|
|
817
|
+
retryAttempts: this.retryAttempts,
|
|
818
|
+
backoffPolicy: this.backoffPolicy,
|
|
819
|
+
retryPolicy: this.retryPolicy,
|
|
820
|
+
retryTimeout: this.retryTimeout,
|
|
1106
821
|
});
|
|
822
|
+
return cache;
|
|
1107
823
|
}
|
|
1108
824
|
}
|
|
1109
825
|
//# sourceMappingURL=cache.js.map
|