@travetto/cache 8.0.0-alpha.1 → 8.0.0-alpha.10

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 CHANGED
@@ -40,7 +40,7 @@ Currently, the following are packages that provide [Expiry](https://github.com/t
40
40
  ## Decorators
41
41
  The caching framework provides method decorators that enables simple use cases. One of the requirements to use the caching decorators is that the method arguments, and return values need to be serializable into [JSON](https://www.json.org). Any other data types are not currently supported and would require either manual usage of the caching services directly, or specification of serialization/deserialization routines in the cache config.
42
42
 
43
- Additionally, to use the decorators you will need to have a [CacheService](https://github.com/travetto/travetto/tree/main/module/cache/src/service.ts#L32) object accessible on the class instance. This can be dependency injected, or manually constructed. The decorators will detect the field at time of method execution, which decouples construction of your class from the cache construction.
43
+ Additionally, to use the decorators you will need to have a [CacheService](https://github.com/travetto/travetto/tree/main/module/cache/src/service.ts#L35) object accessible on the class instance. This can be dependency injected, or manually constructed. The decorators will detect the field at time of method execution, which decouples construction of your class from the cache construction.
44
44
 
45
45
  [@Cache](https://github.com/travetto/travetto/tree/main/module/cache/src/decorator.ts#L12) is a decorator that will cache all successful results, keyed by a computation based on the method arguments. Given the desire for supporting remote caches (e.g. [redis](https://redis.io), [memcached](https://memcached.org)), only asynchronous methods are supported.
46
46
 
@@ -119,7 +119,7 @@ export class UserService {
119
119
  ```
120
120
 
121
121
  ## Extending the Cache Service
122
- By design, the [CacheService](https://github.com/travetto/travetto/tree/main/module/cache/src/service.ts#L32) relies solely on the [Data Modeling Support](https://github.com/travetto/travetto/tree/main/module/model#readme "Datastore abstraction for core operations.") module. Specifically on the [Expiry](https://github.com/travetto/travetto/tree/main/module/model/src/types/expiry.ts#L10). This combines basic support for CRUD as well as knowledge of how to manage expirable content. Any model service that honors these contracts is a valid candidate to power the [CacheService](https://github.com/travetto/travetto/tree/main/module/cache/src/service.ts#L32). The [CacheService](https://github.com/travetto/travetto/tree/main/module/cache/src/service.ts#L32) is expecting the model service to be registered using the @travetto/cache:model:
122
+ By design, the [CacheService](https://github.com/travetto/travetto/tree/main/module/cache/src/service.ts#L35) relies solely on the [Data Modeling Support](https://github.com/travetto/travetto/tree/main/module/model#readme "Datastore abstraction for core operations.") module. Specifically on the [Expiry](https://github.com/travetto/travetto/tree/main/module/model/src/types/expiry.ts#L10). This combines basic support for CRUD as well as knowledge of how to manage expirable content. Any model service that honors these contracts is a valid candidate to power the [CacheService](https://github.com/travetto/travetto/tree/main/module/cache/src/service.ts#L35). The [CacheService](https://github.com/travetto/travetto/tree/main/module/cache/src/service.ts#L35) is expecting the model service to be registered using the @travetto/cache:model:
123
123
 
124
124
  **Code: Registering a Custom Model Source**
125
125
  ```typescript
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/cache",
3
- "version": "8.0.0-alpha.1",
3
+ "version": "8.0.0-alpha.10",
4
4
  "type": "module",
5
5
  "description": "Caching functionality with decorators for declarative use.",
6
6
  "keywords": [
@@ -26,12 +26,12 @@
26
26
  "directory": "module/cache"
27
27
  },
28
28
  "dependencies": {
29
- "@travetto/di": "^8.0.0-alpha.1",
30
- "@travetto/model": "^8.0.0-alpha.1"
29
+ "@travetto/di": "^8.0.0-alpha.10",
30
+ "@travetto/model": "^8.0.0-alpha.10"
31
31
  },
32
32
  "peerDependencies": {
33
- "@travetto/test": "^8.0.0-alpha.1",
34
- "@travetto/transformer": "^8.0.0-alpha.1"
33
+ "@travetto/test": "^8.0.0-alpha.10",
34
+ "@travetto/transformer": "^8.0.0-alpha.5"
35
35
  },
36
36
  "peerDependenciesMeta": {
37
37
  "@travetto/transformer": {
package/src/service.ts CHANGED
@@ -1,5 +1,6 @@
1
- import { ExpiresAt, Index, Model, type ModelExpirySupport, NotFoundError, ModelStorageUtil, ModelIndexedUtil } from '@travetto/model';
2
- import { Text } from '@travetto/schema';
1
+ import { ExpiresAt, Model, type ModelExpirySupport, NotFoundError, ModelStorageUtil } from '@travetto/model';
2
+ import { ModelIndexedUtil, sortedIndex } from '@travetto/model-indexed';
3
+ import { MaxLength, Text } from '@travetto/schema';
3
4
  import { Inject, Injectable } from '@travetto/di';
4
5
  import { RuntimeError, JSONUtil, TimeUtil } from '@travetto/runtime';
5
6
 
@@ -9,22 +10,24 @@ import { type CacheAware, CacheConfigSymbol, CacheModelSymbol, EvictConfigSymbol
9
10
 
10
11
  const INFINITE_MAX_AGE = TimeUtil.duration('10y', 'ms');
11
12
 
12
- @Index({
13
- name: 'keySpace',
14
- type: 'unsorted',
15
- fields: [{ keySpace: 1 }]
16
- })
17
13
  @Model({ autoCreate: 'production' })
18
14
  export class CacheRecord {
19
15
  id: string;
20
16
  @Text()
21
17
  entry: string;
18
+ @MaxLength(500)
22
19
  keySpace: string;
23
20
  @ExpiresAt()
24
21
  expiresAt: Date;
25
22
  issuedAt: Date;
26
23
  }
27
24
 
25
+ const keySpaceIndex = sortedIndex(CacheRecord, {
26
+ name: 'keySpace',
27
+ key: { keySpace: true },
28
+ sort: { expiresAt: 1 }
29
+ });
30
+
28
31
  /**
29
32
  * Cache source
30
33
  */
@@ -104,9 +107,14 @@ export class CacheService {
104
107
  async deleteAll(keySpace: string): Promise<void> {
105
108
  if (ModelIndexedUtil.isSupported(this.#modelService)) {
106
109
  const removes: Promise<void>[] = [];
107
- for await (const item of this.#modelService.listByIndex(CacheRecord, 'keySpace', { keySpace })) {
108
- removes.push(this.#modelService.delete(CacheRecord, item.id));
109
- }
110
+ let offset: string | undefined;
111
+ do {
112
+ const { items, nextOffset } = await this.#modelService.listByIndex(CacheRecord, keySpaceIndex, { keySpace }, { offset });
113
+ for (const item of items) {
114
+ removes.push(this.#modelService.delete(CacheRecord, item.id));
115
+ }
116
+ offset = nextOffset;
117
+ } while (offset);
110
118
  await Promise.all(removes);
111
119
  } else {
112
120
  throw new RuntimeError('Unable to delete all on an un-indexed database');
@@ -2,7 +2,8 @@ import assert from 'node:assert';
2
2
  import timers from 'node:timers/promises';
3
3
 
4
4
  import { Suite, Test } from '@travetto/test';
5
- import { type ModelExpirySupport, ModelIndexedUtil } from '@travetto/model';
5
+ import { type ModelExpirySupport } from '@travetto/model';
6
+ import { ModelIndexedUtil } from '@travetto/model-indexed';
6
7
  import { Inject, Injectable } from '@travetto/di';
7
8
  import { castTo, type Class } from '@travetto/runtime';
8
9
  import { Schema } from '@travetto/schema';