Services - Cache
CrudServices can store single results in a cache.
By default, a basic in-memory (RAM) cache is used.
Configuration
You can provide your own distributed cache, as long as it implements the CrudCache
interface.
export interface CrudCache {
get: (key: string) => Promise<any>;
set: (key: string, value: any, ttl: number) => Promise<any>;
}
You can provide it globally through the CrudConfigService
.
let myCache: CrudCache = ...
@Injectable()
export class MyConfigService extends CrudConfigService {
constructor(/* ... */) {
super({ cacheManager: myCache , /* ... */})
}
//..
}
Or you can provide it to a specific service.
let myCache: CrudCache = ...
@Injectable()
export class UserService extends CrudUserService<User> {
constructor(/* ... */) {
super( /* ... */, { cacheManager: myCache })
}
//..
}
Usage
CrudUserService
has several methods that access the cache.
$findOneCached
$findOneCached
tries to retrieve the result from the cache using id_field
+ CrudOptions?
as a key.
If no result is found, $findOne
is called and the result is stored in cache.
$setCached
$setCached
inserts a given entity into the CrudCache
.
Note
When $findOneCached
is called through the client, found results are not stored in the cache to prevent users from maliciously filling it. You can disable that behavior with allowClientCacheFilling
.
Authentication
During authentication, the user is fetched with CrudUserService
->$findOneCached
.
Note
The CrudCache
of CrudUserService
has a great impact on your application performance since users are fetched in every authenticated request.
Options
import { CacheOptions } from '@eicrud/core/config';
const cacheOptions = new CacheOptions();
@Injectable()
export class ProfileService extends CrudService<Profile> {
constructor(/* ... */) {
super( /* ... */, { cacheOptions })
}
//..
}