Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: sofietoft <stk@umbraco.com>
  • Loading branch information
nikolajlauridsen and sofietoft authored Oct 28, 2024
1 parent ab35a91 commit 6d45f44
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
20 changes: 10 additions & 10 deletions 15/umbraco-cms/extending/creating-custom-seed-key-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ description: A guide to creating a custom seed key provider for Umbraco

# Creating a Custom Seed Key Provider

From version 15 and onwards Umbraco uses a lazy loaded cache, this means content is loaded into the cache on an as-needed basis.
However, you may some specific content to always be in the cache, to achieve this you can implement your own custom seed key providers.
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 is two types of seed key providers: `IDocumentSeedKeyProvider` for documents and `IMediaSeedKeyProvider` for media,
these interfaces are identical so only `IDocumentSeedKeyProvider` is demonstrated here.
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.

{% hint style="warning" %}
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.
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.
{% endhint %}

## Implementation

This example implements a `IDocumentSeedKeyProvider` which seeds all the children of a node, in this case blog posts.

First we'll create a class called `BlogSeedKeyProvider` that implements `IDocumentSeedKeyProvider`.
1. Create a new class called `BlogSeedKeyProvider` that implements `IDocumentSeedKeyProvider`.

```csharp
using Umbraco.Cms.Infrastructure.HybridCache;
Expand Down Expand Up @@ -51,7 +49,9 @@ public class BlogSeedKeyProvider : IDocumentSeedKeyProvider
{...}
```

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`.
3. Parse a hardcoded string to a GUID.
4. Use the `IDocumentNavigationQueryService` to get the children of the blog node.
5. Return their keys as a `HashSet`.

```csharp
public ISet<Guid> GetSeedKeys()
Expand All @@ -66,7 +66,7 @@ public ISet<Guid> GetSeedKeys()
return new HashSet<Guid>();
}
```
We since we're returning it as a set, and all the sets gets unioned, we don't have to worry about duplicates.
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:

Expand Down Expand Up @@ -99,7 +99,7 @@ public class BlogSeedKeyProvider : IDocumentSeedKeyProvider

### Registering the Seed Key Provider

Now that we have implemented the `BlogSeedKeyProvider` we need to register it in the `Startup` class.
Now that the `BlogSeedKeyProvider` is implemented, it must be registered in the `Startup` class.

```csharp
using MySite.SeedKeyProviders;
Expand All @@ -112,4 +112,4 @@ builder.Services.AddSingleton<IDocumentSeedKeyProvider, BlogSeedKeyProvider>();
{...}
```

Now all our blogpost will be seeded into the cache on startup, and will always be present in the cache.
All blogpost will now be seeded into the cache on startup, and will always be present in the cache.
5 changes: 2 additions & 3 deletions 15/umbraco-cms/reference/cache/cache-seeding.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ description: Information about cache seeding

# Cache Seeding

From version 15 and onwards Umbraco uses a lazy loaded cache, this means content is loaded into the cache on an as-needed basis
that is whenever a piece of content is shown on the website for the first time it first needs to be loaded into the cache.
Umbraco uses a lazy loaded cache, meaning content is loaded into the cache on an as-needed basis. Whenever a piece of content is shown on the website for the first time it first needs to be loaded into the cache.

Loading the content into the cache causes a delay. This delay is dependent on the latency between your server and your database, but is generally minimal.
However, for certain pages, for instance the front page, you may not want this delay to be there, the role of cache seeding is to solve this issue.
For certain pages, like the front page, you may not want this delay to be there. The role of cache seeding is meant to solve this issue.

## How it works

Expand Down

0 comments on commit 6d45f44

Please sign in to comment.