1- using System . Buffers ;
2- using Microsoft . Extensions . Caching . Distributed ;
1+ using Microsoft . Extensions . Caching . Distributed ;
32using Microsoft . Extensions . Caching . StackExchangeRedis ;
43using Microsoft . Extensions . Logging ;
54using Microsoft . Extensions . Logging . Abstractions ;
65using Microsoft . Extensions . Options ;
76using StackExchange . Redis ;
7+ using System . Buffers ;
88
99namespace MessagingRedisCache ;
1010
1111/// <summary>
1212/// A Redis distributed cache implementation that uses pub/sub.
1313/// </summary>
1414public class MessagingRedisCache :
15- RedisCache ,
16- IBufferDistributedCache
15+ IBufferDistributedCache ,
16+ IDisposable
1717{
1818 /// <summary>
1919 /// Initializes a new instance of MessagingRedisCache.
2020 /// </summary>
2121 public MessagingRedisCache (
22+ IBufferDistributedCache bufferDistributedCache ,
2223 IMessagePublisher messagePublisher ,
2324 IMessageSubscriber messageSubscriber ,
2425 IMessagingConfigurationVerifier messagingConfigurationVerifier ,
2526 IOptions < MessagingRedisCacheOptions > messagingRedisCacheOptionsAccessor ,
26- ILogger < MessagingRedisCache > ? logger = null ) :
27- base ( messagingRedisCacheOptionsAccessor )
27+ ILogger < MessagingRedisCache > ? logger = null )
2828 {
29+ BufferDistributedCache = bufferDistributedCache ??
30+ throw new ArgumentNullException (
31+ nameof ( bufferDistributedCache ) ) ;
2932 Logger = logger ??
3033 NullLogger < MessagingRedisCache > . Instance ;
3134 MessagePublisher = messagePublisher ??
@@ -56,6 +59,11 @@ public MessagingRedisCache(
5659 SubscribeCancellationTokenSource . Token ) ;
5760 }
5861
62+ /// <summary>
63+ /// The backing <c>IBufferDistributedCache</c>, implemented by <see cref="RedisCache"/>.
64+ /// </summary>
65+ public IBufferDistributedCache BufferDistributedCache { get ; }
66+
5967 /// <summary>
6068 /// The <c>StackExchange.Redis.IDatabase</c> for the <see cref="RedisCache"/>.
6169 /// </summary>
@@ -90,29 +98,69 @@ public MessagingRedisCache(
9098 private CancellationTokenSource SubscribeCancellationTokenSource { get ; set ; }
9199
92100 /// <inheritdoc />
93- public new void Dispose ( )
101+ public void Dispose ( )
94102 {
95- base . Dispose ( ) ;
96103 Dispose ( true ) ;
104+ GC . SuppressFinalize ( this ) ;
105+ }
106+
107+ /// <inheritdoc />
108+ public byte [ ] ? Get ( string key )
109+ {
110+ return BufferDistributedCache
111+ . Get ( key ) ;
112+ }
113+
114+ /// <inheritdoc />
115+ public async Task < byte [ ] ? > GetAsync (
116+ string key ,
117+ CancellationToken token = default )
118+ {
119+ return await BufferDistributedCache
120+ . GetAsync (
121+ key ,
122+ token )
123+ . ConfigureAwait ( false ) ;
97124 }
98125
99126 /// <inheritdoc />
100- public new void Remove ( string key )
127+ public void Refresh ( string key )
101128 {
102- base . Remove ( key ) ;
129+ BufferDistributedCache
130+ . Refresh ( key ) ;
131+ }
132+
133+ /// <inheritdoc />
134+ public async Task RefreshAsync (
135+ string key ,
136+ CancellationToken token = default )
137+ {
138+ await BufferDistributedCache
139+ . RefreshAsync (
140+ key ,
141+ token )
142+ . ConfigureAwait ( false ) ;
143+ }
144+
145+ /// <inheritdoc />
146+ public void Remove ( string key )
147+ {
148+ BufferDistributedCache
149+ . Remove ( key ) ;
103150 MessagePublisher . Publish (
104151 Database . Value . Multiplexer ,
105152 key ) ;
106153 }
107154
108155 /// <inheritdoc />
109- public new async Task RemoveAsync (
156+ public async Task RemoveAsync (
110157 string key ,
111158 CancellationToken token = default )
112159 {
113- await base . RemoveAsync (
114- key ,
115- token )
160+ await BufferDistributedCache
161+ . RemoveAsync (
162+ key ,
163+ token )
116164 . ConfigureAwait ( false ) ;
117165 await MessagePublisher
118166 . PublishAsync (
@@ -123,12 +171,12 @@ await MessagePublisher
123171 }
124172
125173 /// <inheritdoc />
126- public new void Set (
174+ public void Set (
127175 string key ,
128176 byte [ ] value ,
129177 DistributedCacheEntryOptions options )
130178 {
131- base . Set (
179+ BufferDistributedCache . Set (
132180 key ,
133181 value ,
134182 options ) ;
@@ -143,24 +191,28 @@ public void Set(
143191 ReadOnlySequence < byte > value ,
144192 DistributedCacheEntryOptions options )
145193 {
146- ( ( IBufferDistributedCache ) this ) . Set ( key , value , options ) ;
194+ BufferDistributedCache . Set (
195+ key ,
196+ value ,
197+ options ) ;
147198 MessagePublisher . Publish (
148199 Database . Value . Multiplexer ,
149200 key ) ;
150201 }
151202
152203 /// <inheritdoc />
153- public new async Task SetAsync (
204+ public async Task SetAsync (
154205 string key ,
155206 byte [ ] value ,
156207 DistributedCacheEntryOptions options ,
157208 CancellationToken token = default )
158209 {
159- await base . SetAsync (
160- key ,
161- value ,
162- options ,
163- token )
210+ await BufferDistributedCache
211+ . SetAsync (
212+ key ,
213+ value ,
214+ options ,
215+ token )
164216 . ConfigureAwait ( false ) ;
165217 await MessagePublisher
166218 . PublishAsync (
@@ -170,7 +222,51 @@ await MessagePublisher
170222 . ConfigureAwait ( false ) ;
171223 }
172224
173- private void Dispose ( bool isDisposing )
225+ public async ValueTask SetAsync (
226+ string key ,
227+ ReadOnlySequence < byte > value ,
228+ DistributedCacheEntryOptions options ,
229+ CancellationToken token = default )
230+ {
231+ await BufferDistributedCache
232+ . SetAsync (
233+ key ,
234+ value ,
235+ options ,
236+ token )
237+ . ConfigureAwait ( false ) ;
238+ await MessagePublisher
239+ . PublishAsync (
240+ Database . Value . Multiplexer ,
241+ key ,
242+ token )
243+ . ConfigureAwait ( false ) ;
244+ }
245+
246+ public bool TryGet (
247+ string key ,
248+ IBufferWriter < byte > destination )
249+ {
250+ return BufferDistributedCache
251+ . TryGet (
252+ key ,
253+ destination ) ;
254+ }
255+
256+ public async ValueTask < bool > TryGetAsync (
257+ string key ,
258+ IBufferWriter < byte > destination ,
259+ CancellationToken token = default )
260+ {
261+ return await BufferDistributedCache
262+ . TryGetAsync (
263+ key ,
264+ destination ,
265+ token )
266+ . ConfigureAwait ( false ) ;
267+ }
268+
269+ protected virtual void Dispose ( bool isDisposing )
174270 {
175271 if ( IsDisposed )
176272 {
0 commit comments