@lakutata/cache 3.0.0-beta.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.
Files changed (116) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +175 -0
  3. package/dist/cjs/cache/Cacher.d.ts +446 -0
  4. package/dist/cjs/cache/Cacher.js +523 -0
  5. package/dist/cjs/cache/exceptions/CacheDriverNotFoundException.d.ts +10 -0
  6. package/dist/cjs/cache/exceptions/CacheDriverNotFoundException.js +17 -0
  7. package/dist/cjs/cache/interfaces/CacherOptions.d.ts +42 -0
  8. package/dist/cjs/cache/interfaces/CacherOptions.js +2 -0
  9. package/dist/cjs/cache/lib/LikePrefix.d.ts +5 -0
  10. package/dist/cjs/cache/lib/LikePrefix.js +10 -0
  11. package/dist/cjs/cache/lib/LoadDriver.d.ts +6 -0
  12. package/dist/cjs/cache/lib/LoadDriver.js +66 -0
  13. package/dist/cjs/cache/lib/Serializer.d.ts +18 -0
  14. package/dist/cjs/cache/lib/Serializer.js +46 -0
  15. package/dist/cjs/cache/lib/WithTimeout.d.ts +7 -0
  16. package/dist/cjs/cache/lib/WithTimeout.js +23 -0
  17. package/dist/cjs/cache/options/FileCacheOptions.d.ts +54 -0
  18. package/dist/cjs/cache/options/FileCacheOptions.js +62 -0
  19. package/dist/cjs/cache/options/MemcacheCacheOptions.d.ts +57 -0
  20. package/dist/cjs/cache/options/MemcacheCacheOptions.js +66 -0
  21. package/dist/cjs/cache/options/MemoryCacheOptions.d.ts +39 -0
  22. package/dist/cjs/cache/options/MemoryCacheOptions.js +52 -0
  23. package/dist/cjs/cache/options/MongoCacheOptions.d.ts +72 -0
  24. package/dist/cjs/cache/options/MongoCacheOptions.js +81 -0
  25. package/dist/cjs/cache/options/MysqlCacheOptions.d.ts +73 -0
  26. package/dist/cjs/cache/options/MysqlCacheOptions.js +82 -0
  27. package/dist/cjs/cache/options/PostgresCacheOptions.d.ts +87 -0
  28. package/dist/cjs/cache/options/PostgresCacheOptions.js +92 -0
  29. package/dist/cjs/cache/options/RedisCacheOptions.d.ts +120 -0
  30. package/dist/cjs/cache/options/RedisCacheOptions.js +113 -0
  31. package/dist/cjs/cache/options/SqliteCacheOptions.d.ts +53 -0
  32. package/dist/cjs/cache/options/SqliteCacheOptions.js +61 -0
  33. package/dist/cjs/cache/stores/CacheStore.d.ts +44 -0
  34. package/dist/cjs/cache/stores/CacheStore.js +42 -0
  35. package/dist/cjs/cache/stores/CreateCacheStore.d.ts +7 -0
  36. package/dist/cjs/cache/stores/CreateCacheStore.js +37 -0
  37. package/dist/cjs/cache/stores/FileStore.d.ts +51 -0
  38. package/dist/cjs/cache/stores/FileStore.js +143 -0
  39. package/dist/cjs/cache/stores/MemcacheStore.d.ts +42 -0
  40. package/dist/cjs/cache/stores/MemcacheStore.js +69 -0
  41. package/dist/cjs/cache/stores/MemoryStore.d.ts +34 -0
  42. package/dist/cjs/cache/stores/MemoryStore.js +75 -0
  43. package/dist/cjs/cache/stores/MongoStore.d.ts +55 -0
  44. package/dist/cjs/cache/stores/MongoStore.js +72 -0
  45. package/dist/cjs/cache/stores/MysqlStore.d.ts +27 -0
  46. package/dist/cjs/cache/stores/MysqlStore.js +71 -0
  47. package/dist/cjs/cache/stores/PostgresStore.d.ts +30 -0
  48. package/dist/cjs/cache/stores/PostgresStore.js +85 -0
  49. package/dist/cjs/cache/stores/RedisStore.d.ts +58 -0
  50. package/dist/cjs/cache/stores/RedisStore.js +131 -0
  51. package/dist/cjs/cache/stores/SerializedCacheStore.d.ts +48 -0
  52. package/dist/cjs/cache/stores/SerializedCacheStore.js +80 -0
  53. package/dist/cjs/cache/stores/SqliteStore.d.ts +44 -0
  54. package/dist/cjs/cache/stores/SqliteStore.js +91 -0
  55. package/dist/cjs/cache/types/CacheStoreOptions.d.ts +15 -0
  56. package/dist/cjs/cache/types/CacheStoreOptions.js +2 -0
  57. package/dist/cjs/exports/Cache.d.ts +12 -0
  58. package/dist/cjs/exports/Cache.js +31 -0
  59. package/dist/cjs/package.json +1 -0
  60. package/dist/esm/cache/Cacher.js +518 -0
  61. package/dist/esm/cache/exceptions/CacheDriverNotFoundException.js +13 -0
  62. package/dist/esm/cache/interfaces/CacherOptions.js +1 -0
  63. package/dist/esm/cache/lib/LikePrefix.js +7 -0
  64. package/dist/esm/cache/lib/LoadDriver.js +30 -0
  65. package/dist/esm/cache/lib/Serializer.js +42 -0
  66. package/dist/esm/cache/lib/WithTimeout.js +20 -0
  67. package/dist/esm/cache/options/FileCacheOptions.js +58 -0
  68. package/dist/esm/cache/options/MemcacheCacheOptions.js +62 -0
  69. package/dist/esm/cache/options/MemoryCacheOptions.js +48 -0
  70. package/dist/esm/cache/options/MongoCacheOptions.js +77 -0
  71. package/dist/esm/cache/options/MysqlCacheOptions.js +78 -0
  72. package/dist/esm/cache/options/PostgresCacheOptions.js +88 -0
  73. package/dist/esm/cache/options/RedisCacheOptions.js +109 -0
  74. package/dist/esm/cache/options/SqliteCacheOptions.js +57 -0
  75. package/dist/esm/cache/stores/CacheStore.js +36 -0
  76. package/dist/esm/cache/stores/CreateCacheStore.js +34 -0
  77. package/dist/esm/cache/stores/FileStore.js +136 -0
  78. package/dist/esm/cache/stores/MemcacheStore.js +65 -0
  79. package/dist/esm/cache/stores/MemoryStore.js +71 -0
  80. package/dist/esm/cache/stores/MongoStore.js +68 -0
  81. package/dist/esm/cache/stores/MysqlStore.js +67 -0
  82. package/dist/esm/cache/stores/PostgresStore.js +81 -0
  83. package/dist/esm/cache/stores/RedisStore.js +127 -0
  84. package/dist/esm/cache/stores/SerializedCacheStore.js +76 -0
  85. package/dist/esm/cache/stores/SqliteStore.js +87 -0
  86. package/dist/esm/cache/types/CacheStoreOptions.js +1 -0
  87. package/dist/esm/exports/Cache.js +12 -0
  88. package/dist/types/cache/Cacher.d.ts +446 -0
  89. package/dist/types/cache/exceptions/CacheDriverNotFoundException.d.ts +10 -0
  90. package/dist/types/cache/interfaces/CacherOptions.d.ts +42 -0
  91. package/dist/types/cache/lib/LikePrefix.d.ts +5 -0
  92. package/dist/types/cache/lib/LoadDriver.d.ts +6 -0
  93. package/dist/types/cache/lib/Serializer.d.ts +18 -0
  94. package/dist/types/cache/lib/WithTimeout.d.ts +7 -0
  95. package/dist/types/cache/options/FileCacheOptions.d.ts +54 -0
  96. package/dist/types/cache/options/MemcacheCacheOptions.d.ts +57 -0
  97. package/dist/types/cache/options/MemoryCacheOptions.d.ts +39 -0
  98. package/dist/types/cache/options/MongoCacheOptions.d.ts +72 -0
  99. package/dist/types/cache/options/MysqlCacheOptions.d.ts +73 -0
  100. package/dist/types/cache/options/PostgresCacheOptions.d.ts +87 -0
  101. package/dist/types/cache/options/RedisCacheOptions.d.ts +120 -0
  102. package/dist/types/cache/options/SqliteCacheOptions.d.ts +53 -0
  103. package/dist/types/cache/stores/CacheStore.d.ts +44 -0
  104. package/dist/types/cache/stores/CreateCacheStore.d.ts +7 -0
  105. package/dist/types/cache/stores/FileStore.d.ts +51 -0
  106. package/dist/types/cache/stores/MemcacheStore.d.ts +42 -0
  107. package/dist/types/cache/stores/MemoryStore.d.ts +34 -0
  108. package/dist/types/cache/stores/MongoStore.d.ts +55 -0
  109. package/dist/types/cache/stores/MysqlStore.d.ts +27 -0
  110. package/dist/types/cache/stores/PostgresStore.d.ts +30 -0
  111. package/dist/types/cache/stores/RedisStore.d.ts +58 -0
  112. package/dist/types/cache/stores/SerializedCacheStore.d.ts +48 -0
  113. package/dist/types/cache/stores/SqliteStore.d.ts +44 -0
  114. package/dist/types/cache/types/CacheStoreOptions.d.ts +15 -0
  115. package/dist/types/exports/Cache.d.ts +12 -0
  116. package/package.json +72 -0
