Table of Contents

CacheableFactory Class

Definition

Namespace
Cuemon.AspNetCore.Mvc
Assemblies
Cuemon.AspNetCore.Mvc.dll
Source
src/Cuemon.AspNetCore.Mvc/CacheableFactory.cs

Provides access to factory methods for creating and configuring objects implementing the ICacheableObjectResult interface.

public static class CacheableFactory
Inheritance
object
CacheableFactory

Examples

The following example demonstrates how to create cacheable response objects using CacheableFactory.

using System;
using System.Security.Cryptography;
using System.Text;
using Cuemon.AspNetCore.Mvc;

namespace MyApp.Examples;

public static class CacheableFactoryExample
{
    public static void Demonstrate()
    {
        var content = "hello-world";

        var lastModified = CacheableFactory.CreateHttpLastModified(content, o =>
        {
            o.TimestampProvider = _ => DateTime.UtcNow;
            o.ChangedTimestampProvider = _ => DateTime.UtcNow;
        });

        var entityTag = CacheableFactory.CreateHttpEntityTag(content, o =>
        {
            o.ChecksumProvider = value => SHA256.HashData(Encoding.UTF8.GetBytes(value));
            o.WeakChecksumProvider = _ => false;
        });

        var combined = CacheableFactory.Create(content, o =>
        {
            o.TimestampProvider = _ => DateTime.UtcNow;
            o.ChecksumProvider = value => SHA256.HashData(Encoding.UTF8.GetBytes(value));
            o.ChangedTimestampProvider = _ => DateTime.UtcNow;
            o.WeakChecksumProvider = _ => false;
        });

        Console.WriteLine(lastModified is ICacheableObjectResult);
        Console.WriteLine(entityTag is ICacheableObjectResult);
        Console.WriteLine(combined is ICacheableObjectResult);
    }
}

Methods

Name Description
CreateHttpEntityTag<T>(T, Action<ContentBasedObjectResultOptions<T>>)

Encapsulates the specified instance within an integrity based object that is processed by an HTTP ETag filter implementation.

CreateHttpLastModified<T>(T, Action<TimeBasedObjectResultOptions<T>>)

Encapsulates the specified instance within a timestamp based object that is processed by a Last-Modified filter implementation.

Create<T>(T, Action<CacheableObjectResultOptions<T>>)

Encapsulates the specified instance within a timestamp and integrity based object that is processed by both HTTP Last-Modified and HTTP ETag filters implementation.