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.
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.
$setCached
$setCached inserts a given entity into the CrudCache.
$deleteCached
$deleteCached removes a given entity into the CrudCache.
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 })
    }
    //..
}
Use a different cache key
Each CrudService takes an optional cacheField config parameter.
  constructor(protected moduleRef: ModuleRef) {
    const serviceName = CrudService.getName(Token);
    super(moduleRef, Token, getSecurity(serviceName), { hooks, cacheField: 'token' });
  }
The new field replaces id_field to identify your cached entities ($findOneCached, $setCached...)