-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b0a26e
commit dbee938
Showing
5 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
title: "CacheConfig" | ||
isDefaultIndex: false | ||
generated: true | ||
--- | ||
<!-- This file was generated from the Vendure source. Do not modify. Instead, re-run the "docs:build" script --> | ||
import MemberInfo from '@site/src/components/MemberInfo'; | ||
import GenerationInfo from '@site/src/components/GenerationInfo'; | ||
import MemberDescription from '@site/src/components/MemberDescription'; | ||
|
||
|
||
## CacheConfig | ||
|
||
<GenerationInfo sourceFile="packages/core/src/cache/cache.ts" sourceLine="14" packageName="@vendure/core" since="3.1.0" /> | ||
|
||
Configuration for a new <a href='/reference/typescript-api/cache/#cache'>Cache</a> instance. | ||
|
||
```ts title="Signature" | ||
interface CacheConfig { | ||
getKey: (id: string | number) => string; | ||
options?: SetCacheKeyOptions; | ||
} | ||
``` | ||
|
||
<div className="members-wrapper"> | ||
|
||
### getKey | ||
|
||
<MemberInfo kind="property" type={`(id: string | number) => string`} /> | ||
|
||
A function which generates a cache key from the given id. | ||
This key will be used to store the value in the cache. | ||
|
||
By convention, the key should be namespaced to avoid conflicts. | ||
|
||
*Example* | ||
|
||
```ts | ||
getKey: id => `MyStrategy:getProductVariantIds:${id}`, | ||
``` | ||
### options | ||
|
||
<MemberInfo kind="property" type={`SetCacheKeyOptions`} /> | ||
|
||
Options available when setting the value in the cache. | ||
|
||
|
||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
--- | ||
title: "CacheService" | ||
isDefaultIndex: false | ||
generated: true | ||
--- | ||
<!-- This file was generated from the Vendure source. Do not modify. Instead, re-run the "docs:build" script --> | ||
import MemberInfo from '@site/src/components/MemberInfo'; | ||
import GenerationInfo from '@site/src/components/GenerationInfo'; | ||
import MemberDescription from '@site/src/components/MemberDescription'; | ||
|
||
|
||
## CacheService | ||
|
||
<GenerationInfo sourceFile="packages/core/src/cache/cache.service.ts" sourceLine="20" packageName="@vendure/core" since="3.1.0" /> | ||
|
||
The CacheService is used to cache data in order to optimize performance. | ||
|
||
Internally it makes use of the configured <a href='/reference/typescript-api/cache/cache-strategy#cachestrategy'>CacheStrategy</a> to persist | ||
the cache into a key-value store. | ||
|
||
```ts title="Signature" | ||
class CacheService { | ||
protected cacheStrategy: CacheStrategy; | ||
constructor(configService: ConfigService) | ||
createCache(config: CacheConfig) => Cache; | ||
get(key: string) => Promise<T | undefined>; | ||
set(key: string, value: T, options?: SetCacheKeyOptions) => Promise<void>; | ||
delete(key: string) => Promise<void>; | ||
invalidateTags(tags: string[]) => Promise<void>; | ||
} | ||
``` | ||
|
||
<div className="members-wrapper"> | ||
|
||
### cacheStrategy | ||
|
||
<MemberInfo kind="property" type={`<a href='/reference/typescript-api/cache/cache-strategy#cachestrategy'>CacheStrategy</a>`} /> | ||
|
||
|
||
### constructor | ||
|
||
<MemberInfo kind="method" type={`(configService: ConfigService) => CacheService`} /> | ||
|
||
|
||
### createCache | ||
|
||
<MemberInfo kind="method" type={`(config: <a href='/reference/typescript-api/cache/cache-config#cacheconfig'>CacheConfig</a>) => <a href='/reference/typescript-api/cache/#cache'>Cache</a>`} /> | ||
|
||
Creates a new <a href='/reference/typescript-api/cache/#cache'>Cache</a> instance with the given configuration. | ||
|
||
The `Cache` instance provides a convenience wrapper around the `CacheService` | ||
methods. | ||
### get | ||
|
||
<MemberInfo kind="method" type={`(key: string) => Promise<T | undefined>`} /> | ||
|
||
Gets an item from the cache, or returns undefined if the key is not found, or the | ||
item has expired. | ||
### set | ||
|
||
<MemberInfo kind="method" type={`(key: string, value: T, options?: SetCacheKeyOptions) => Promise<void>`} /> | ||
|
||
Sets a key-value pair in the cache. The value must be serializable, so cannot contain | ||
things like functions, circular data structures, class instances etc. | ||
|
||
Optionally a "time to live" (ttl) can be specified, which means that the key will | ||
be considered stale after that many milliseconds. | ||
### delete | ||
|
||
<MemberInfo kind="method" type={`(key: string) => Promise<void>`} /> | ||
|
||
Deletes an item from the cache. | ||
### invalidateTags | ||
|
||
<MemberInfo kind="method" type={`(tags: string[]) => Promise<void>`} /> | ||
|
||
Deletes all items from the cache which contain at least one matching tag. | ||
|
||
|
||
</div> |
62 changes: 62 additions & 0 deletions
62
docs/docs/reference/typescript-api/cache/cache-strategy.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
--- | ||
title: "CacheStrategy" | ||
isDefaultIndex: false | ||
generated: true | ||
--- | ||
<!-- This file was generated from the Vendure source. Do not modify. Instead, re-run the "docs:build" script --> | ||
import MemberInfo from '@site/src/components/MemberInfo'; | ||
import GenerationInfo from '@site/src/components/GenerationInfo'; | ||
import MemberDescription from '@site/src/components/MemberDescription'; | ||
|
||
|
||
## CacheStrategy | ||
|
||
<GenerationInfo sourceFile="packages/core/src/config/system/cache-strategy.ts" sourceLine="36" packageName="@vendure/core" since="3.1.0" /> | ||
|
||
The CacheStrategy defines how the underlying shared cache mechanism is implemented. | ||
|
||
It is used by the <a href='/reference/typescript-api/cache/cache-service#cacheservice'>CacheService</a> to take care of storage and retrieval of items | ||
from the cache. | ||
|
||
```ts title="Signature" | ||
interface CacheStrategy extends InjectableStrategy { | ||
get<T extends JsonCompatible<T>>(key: string): Promise<T | undefined>; | ||
set<T extends JsonCompatible<T>>(key: string, value: T, options?: SetCacheKeyOptions): Promise<void>; | ||
delete(key: string): Promise<void>; | ||
invalidateTags(tags: string[]): Promise<void>; | ||
} | ||
``` | ||
* Extends: <code><a href='/reference/typescript-api/common/injectable-strategy#injectablestrategy'>InjectableStrategy</a></code> | ||
|
||
|
||
|
||
<div className="members-wrapper"> | ||
|
||
### get | ||
|
||
<MemberInfo kind="method" type={`(key: string) => Promise<T | undefined>`} /> | ||
|
||
Gets an item from the cache, or returns undefined if the key is not found, or the | ||
item has expired. | ||
### set | ||
|
||
<MemberInfo kind="method" type={`(key: string, value: T, options?: SetCacheKeyOptions) => Promise<void>`} /> | ||
|
||
Sets a key-value pair in the cache. The value must be serializable, so cannot contain | ||
things like functions, circular data structures, class instances etc. | ||
|
||
Optionally a "time to live" (ttl) can be specified, which means that the key will | ||
be considered stale after that many milliseconds. | ||
### delete | ||
|
||
<MemberInfo kind="method" type={`(key: string) => Promise<void>`} /> | ||
|
||
Deletes an item from the cache. | ||
### invalidateTags | ||
|
||
<MemberInfo kind="method" type={`(tags: string[]) => Promise<void>`} /> | ||
|
||
Deletes all items from the cache which contain at least one matching tag. | ||
|
||
|
||
</div> |
58 changes: 58 additions & 0 deletions
58
docs/docs/reference/typescript-api/cache/request-context-cache-service.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
--- | ||
title: "RequestContextCacheService" | ||
isDefaultIndex: false | ||
generated: true | ||
--- | ||
<!-- This file was generated from the Vendure source. Do not modify. Instead, re-run the "docs:build" script --> | ||
import MemberInfo from '@site/src/components/MemberInfo'; | ||
import GenerationInfo from '@site/src/components/GenerationInfo'; | ||
import MemberDescription from '@site/src/components/MemberDescription'; | ||
|
||
|
||
## RequestContextCacheService | ||
|
||
<GenerationInfo sourceFile="packages/core/src/cache/request-context-cache.service.ts" sourceLine="15" packageName="@vendure/core" /> | ||
|
||
This service is used to cache arbitrary data relative to an ongoing request. | ||
It does this by using a WeakMap bound to the current RequestContext, so the cached | ||
data is available for the duration of the request. Once the request completes, the | ||
cached data will be automatically garbage-collected. | ||
|
||
This is useful for caching data which is expensive to compute and which is needed | ||
multiple times during the handling of a single request. | ||
|
||
```ts title="Signature" | ||
class RequestContextCacheService { | ||
set(ctx: RequestContext, key: any, val: T) => void; | ||
get(ctx: RequestContext, key: any) => T | undefined; | ||
get(ctx: RequestContext, key: any, getDefault?: () => T) => T; | ||
get(ctx: RequestContext, key: any, getDefault?: () => T) => T | Promise<T> | undefined; | ||
} | ||
``` | ||
|
||
<div className="members-wrapper"> | ||
|
||
### set | ||
|
||
<MemberInfo kind="method" type={`(ctx: <a href='/reference/typescript-api/request/request-context#requestcontext'>RequestContext</a>, key: any, val: T) => void`} /> | ||
|
||
Set a value in the RequestContext cache. | ||
### get | ||
|
||
<MemberInfo kind="method" type={`(ctx: <a href='/reference/typescript-api/request/request-context#requestcontext'>RequestContext</a>, key: any) => T | undefined`} /> | ||
|
||
Get a value from the RequestContext cache. If the value is not found, the `getDefault` | ||
function will be called to get the value, which will then be cached and returned. | ||
### get | ||
|
||
<MemberInfo kind="method" type={`(ctx: <a href='/reference/typescript-api/request/request-context#requestcontext'>RequestContext</a>, key: any, getDefault?: () => T) => T`} /> | ||
|
||
|
||
### get | ||
|
||
<MemberInfo kind="method" type={`(ctx: <a href='/reference/typescript-api/request/request-context#requestcontext'>RequestContext</a>, key: any, getDefault?: () => T) => T | Promise<T> | undefined`} /> | ||
|
||
|
||
|
||
|
||
</div> |