A guide to creating a custom seed key provider for Umbraco
Umbraco uses a lazy loaded cache, which means that content is loaded into the cache on an as-needed basis. However, you may need specific content to always be in the cache. To achieve this you can implement your own custom seed key providers.
There are two types of seed key providers: IDocumentSeedKeyProvider
for documents and IMediaSeedKeyProvider
for media. As these interfaces are identical only IDocumentSeedKeyProvider
is demonstrated in this article.
Seed keys are cached and calculated once. Any documents created after the site has started will not be included in the seed keys until after a server restart.
This example implements a IDocumentSeedKeyProvider
which seeds all the children of a node, in this case blog posts.
Create a new class called BlogSeedKeyProvider
that implements IDocumentSeedKeyProvider
.
Next we'll inject the IDocumentNavigationQueryService
in order to get the children of the blog node.
Parse a hardcoded string to a GUID.
Use the IDocumentNavigationQueryService
to get the children of the blog node.
Return their keys as a HashSet
.
Since this returns it as a set, and all the sets get unioned, we do not have to worry about duplicates.
The final class looks like this:
Now that the BlogSeedKeyProvider
is implemented, it must be registered in the Startup
class.
All blogpost will now be seeded into the cache on startup, and will always be present in the cache.