SessionCacheStrategy
This strategy defines how sessions get cached. Since most requests will need the Session object for permissions data, it can become a bottleneck to go to the database and do a multi-join SQL query each time. Therefore, we cache the session data only perform the SQL query once and upon invalidation of the cache.
The Vendure default from v3.1+ is to use a the DefaultSessionCacheStrategy, which delegates to the configured CacheStrategy to store the session data. This should be suitable for most use-cases.
If you are using v3.1 or later, you should not normally need to implement a custom SessionCacheStrategy,
since this is now handled by the DefaultSessionCacheStrategy.
Prior to v3.1, the default was to use the InMemorySessionCacheStrategy, which is fast but suitable for single-instance deployments.
This is configured via the authOptions.sessionCacheStrategy property of
your VendureConfig.
Here's an example implementation using Redis. To use this, you need to add the ioredis package as a dependency.
Example
- Extends:
InjectableStrategy
set
(session: CachedSession) => void | Promise<void>Store the session in the cache. When caching a session, the data should not be modified apart from performing any transforms needed to get it into a state to be stored, e.g. JSON.stringify().
get
(sessionToken: string) => CachedSession | undefined | Promise<CachedSession | undefined>Retrieve the session from the cache
delete
(sessionToken: string) => void | Promise<void>Delete a session from the cache
clear
() => void | Promise<void>Clear the entire cache
A simplified representation of the User associated with the current Session.
id
IDidentifier
stringverified
booleanchannelPermissions
UserChannelPermissions[]A simplified representation of a Session which is easy to store.
cacheExpiry
numberThe timestamp after which this cache entry is considered stale and
a fresh copy of the data will be set. Based on the sessionCacheTTL
option.
id
IDtoken
stringexpires
DateactiveOrderId
IDauthenticationStrategy
stringuser
CachedSessionUseractiveChannelId
ID