aboutsummaryrefslogtreecommitdiff
path: root/lib/VNLib.Plugins.Extensions.VNCache/src/DataModel/EntityCacheExtensions.cs
blob: 73baa4a99362d5897ffc03b6d7ad9aa2834ce732 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Copyright (c) 2023 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Plugins.Extensions.VNCache
* File: EntityCacheExtensions.cs 
*
* EntityCacheExtensions.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;
using System.Runtime.CompilerServices;

using VNLib.Data.Caching;

namespace VNLib.Plugins.Extensions.VNCache.DataModel
{
    /// <summary>
    /// Provides cache extensions for entity caching
    /// </summary>
    public static class EntityCacheExtensions
    {
        /// <summary>
        /// Gets a new <see cref="ScopedCache"/> that is backed by the current cache provider
        /// and generates 1:1 keys from the <paramref name="cacheKeyGenerator"/>
        /// </summary>
        /// <param name="cache"></param>
        /// <param name="cacheKeyGenerator">The instance that generates unique keys for a given entity id</param>
        /// <returns>The new <see cref="ScopedCache"/> instance</returns>
        public static ScopedCache GetScopedCache(this IGlobalCacheProvider cache, ICacheKeyGenerator cacheKeyGenerator) => new ScopedCacheImpl(cache, cacheKeyGenerator);

        /// <summary>
        /// Deletes an <see cref="ICacheEntity"/> from the cache from its id
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cache"></param>
        /// <param name="entity">The entity to delete from the store</param>
        /// <param name="cancellation">A token to cancel the operation</param>
        /// <returns>A task that completes when the delete operation has compelted</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static Task DeleteAsync<T>(this IGlobalCacheProvider cache, T entity, CancellationToken cancellation) where T: class, ICacheEntity
        {
            _ = entity ?? throw new ArgumentNullException(nameof(entity));
            _ = cache ?? throw new ArgumentNullException(nameof(entity));
            //Delete by its id
            return cache.DeleteAsync(entity.Id, cancellation);
        }

        /// <summary>
        /// Asynchronously sets (or updates) a cached value in the backing cache store
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cache"></param>
        /// <param name="cancellation">A token to cancel the async operation</param>
        /// <param name="entity">The entity to set at the given key</param>
        /// <returns>A task that completes when the add/update operation has compelted</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static Task AddOrUpdateAsync<T>(this IGlobalCacheProvider cache, T entity, CancellationToken cancellation) where T: class, ICacheEntity
        {
            _ = entity ?? throw new ArgumentNullException(nameof(entity));
            _ = cache ?? throw new ArgumentNullException(nameof(cache));

            //Add/update with its id
            return cache.AddOrUpdateAsync(entity.Id, null, entity, cancellation);
        }
     

        private sealed class ScopedCacheImpl: ScopedCache
        {
            private readonly IGlobalCacheProvider cache;

            public override bool IsConnected
            {
                [MethodImpl(MethodImplOptions.AggressiveInlining)]
                get => cache.IsConnected;
            }


            protected override ICacheKeyGenerator KeyGen { get; }

            public ScopedCacheImpl(IGlobalCacheProvider cache, ICacheKeyGenerator keyGen)
            {
                this.cache = cache;
                KeyGen = keyGen;
            }

            ///<inheritdoc/>
            public override Task AddOrUpdateAsync<T>(string key, string? newKey, T value, CancellationToken cancellation)
            {
                _ = key ?? throw new ArgumentNullException(nameof(key));

                //Compute primary key from id
                string primary = KeyGen.ComputedKey(key);

                //If newkey exists, compute the secondary key
                string? secondary = newKey != null ? KeyGen.ComputedKey(newKey) : null;

                return cache.AddOrUpdateAsync(primary, secondary, value, cancellation);
            }

            ///<inheritdoc/>
            public override Task DeleteAsync(string key, CancellationToken cancellation)
            {
                _ = key ?? throw new ArgumentNullException(nameof(key));
                //Compute the key for the id
                string scoped = KeyGen.ComputedKey(key);
                return cache.DeleteAsync(scoped, cancellation);
            }

            ///<inheritdoc/>
            public override Task<T> GetAsync<T>(string key, CancellationToken cancellation)
            {
                _ = key ?? throw new ArgumentNullException(nameof(key));

                //Compute the key for the id
                string scoped = KeyGen.ComputedKey(key);

                return cache.GetAsync<T?>(scoped, cancellation);
            }

            ///<inheritdoc/>
            public override Task<T> GetAsync<T>(string key, ICacheObjectDeserialzer deserializer, CancellationToken cancellation)
            {
                _ = key ?? throw new ArgumentNullException(nameof(key));

                //Compute the key for the id
                string scoped = KeyGen.ComputedKey(key);

                return cache.GetAsync<T?>(scoped, deserializer, cancellation);
            }

            ///<inheritdoc/>
            public override Task AddOrUpdateAsync<T>(string key, string? newKey, T value, ICacheObjectSerialzer serialzer, CancellationToken cancellation)
            {
                _ = key ?? throw new ArgumentNullException(nameof(key));

                //Compute primary key from id
                string primary = KeyGen.ComputedKey(key);

                //If newkey exists, compute the secondary key
                string? secondary = newKey != null ? KeyGen.ComputedKey(newKey) : null;

                return cache.AddOrUpdateAsync(primary, secondary, value, serialzer, cancellation);
            }

            ///<inheritdoc/>
            public override Task GetAsync(string key, IObjectData rawData, CancellationToken cancellation)
            {
                _ = key ?? throw new ArgumentNullException(nameof(key));

                //Compute the key for the id
                string scoped = KeyGen.ComputedKey(key);

                return cache.GetAsync(scoped, rawData, cancellation);
            }

            ///<inheritdoc/>
            public override Task AddOrUpdateAsync(string key, string? newKey, IObjectData rawData, ICacheObjectSerialzer serialzer, CancellationToken cancellation)
            {
                _ = key ?? throw new ArgumentNullException(nameof(key));

                //Compute primary key from id
                string primary = KeyGen.ComputedKey(key);

                //If newkey exists, compute the secondary key
                string? secondary = newKey != null ? KeyGen.ComputedKey(newKey) : null;

                return cache.AddOrUpdateAsync(primary, secondary, rawData, serialzer, cancellation);
            }
        }
    }
   
}