|
| 1 | +--- |
| 2 | +description: A guid to creating a custom seed key provider for Umbraco |
| 3 | +--- |
| 4 | + |
| 5 | +# Creating a Custom Seed Key Provider |
| 6 | + |
| 7 | +From version 15 and onwards Umbraco uses a lazy loaded cache, this means content is loaded into the cache on an as-needed basis. |
| 8 | +However, you may some specific content to always be in the cache, to achieve this you can implement your own custom seed key providers. |
| 9 | + |
| 10 | +There is two types of seed key providers: `IDocumentSeedKeyProvider` for documents and `IMediaSeedKeyProvider` for media, |
| 11 | +these interfaces are identical so only `IDocumentSeedKeyProvider` is demonstrated here. |
| 12 | + |
| 13 | +{% hint style="warning" %} |
| 14 | +Seed keys are themselves cached and only calculated once, this means that any documents created after the site has started won't be included in the seed keys untill ther server has restarted. |
| 15 | +{% endhint %} |
| 16 | + |
| 17 | +## Implementation |
| 18 | + |
| 19 | +This example implements a `IDocumentSeedKeyProvider` which seeds all the children of a node, in this case blog posts. |
| 20 | + |
| 21 | +First we'll create a class called `BlogSeedKeyProvider` that implements `IDocumentSeedKeyProvider`. |
| 22 | + |
| 23 | +```csharp |
| 24 | +using Umbraco.Cms.Infrastructure.HybridCache; |
| 25 | + |
| 26 | +namespace MySite.SeedKeyProviders; |
| 27 | + |
| 28 | +public class BlogSeedKeyProvider : IDocumentSeedKeyProvider |
| 29 | +{ |
| 30 | + public ISet<Guid> GetSeedKeys() |
| 31 | + { |
| 32 | + } |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +Next we'll inject the `IDocumentNavigationQueryService` in order to get the children of the blog node. |
| 37 | + |
| 38 | +```csharp |
| 39 | +using Umbraco.Cms.Core.Services.Navigation; |
| 40 | +using Umbraco.Cms.Infrastructure.HybridCache; |
| 41 | + |
| 42 | +namespace MySite.SeedKeyProviders; |
| 43 | + |
| 44 | +public class BlogSeedKeyProvider : IDocumentSeedKeyProvider |
| 45 | +{ |
| 46 | + private readonly IDocumentNavigationQueryService _documentNavigationQueryService; |
| 47 | + |
| 48 | + public BlogSeedKeyProvider(IDocumentNavigationQueryService documentNavigationQueryService) |
| 49 | + => _documentNavigationQueryService = documentNavigationQueryService; |
| 50 | + |
| 51 | +{...} |
| 52 | +``` |
| 53 | + |
| 54 | +Now we can parse a hardcoded string to a guid and use the `IDocumentNavigationQueryService` to get the children of the blog node and return their keys as a `HashSet`. |
| 55 | + |
| 56 | +```csharp |
| 57 | +public ISet<Guid> GetSeedKeys() |
| 58 | +{ |
| 59 | + var blogRoot = Guid.Parse("a5fdb22d-b7f2-4a59-8c4e-46ed86bde56c"); |
| 60 | + |
| 61 | + if (_documentNavigationQueryService.TryGetChildrenKeys(blogRoot, out IEnumerable<Guid> blogPostKeys)) |
| 62 | + { |
| 63 | + return new HashSet<Guid>(blogPostKeys); |
| 64 | + } |
| 65 | + |
| 66 | + return new HashSet<Guid>(); |
| 67 | +} |
| 68 | +``` |
| 69 | +We since we're returning it as a set, and all the sets gets unioned, we don't have to worry about duplicates. |
| 70 | + |
| 71 | +The final class looks like this: |
| 72 | + |
| 73 | +```csharp |
| 74 | +using Umbraco.Cms.Core.Services.Navigation; |
| 75 | +using Umbraco.Cms.Infrastructure.HybridCache; |
| 76 | + |
| 77 | +namespace MySite.SeedKeyProviders; |
| 78 | + |
| 79 | +public class BlogSeedKeyProvider : IDocumentSeedKeyProvider |
| 80 | +{ |
| 81 | + private readonly IDocumentNavigationQueryService _documentNavigationQueryService; |
| 82 | + |
| 83 | + public BlogSeedKeyProvider(IDocumentNavigationQueryService documentNavigationQueryService) |
| 84 | + => _documentNavigationQueryService = documentNavigationQueryService; |
| 85 | + |
| 86 | + public ISet<Guid> GetSeedKeys() |
| 87 | + { |
| 88 | + var blogRoot = Guid.Parse("a5fdb22d-b7f2-4a59-8c4e-46ed86bde56c"); |
| 89 | + |
| 90 | + if (_documentNavigationQueryService.TryGetChildrenKeys(blogRoot, out IEnumerable<Guid> blogPostKeys)) |
| 91 | + { |
| 92 | + return new HashSet<Guid>(blogPostKeys); |
| 93 | + } |
| 94 | + |
| 95 | + return new HashSet<Guid>(); |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +### Registering the Seed Key Provider |
| 101 | + |
| 102 | +Now that we have implemented the `BlogSeedKeyProvider` we need to register it in the `Startup` class. |
| 103 | + |
| 104 | +```csharp |
| 105 | +using MySite.SeedKeyProviders; |
| 106 | +using Umbraco.Cms.Infrastructure.DependencyInjection; |
| 107 | +using Umbraco.Cms.Infrastructure.HybridCache; |
| 108 | +using Umbraco.Cms.Web.UI; |
| 109 | + |
| 110 | +WebApplicationBuilder builder = WebApplication.CreateBuilder(args); |
| 111 | + |
| 112 | +builder.Services.AddSingleton<IDocumentSeedKeyProvider, BlogSeedKeyProvider>(); |
| 113 | +{...} |
| 114 | +``` |
| 115 | + |
| 116 | +Now all our blogpost will be seeded into the cache on startup, and will always be present in the cache. |
0 commit comments