aboutsummaryrefslogtreecommitdiff
path: root/lib/VNLib.Plugins.Extensions.VNCache/src/DataModel/EntityResultCache.cs
blob: fb75fc632c23af022cfb743db0de8bb6ccb7739a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright (c) 2024 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Plugins.Extensions.VNCache
* File: EntityResultCache.cs 
*
* EntityResultCache.cs is part of VNLib.Plugins.Extensions.VNCache 
* which is part of the larger VNLib collection of libraries and utilities.
*
* VNLib.Plugins.Extensions.VNCache is free software: you can redistribute it and/or modify 
* it under the terms of the GNU Affero General Public License as 
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* VNLib.Plugins.Extensions.VNCache is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see https://www.gnu.org/licenses/.
*/

using System;
using System.Threading;
using System.Threading.Tasks;

namespace VNLib.Plugins.Extensions.VNCache.DataModel
{
    /// <summary>
    /// Represents a cache that can store entities by their unique key
    /// using a user-provided backing store and custom request state.
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="cache">The cache backing store</param>
    /// <param name="taskPolicy">Specifies how background cache tasks are handled</param>
    /// <param name="expirationPolicy">The result expiration policy</param>
    public class EntityResultCache<TEntity>(
        IEntityCache<TEntity> cache, 
        ICacheTaskPolicy taskPolicy, 
        ICacheExpirationPolicy<TEntity> expirationPolicy
    )
        where TEntity : class
    {

        /// <summary>
        /// Fetchs a result by it's request entity
        /// </summary>
        /// <param name="request">The fetch request state object</param>
        /// <param name="cancellation">A token to canel the operation</param>
        /// <param name="resultFactory">A callback generator function</param>
        /// <returns>A task the returns the result of the requested entity, or null if it was not found or provided by the backing store</returns>
        public async Task<TEntity?> FetchAsync<TRequest>(
            TRequest request, 
            Func<TRequest, CancellationToken, Task<TEntity?>> resultFactory, 
            CancellationToken cancellation = default
        ) where TRequest : IEntityCacheKey
        {
            ArgumentNullException.ThrowIfNull(request);
            ArgumentNullException.ThrowIfNull(resultFactory);
            cancellation.ThrowIfCancellationRequested();

            string key = request.GetKey();

            //try to fetch from cache
            TEntity? entity = await cache.GetAsync(key, cancellation);

            if (entity is not null)
            {
                //Check if the entity is expired
                if (expirationPolicy.IsExpired(entity))
                {
                    //Setting to null will force a cache miss
                    entity = null;
                }
            }

            if (entity is null)
            {
                //Cache miss, load from factory
                entity = await resultFactory(request, cancellation);

                if (entity is not null)
                {
                    //Notify the expiration policy that the entity was refreshed before writing back to cache
                    expirationPolicy.OnRefreshed(entity);

                    //Fresh entity was fetched from the factory so write to cache
                    Task upsert = cache.UpsertAsync(key, entity, cancellation);

                    //Allow task policy to determine how completions are observed
                    await taskPolicy.ObserveOperationAsync(upsert);
                }
            }

            return entity;
        }

        /// <summary>
        /// Removes an entity from the cache by it's request entity
        /// </summary>
        /// <param name="request">The request entity to retrieve the entity key from</param>
        /// <param name="cancellation">A token to cancel the async operation</param>
        /// <returns>A task that completes when the key is removed, based on the task policy</returns>
        public Task RemoveAsync<TRequest>(TRequest request, CancellationToken cancellation = default) 
            where TRequest : IEntityCacheKey
        {
            ArgumentNullException.ThrowIfNull(request);

            string key = request.GetKey();

            return cache.RemoveAsync(key, cancellation);
        }

        /// <summary>
        /// Removes an entity from the cache by it's request entity
        /// </summary>
        /// <param name="key">The entities unique key</param>
        /// <param name="cancellation">A token to cancel the async operation</param>
        /// <returns>A task that completes when the key is removed, based on the task policy</returns>
        public Task RemoveAsync(string key, CancellationToken cancellation = default)
        {
            ArgumentException.ThrowIfNullOrWhiteSpace(key);

            Task remove = cache.RemoveAsync(key, cancellation);

            return taskPolicy.ObserveOperationAsync(remove);
        }
    }
}