@@ -0,0 +1,58 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { DTO } from '@lakutata/core';
11
+ import { Expect } from '@lakutata/core/decorator/dto';
12
+ /**
13
+ * The options of the file store: the entries are kept in memory and saved to one JSON file (loaded when the cache
14
+ * starts, written in batches, flushed when the cache is destroyed), so they survive a restart. It needs no optional
15
+ * dependency.
16
+ *
17
+ * The file is rewritten whole on each save (through a temporary file renamed over it), so it suits small caches;
18
+ * the processes sharing one file overwrite each other's entries. An unreadable file (not JSON) fails the cache's
19
+ * creation; a failed save is emitted as an `'error'` event of the cache. The values are kept as JSON.
20
+ * @example
21
+ * ```typescript
22
+ * import {Application} from 'lakutata'
23
+ * import {buildCacherOptions} from 'lakutata/com/cacher'
24
+ *
25
+ * Application.run(() => ({
26
+ * id: 'file-cache.app',
27
+ * name: 'FileCache',
28
+ * components: {
29
+ * cache: buildCacherOptions({
30
+ * stores: {type: 'file', filename: './cache/data.json', namespace: 'app', writeDelay: 500},
31
+ * ttl: 3600000
32
+ * })
33
+ * }
34
+ * }))
35
+ * ```
36
+ */
37
+ export class FileCacheOptions extends DTO {
38
+ }
39
+ __decorate([
40
+ Expect(DTO.String().allow('file').only().required()),
41
+ __metadata("design:type", String)
42
+ ], FileCacheOptions.prototype, "type", void 0);
43
+ __decorate([
44
+ Expect(DTO.String().required()),
45
+ __metadata("design:type", String)
46
+ ], FileCacheOptions.prototype, "filename", void 0);
47
+ __decorate([
48
+ Expect(DTO.Number().positive().optional().default(3600 * 1000)),
49
+ __metadata("design:type", Number)
50
+ ], FileCacheOptions.prototype, "expiredCheckDelay", void 0);
51
+ __decorate([
52
+ Expect(DTO.Number().positive().allow(0).optional().default(100)),
53
+ __metadata("design:type", Number)
54
+ ], FileCacheOptions.prototype, "writeDelay", void 0);
55
+ __decorate([
56
+ Expect(DTO.String().optional()),
57
+ __metadata("design:type", String)
58
+ ], FileCacheOptions.prototype, "namespace", void 0);
@@ -0,0 +1,62 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { DTO } from '@lakutata/core';
11
+ import { Expect } from '@lakutata/core/decorator/dto';
12
+ /**
13
+ * The options of the Memcache store, a memcached server. It needs the optional peer dependency `memjs`
14
+ * (`npm install memjs`): without it, the cache's creation fails with `CacheDriverNotFoundException`.
15
+ *
16
+ * The client connects on its first command, so an unreachable server does not fail the cache's creation: its reads
17
+ * are misses (emitted as `'error'` events), its writes reject. The TTLs are rounded up to the second by memcached
18
+ * (an entry is a miss once its TTL in milliseconds has passed). memcached cannot list its keys: `clear()` flushes the
19
+ * whole server, the entries of the other namespaces and applications included. The values are kept as JSON.
20
+ * @example
21
+ * ```typescript
22
+ * import {Application} from 'lakutata'
23
+ * import {buildCacherOptions} from 'lakutata/com/cacher'
24
+ *
25
+ * Application.run(() => ({
26
+ * id: 'memcache.app',
27
+ * name: 'Memcache',
28
+ * components: {
29
+ * cache: buildCacherOptions({
30
+ * stores: {type: 'memcache', host: '127.0.0.1', port: 11211, namespace: 'app'},
31
+ * ttl: 60000
32
+ * })
33
+ * }
34
+ * }))
35
+ * ```
36
+ */
37
+ export class MemcacheCacheOptions extends DTO {
38
+ }
39
+ __decorate([
40
+ Expect(DTO.String().allow('memcache').only().required()),
41
+ __metadata("design:type", String)
42
+ ], MemcacheCacheOptions.prototype, "type", void 0);
43
+ __decorate([
44
+ Expect(DTO.String().required()),
45
+ __metadata("design:type", String)
46
+ ], MemcacheCacheOptions.prototype, "host", void 0);
47
+ __decorate([
48
+ Expect(DTO.Number().positive().port().optional().default(11211)),
49
+ __metadata("design:type", Number)
50
+ ], MemcacheCacheOptions.prototype, "port", void 0);
51
+ __decorate([
52
+ Expect(DTO.String().optional()),
53
+ __metadata("design:type", String)
54
+ ], MemcacheCacheOptions.prototype, "username", void 0);
55
+ __decorate([
56
+ Expect(DTO.String().optional()),
57
+ __metadata("design:type", String)
58
+ ], MemcacheCacheOptions.prototype, "password", void 0);
59
+ __decorate([
60
+ Expect(DTO.String().optional()),
61
+ __metadata("design:type", String)
62
+ ], MemcacheCacheOptions.prototype, "namespace", void 0);
@@ -0,0 +1,48 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { DTO } from '@lakutata/core';
11
+ import { Expect } from '@lakutata/core/decorator/dto';
12
+ /**
13
+ * The options of the in-memory store: the entries are kept in the process (lost when it ends, not shared with the
14
+ * other processes), the values as they are (not copied, not converted to JSON). It needs no optional dependency.
15
+ *
16
+ * It is the store of a cache without `stores`; declared, it is a tier in front of a slower store (the first store of
17
+ * the list is read first, the hits of a later store are written back to the earlier ones).
18
+ * @example
19
+ * ```typescript
20
+ * import {Application} from 'lakutata'
21
+ * import {buildCacherOptions} from 'lakutata/com/cacher'
22
+ *
23
+ * Application.run(() => ({
24
+ * id: 'tiered-cache.app',
25
+ * name: 'TieredCache',
26
+ * components: {
27
+ * //A memory tier in front of Redis
28
+ * cache: buildCacherOptions({
29
+ * stores: [
30
+ * {type: 'memory', sweepInterval: 30000},
31
+ * {type: 'redis', host: '127.0.0.1', namespace: 'app'}
32
+ * ],
33
+ * ttl: 60000
34
+ * })
35
+ * }
36
+ * }))
37
+ * ```
38
+ */
39
+ export class MemoryCacheOptions extends DTO {
40
+ }
41
+ __decorate([
42
+ Expect(DTO.String().allow('memory').only().required()),
43
+ __metadata("design:type", String)
44
+ ], MemoryCacheOptions.prototype, "type", void 0);
45
+ __decorate([
46
+ Expect(DTO.Number().positive().optional().default(60000)),
47
+ __metadata("design:type", Number)
48
+ ], MemoryCacheOptions.prototype, "sweepInterval", void 0);
@@ -0,0 +1,77 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { DTO } from '@lakutata/core';
11
+ import { Expect } from '@lakutata/core/decorator/dto';
12
+ /**
13
+ * The options of the MongoDB store: one document per entry in a collection. It needs the optional peer dependency
14
+ * `mongodb` (`npm install mongodb`): without it, the cache's creation fails with `CacheDriverNotFoundException`.
15
+ *
16
+ * When the cache starts, it connects to `mongodb://{username}:{password}@{host}:{port}` (the user authenticated
17
+ * against the `admin` database; `mongodb://{host}:{port}` without a username) and creates the collection's
18
+ * indexes: a unique one on `key`, and a TTL index on
19
+ * `expiresAt` by which MongoDB deletes the expired documents (its TTL monitor runs about once a minute; an expired
20
+ * document is a miss before). A failed connection fails the cache's creation. The values are kept as JSON.
21
+ * @example
22
+ * ```typescript
23
+ * import {Application} from 'lakutata'
24
+ * import {buildCacherOptions} from 'lakutata/com/cacher'
25
+ *
26
+ * Application.run(() => ({
27
+ * id: 'mongo-cache.app',
28
+ * name: 'MongoCache',
29
+ * components: {
30
+ * cache: buildCacherOptions({
31
+ * stores: {
32
+ * type: 'mongo',
33
+ * host: '127.0.0.1',
34
+ * database: 'app',
35
+ * collection: 'cache',
36
+ * username: 'app',
37
+ * password: process.env.MONGO_PASSWORD ?? ''
38
+ * }
39
+ * })
40
+ * }
41
+ * }))
42
+ * ```
43
+ */
44
+ export class MongoCacheOptions extends DTO {
45
+ }
46
+ __decorate([
47
+ Expect(DTO.String().allow('mongo').only().required()),
48
+ __metadata("design:type", String)
49
+ ], MongoCacheOptions.prototype, "type", void 0);
50
+ __decorate([
51
+ Expect(DTO.String().required()),
52
+ __metadata("design:type", String)
53
+ ], MongoCacheOptions.prototype, "host", void 0);
54
+ __decorate([
55
+ Expect(DTO.String().required()),
56
+ __metadata("design:type", String)
57
+ ], MongoCacheOptions.prototype, "database", void 0);
58
+ __decorate([
59
+ Expect(DTO.String().required()),
60
+ __metadata("design:type", String)
61
+ ], MongoCacheOptions.prototype, "collection", void 0);
62
+ __decorate([
63
+ Expect(DTO.Number().positive().port().optional().default(27017)),
64
+ __metadata("design:type", Number)
65
+ ], MongoCacheOptions.prototype, "port", void 0);
66
+ __decorate([
67
+ Expect(DTO.String().optional()),
68
+ __metadata("design:type", String)
69
+ ], MongoCacheOptions.prototype, "username", void 0);
70
+ __decorate([
71
+ Expect(DTO.String().allow('').optional()),
72
+ __metadata("design:type", String)
73
+ ], MongoCacheOptions.prototype, "password", void 0);
74
+ __decorate([
75
+ Expect(DTO.String().optional()),
76
+ __metadata("design:type", String)
77
+ ], MongoCacheOptions.prototype, "namespace", void 0);
@@ -0,0 +1,78 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { DTO } from '@lakutata/core';
11
+ import { Expect } from '@lakutata/core/decorator/dto';
12
+ /**
13
+ * The options of the MySQL store (MySQL or MariaDB): one row per entry in a table. It needs the optional peer
14
+ * dependency `mysql2` (`npm install mysql2`): without it, the cache's creation fails with
15
+ * `CacheDriverNotFoundException`.
16
+ *
17
+ * When the cache starts, it creates the table when missing: `(id VARCHAR(255) PRIMARY KEY, value MEDIUMTEXT)`, the
18
+ * keys case sensitive (`utf8mb4_bin`); a failure (an unreachable server, a denied creation) fails the cache's
19
+ * creation. A key longer than 255 characters, its namespace prefix included, fails its write (in the strict SQL mode,
20
+ * the default), and a value is limited to 16 MB of JSON. An expired row is a miss; it stays in the table until it is read, overwritten or cleared. The
21
+ * values are kept as JSON.
22
+ * @example
23
+ * ```typescript
24
+ * import {Application} from 'lakutata'
25
+ * import {buildCacherOptions} from 'lakutata/com/cacher'
26
+ *
27
+ * Application.run(() => ({
28
+ * id: 'mysql-cache.app',
29
+ * name: 'MysqlCache',
30
+ * components: {
31
+ * cache: buildCacherOptions({
32
+ * stores: {
33
+ * type: 'mysql',
34
+ * host: '127.0.0.1',
35
+ * database: 'app',
36
+ * table: 'cache',
37
+ * username: 'app',
38
+ * password: process.env.MYSQL_PASSWORD ?? ''
39
+ * }
40
+ * })
41
+ * }
42
+ * }))
43
+ * ```
44
+ */
45
+ export class MysqlCacheOptions extends DTO {
46
+ }
47
+ __decorate([
48
+ Expect(DTO.String().allow('mysql').only().required()),
49
+ __metadata("design:type", String)
50
+ ], MysqlCacheOptions.prototype, "type", void 0);
51
+ __decorate([
52
+ Expect(DTO.String().required()),
53
+ __metadata("design:type", String)
54
+ ], MysqlCacheOptions.prototype, "host", void 0);
55
+ __decorate([
56
+ Expect(DTO.String().required()),
57
+ __metadata("design:type", String)
58
+ ], MysqlCacheOptions.prototype, "database", void 0);
59
+ __decorate([
60
+ Expect(DTO.String().required()),
61
+ __metadata("design:type", String)
62
+ ], MysqlCacheOptions.prototype, "table", void 0);
63
+ __decorate([
64
+ Expect(DTO.Number().positive().port().optional().default(3306)),
65
+ __metadata("design:type", Number)
66
+ ], MysqlCacheOptions.prototype, "port", void 0);
67
+ __decorate([
68
+ Expect(DTO.String().optional()),
69
+ __metadata("design:type", String)
70
+ ], MysqlCacheOptions.prototype, "username", void 0);
71
+ __decorate([
72
+ Expect(DTO.String().allow('').optional()),
73
+ __metadata("design:type", String)
74
+ ], MysqlCacheOptions.prototype, "password", void 0);
75
+ __decorate([
76
+ Expect(DTO.String().optional()),
77
+ __metadata("design:type", String)
78
+ ], MysqlCacheOptions.prototype, "namespace", void 0);
@@ -0,0 +1,88 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { DTO } from '@lakutata/core';
11
+ import { Expect } from '@lakutata/core/decorator/dto';
12
+ /**
13
+ * The options of the PostgreSQL store: one row per entry in a table. It needs the optional peer dependency `pg`
14
+ * (`npm install pg`): without it, the cache's creation fails with `CacheDriverNotFoundException`.
15
+ *
16
+ * When the cache starts, it creates the schema (other than `public`) and the table when missing:
17
+ * `(key VARCHAR(255) PRIMARY KEY, value TEXT)`; a failure (an unreachable server, a denied creation) fails the cache's
18
+ * creation. A key longer than 255 characters, its namespace prefix included, fails its write. An expired row is a
19
+ * miss; it stays in the table until it is read, overwritten or cleared. The errors of the idle connections are emitted
20
+ * as `'error'` events of the cache. The values are kept as JSON.
21
+ * @example
22
+ * ```typescript
23
+ * import {Application} from 'lakutata'
24
+ * import {buildCacherOptions} from 'lakutata/com/cacher'
25
+ *
26
+ * Application.run(() => ({
27
+ * id: 'postgres-cache.app',
28
+ * name: 'PostgresCache',
29
+ * components: {
30
+ * cache: buildCacherOptions({
31
+ * stores: {
32
+ * type: 'postgres',
33
+ * host: '127.0.0.1',
34
+ * database: 'app',
35
+ * schema: 'cache',
36
+ * table: 'entries',
37
+ * username: 'app',
38
+ * password: process.env.PGPASSWORD ?? '',
39
+ * maxPoolSize: 5
40
+ * },
41
+ * ttl: 600000
42
+ * })
43
+ * }
44
+ * }))
45
+ * ```
46
+ */
47
+ export class PostgresCacheOptions extends DTO {
48
+ }
49
+ __decorate([
50
+ Expect(DTO.String().allow('postgres').only().required()),
51
+ __metadata("design:type", String)
52
+ ], PostgresCacheOptions.prototype, "type", void 0);
53
+ __decorate([
54
+ Expect(DTO.String().required()),
55
+ __metadata("design:type", String)
56
+ ], PostgresCacheOptions.prototype, "host", void 0);
57
+ __decorate([
58
+ Expect(DTO.String().required()),
59
+ __metadata("design:type", String)
60
+ ], PostgresCacheOptions.prototype, "database", void 0);
61
+ __decorate([
62
+ Expect(DTO.String().required()),
63
+ __metadata("design:type", String)
64
+ ], PostgresCacheOptions.prototype, "table", void 0);
65
+ __decorate([
66
+ Expect(DTO.Number().positive().port().optional().default(5432)),
67
+ __metadata("design:type", Number)
68
+ ], PostgresCacheOptions.prototype, "port", void 0);
69
+ __decorate([
70
+ Expect(DTO.String().optional().default('public')),
71
+ __metadata("design:type", String)
72
+ ], PostgresCacheOptions.prototype, "schema", void 0);
73
+ __decorate([
74
+ Expect(DTO.String().optional()),
75
+ __metadata("design:type", String)
76
+ ], PostgresCacheOptions.prototype, "username", void 0);
77
+ __decorate([
78
+ Expect(DTO.String().allow('').optional()),
79
+ __metadata("design:type", String)
80
+ ], PostgresCacheOptions.prototype, "password", void 0);
81
+ __decorate([
82
+ Expect(DTO.Number().positive().integer().optional()),
83
+ __metadata("design:type", Number)
84
+ ], PostgresCacheOptions.prototype, "maxPoolSize", void 0);
85
+ __decorate([
86
+ Expect(DTO.String().optional()),
87
+ __metadata("design:type", String)
88
+ ], PostgresCacheOptions.prototype, "namespace", void 0);
@@ -0,0 +1,109 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { DTO } from '@lakutata/core';
11
+ import { Expect } from '@lakutata/core/decorator/dto';
12
+ /**
13
+ * The options of the Redis store (Redis, Valkey or a compatible server). It needs the optional peer dependency
14
+ * `redis` (`npm install redis`, version 4 or 5; `@redis/client` is used when `redis` is not installed): without it,
15
+ * the cache's creation fails with `CacheDriverNotFoundException`.
16
+ *
17
+ * The store connects when the cache starts. While it is disconnected, its commands fail at once instead of waiting:
18
+ * the reads are misses (emitted as `'error'` events), the writes reject. The connection errors are emitted as
19
+ * `'error'` events of the cache. The entries expire in Redis (`PSETEX`). The values are kept as JSON.
20
+ * @example
21
+ * ```typescript
22
+ * import {Application} from 'lakutata'
23
+ * import {buildCacherOptions} from 'lakutata/com/cacher'
24
+ *
25
+ * Application.run(() => ({
26
+ * id: 'redis-cache.app',
27
+ * name: 'RedisCache',
28
+ * components: {
29
+ * cache: buildCacherOptions({
30
+ * stores: {
31
+ * type: 'redis',
32
+ * host: '127.0.0.1',
33
+ * password: process.env.REDIS_PASSWORD,
34
+ * namespace: 'app',
35
+ * //Start even when Redis is down, and keep reconnecting
36
+ * throwOnConnectError: false
37
+ * },
38
+ * ttl: 60000
39
+ * })
40
+ * }
41
+ * }))
42
+ * ```
43
+ */
44
+ export class RedisCacheOptions extends DTO {
45
+ }
46
+ __decorate([
47
+ Expect(DTO.String().allow('redis').only().required()),
48
+ __metadata("design:type", String)
49
+ ], RedisCacheOptions.prototype, "type", void 0);
50
+ __decorate([
51
+ Expect(DTO.String().required()),
52
+ __metadata("design:type", String)
53
+ ], RedisCacheOptions.prototype, "host", void 0);
54
+ __decorate([
55
+ Expect(DTO.Number().positive().port().optional().default(6379)),
56
+ __metadata("design:type", Number)
57
+ ], RedisCacheOptions.prototype, "port", void 0);
58
+ __decorate([
59
+ Expect(DTO.Number().positive().allow(0).default(0).optional()),
60
+ __metadata("design:type", Number)
61
+ ], RedisCacheOptions.prototype, "database", void 0);
62
+ __decorate([
63
+ Expect(DTO.Boolean().optional().default(false)),
64
+ __metadata("design:type", Boolean)
65
+ ], RedisCacheOptions.prototype, "tls", void 0);
66
+ __decorate([
67
+ Expect(DTO.Number().positive().allow(0).optional()),
68
+ __metadata("design:type", Number)
69
+ ], RedisCacheOptions.prototype, "keepAlive", void 0);
70
+ __decorate([
71
+ Expect(DTO.String().optional()),
72
+ __metadata("design:type", String)
73
+ ], RedisCacheOptions.prototype, "username", void 0);
74
+ __decorate([
75
+ Expect(DTO.String().optional()),
76
+ __metadata("design:type", String)
77
+ ], RedisCacheOptions.prototype, "password", void 0);
78
+ __decorate([
79
+ Expect(DTO.Boolean().optional().default(true)),
80
+ __metadata("design:type", Boolean)
81
+ ], RedisCacheOptions.prototype, "reconnect", void 0);
82
+ __decorate([
83
+ Expect(DTO.String().optional()),
84
+ __metadata("design:type", String)
85
+ ], RedisCacheOptions.prototype, "namespace", void 0);
86
+ __decorate([
87
+ Expect(DTO.String().optional()),
88
+ __metadata("design:type", String)
89
+ ], RedisCacheOptions.prototype, "keyPrefixSeparator", void 0);
90
+ __decorate([
91
+ Expect(DTO.Number().positive().allow(0).optional()),
92
+ __metadata("design:type", Number)
93
+ ], RedisCacheOptions.prototype, "clearBatchSize", void 0);
94
+ __decorate([
95
+ Expect(DTO.Boolean().optional()),
96
+ __metadata("design:type", Boolean)
97
+ ], RedisCacheOptions.prototype, "useUnlink", void 0);
98
+ __decorate([
99
+ Expect(DTO.Boolean().optional()),
100
+ __metadata("design:type", Boolean)
101
+ ], RedisCacheOptions.prototype, "noNamespaceAffectsAll", void 0);
102
+ __decorate([
103
+ Expect(DTO.Number().positive().optional()),
104
+ __metadata("design:type", Number)
105
+ ], RedisCacheOptions.prototype, "connectTimeout", void 0);
106
+ __decorate([
107
+ Expect(DTO.Boolean().optional().default(true)),
108
+ __metadata("design:type", Boolean)
109
+ ], RedisCacheOptions.prototype, "throwOnConnectError", void 0);
@@ -0,0 +1,57 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { DTO } from '@lakutata/core';
11
+ import { Expect } from '@lakutata/core/decorator/dto';
12
+ /**
13
+ * The options of the SQLite store: one row per entry in a table of a SQLite database file. It needs the optional peer
14
+ * dependency `sqlite3` (`npm install sqlite3`): without it, the cache's creation fails with
15
+ * `CacheDriverNotFoundException`.
16
+ *
17
+ * When the cache starts, it opens the database (creating the file) and creates the table when missing:
18
+ * `(key VARCHAR(255) PRIMARY KEY, value TEXT)`. An expired row is a miss; it stays in the table until it is read,
19
+ * overwritten or cleared. The values are kept as JSON.
20
+ * @example
21
+ * ```typescript
22
+ * import {Application} from 'lakutata'
23
+ * import {buildCacherOptions} from 'lakutata/com/cacher'
24
+ *
25
+ * Application.run(() => ({
26
+ * id: 'sqlite-cache.app',
27
+ * name: 'SqliteCache',
28
+ * components: {
29
+ * cache: buildCacherOptions({
30
+ * stores: {type: 'sqlite', database: './cache.db', table: 'cache', namespace: 'app'}
31
+ * })
32
+ * }
33
+ * }))
34
+ * ```
35
+ */
36
+ export class SqliteCacheOptions extends DTO {
37
+ }
38
+ __decorate([
39
+ Expect(DTO.String().allow('sqlite').only().required()),
40
+ __metadata("design:type", String)
41
+ ], SqliteCacheOptions.prototype, "type", void 0);
42
+ __decorate([
43
+ Expect(DTO.String().required()),
44
+ __metadata("design:type", String)
45
+ ], SqliteCacheOptions.prototype, "database", void 0);
46
+ __decorate([
47
+ Expect(DTO.String().required()),
48
+ __metadata("design:type", String)
49
+ ], SqliteCacheOptions.prototype, "table", void 0);
50
+ __decorate([
51
+ Expect(DTO.Number().positive().integer().optional().default(10000)),
52
+ __metadata("design:type", Number)
53
+ ], SqliteCacheOptions.prototype, "busyTimeout", void 0);
54
+ __decorate([
55
+ Expect(DTO.String().optional()),
56
+ __metadata("design:type", String)
57
+ ], SqliteCacheOptions.prototype, "namespace", void 0);
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Whether an entry has expired
3
+ * @param entry
4
+ */
5
+ export function isExpired(entry) {
6
+ return typeof entry.expires === 'number' && Date.now() > entry.expires;
7
+ }
8
+ /**
9
+ * The expiration time of a ttl
10
+ * @param ttl
11
+ */
12
+ export function expiresOf(ttl) {
13
+ return ttl ? Date.now() + ttl : undefined;
14
+ }
15
+ /**
16
+ * A cache store
17
+ * The ttls are in milliseconds, a missing or zero ttl never expires
18
+ */
19
+ export class CacheStore {
20
+ constructor() {
21
+ /**
22
+ * Report the errors of the background operations
23
+ */
24
+ this.onError = () => undefined;
25
+ }
26
+ /**
27
+ * Connect to the backend
28
+ */
29
+ async connect() {
30
+ }
31
+ /**
32
+ * Disconnect from the backend
33
+ */
34
+ async disconnect() {
35
+ }
36
+ }
@@ -0,0 +1,34 @@
1
+ import { FileStore } from './FileStore.js';
2
+ import { MemoryStore } from './MemoryStore.js';
3
+ import { RedisStore } from './RedisStore.js';
4
+ import { MemcacheStore } from './MemcacheStore.js';
5
+ import { MongoStore } from './MongoStore.js';
6
+ import { SqliteStore } from './SqliteStore.js';
7
+ import { PostgresStore } from './PostgresStore.js';
8
+ import { MysqlStore } from './MysqlStore.js';
9
+ /**
10
+ * Create the store of the options
11
+ * @param options
12
+ */
13
+ export function createCacheStore(options) {
14
+ switch (options.type) {
15
+ case 'memory':
16
+ return new MemoryStore(options.sweepInterval);
17
+ case 'file':
18
+ return new FileStore(options);
19
+ case 'redis':
20
+ return new RedisStore(options);
21
+ case 'memcache':
22
+ return new MemcacheStore(options);
23
+ case 'mongo':
24
+ return new MongoStore(options);
25
+ case 'sqlite':
26
+ return new SqliteStore(options);
27
+ case 'postgres':
28
+ return new PostgresStore(options);
29
+ case 'mysql':
30
+ return new MysqlStore(options);
31
+ default:
32
+ return undefined;
33
+ }
34
+ }