Skip to main content

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. Why PascalCase, you ask? It is useful for creating keys dynamically using the nameof expression.

Keys - Example​

  • "User:Id:42"

    Look for User with Id equal to 42.

  • "FreightUnit:OwnerId:6"

    Look for FreightUnit with OwnerId equal to 6.

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 Asset with LicensePlateNumber equal to "XXX-1234" as AssetForCrossingViewDto".

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 Id equal to 6, look for Asset with LicensePlateNumber equal to "XXX-1234"".

Summary​

Currently, the full format is the following:

[Company:<Company.Id>:]<Entity>:<Property>:<Value>[:<DtoName>]