Working with caching

Information on how to insert and delete from the runtime cache

This article will show you how to insert and delete from the runtime cache.

Scenario

For this example we're working with tags. On my site I have two tag properties:

  1. One on every page using the tag group default

  2. One on my blog posts using the tag group blog

We're going to expose an endpoint that allows us to get the tags from each group.

The tags from the default should be cached for a minute. The blog tags will be cached until site restart or if you publish a blog post node in the Backoffice.

Example

Why work with tags? Because they're not cached by default.. which makes them ideal for demo purposes :)

TagService

First we want to create our CacheTagService. In this example it's a basic class with one method (GetAll) that wraps Umbraco's TagQuery.GetAllTags().

using System;
using System.Collections.Generic;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Extensions;

namespace Doccers.Core.Services.Implement;

public class CacheTagService : ICacheTagService
{
    private readonly ITagQuery _tagQuery;
    private readonly IAppPolicyCache _runtimeCache;

    public CacheTagService(ITagQuery tagQuery, AppCaches appCaches)
    {
        _tagQuery = tagQuery;
        // Get the RuntimeCache from appCaches
        // and assign to our private field.
        _runtimeCache = appCaches.RuntimeCache;
    }

    public IEnumerable<TagModel> GetAll(
        string group,
        string cacheKey,
        TimeSpan? timeout = null)
    {
        // GetCacheItem will automatically insert the object
        // into cache if it doesn't exist.
        return _runtimeCache.GetCacheItem(cacheKey, () =>
        {
            return _tagQuery.GetAllTags(group);
        }, timeout);
    }
}

As you can see we inherit from the ICacheTagService interface. All that has is:

The interface was created to better register it so we can use dependency injection. You can register your own classes like so:

Now you can inject ICacheTagService in any constructor in your project - wohooo!

API

Now that we have our service it's time to create an endpoint where we can fetch the (cached) tags.

/umbraco/api/tags/getblogtags:

Result

/umbraco/api/tags/getdefaulttags:

Result

Everything should now work as expected when it comes to getting tags. However, if I go to my Backoffice and add a new tag to the blog group the changes aren't shown on the endpoint. Let's fix that.

Clearing cache on publish

To clear the cache we need a notification handler in which we register to the ContentPublishedNotification event on the ContentService. This allows us to run a piece of code whenever you publish a node.

Now that we have our notification we also need to register it. Add builder.AddNotificationHandler<ContentPublishedNotification, Notification>(); to the Compose method in the Composer class so it becomes:

Awesome! Now we have set up caching on our tags - making the site a bit faster. :)

Last updated