Caching strategy
Keys
For all services to have a common convention for the cache keys, it is important to set a standard.
A market-standard approach is to use the following format:
<Entity>:<Property>:<Value>
Both
<Entity>and<Property>path parts should be in Pascal case. WhyPascalCase, you ask? It is useful for creating keys dynamically using thenameofexpression.
Keys - Example
-
"User:Id:42"Look for
UserwithIdequal to42. -
"FreightUnit:OwnerId:6"Look for
FreightUnitwithOwnerIdequal to6.
DTOs
If you have cached a DTO instead of an entity, simply add another path part for that DTO's name.
E.g. Asset:LicensePlateNumber:XXX-1234:AssetForCrossingViewDto.
The above means "Look for
AssetwithLicensePlateNumberequal to"XXX-1234"asAssetForCrossingViewDto".
Tenancy
For entities that are specific to a certain tenant, it's a huge security issue to get values straight from the cache as it skips the whole filtering implemented in EF Core.
The common practice for this is to set the tenant's identifier as a prefix to the key itself.
The format is the following:
Company:<Company.Id>:<Entity>:<Property>:<Value>
E.g. "Company:6:Asset:LicensePlateNumber:XXX-1234"
The above means "Inside of company (tenant) with
Idequal to6, look forAssetwithLicensePlateNumberequal to"XXX-1234"".
Summary
Currently, the full format is the following:
[Company:<Company.Id>:]<Entity>:<Property>:<Value>[:<DtoName>]