1
+ //
2
+ // https://github.com/ServiceStack/ServiceStack.Redis
3
+ // ServiceStack.Redis: ECMA CLI Binding to the Redis key-value storage system
4
+ //
5
+ // Authors:
6
+ // Demis Bellot ([email protected] )
7
+ //
8
+ // Copyright 2013 Service Stack LLC. All Rights Reserved.
9
+ //
10
+ // Licensed under the same terms of ServiceStack.
11
+ //
12
+
13
+ using ServiceStack . Caching ;
14
+ using ServiceStack . Redis . Internal ;
15
+ using System ;
16
+ using System . Collections . Generic ;
17
+ using System . Runtime . CompilerServices ;
18
+ using System . Threading ;
19
+ using System . Threading . Tasks ;
20
+
21
+ namespace ServiceStack . Redis
22
+ {
23
+ /// <summary>
24
+ /// Provides thread-safe retrieval of redis clients since each client is a new one.
25
+ /// Allows the configuration of different ReadWrite and ReadOnly hosts
26
+ /// </summary>
27
+ public partial class BasicRedisClientManager
28
+ : IRedisClientsManagerAsync , ICacheClientAsync
29
+ {
30
+ private ValueTask < ICacheClientAsync > GetCacheClientAsync ( in CancellationToken _ )
31
+ => new RedisClientManagerCacheClient ( this ) . AsValueTaskResult < ICacheClientAsync > ( ) ;
32
+
33
+ private ValueTask < ICacheClientAsync > GetReadOnlyCacheClientAsync ( in CancellationToken _ )
34
+ => ConfigureRedisClientAsync ( this . GetReadOnlyClientImpl ( ) ) . AsValueTaskResult < ICacheClientAsync > ( ) ;
35
+
36
+ private IRedisClientAsync ConfigureRedisClientAsync ( IRedisClientAsync client )
37
+ => client ;
38
+
39
+ ValueTask < ICacheClientAsync > IRedisClientsManagerAsync . GetCacheClientAsync ( CancellationToken token )
40
+ => GetCacheClientAsync ( token ) ;
41
+
42
+ ValueTask < IRedisClientAsync > IRedisClientsManagerAsync . GetClientAsync ( CancellationToken token )
43
+ => GetClientImpl ( ) . AsValueTaskResult < IRedisClientAsync > ( ) ;
44
+
45
+ ValueTask < ICacheClientAsync > IRedisClientsManagerAsync . GetReadOnlyCacheClientAsync ( CancellationToken token )
46
+ => GetReadOnlyCacheClientAsync ( token ) ;
47
+
48
+ ValueTask < IRedisClientAsync > IRedisClientsManagerAsync . GetReadOnlyClientAsync ( CancellationToken token )
49
+ => GetReadOnlyClientImpl ( ) . AsValueTaskResult < IRedisClientAsync > ( ) ;
50
+
51
+ ValueTask IAsyncDisposable . DisposeAsync ( )
52
+ {
53
+ Dispose ( ) ;
54
+ return default ;
55
+ }
56
+
57
+ async Task < T > ICacheClientAsync . GetAsync < T > ( string key , CancellationToken token )
58
+ {
59
+ await using var client = await GetReadOnlyCacheClientAsync ( token ) . ConfigureAwait ( false ) ;
60
+ return await client . GetAsync < T > ( key ) . ConfigureAwait ( false ) ;
61
+ }
62
+
63
+ async Task < bool > ICacheClientAsync . SetAsync < T > ( string key , T value , CancellationToken token )
64
+ {
65
+ await using var client = await GetCacheClientAsync ( token ) . ConfigureAwait ( false ) ;
66
+ return await client . SetAsync < T > ( key , value , token ) . ConfigureAwait ( false ) ;
67
+ }
68
+
69
+ async Task < bool > ICacheClientAsync . SetAsync < T > ( string key , T value , DateTime expiresAt , CancellationToken token )
70
+ {
71
+ await using var client = await GetCacheClientAsync ( token ) . ConfigureAwait ( false ) ;
72
+ return await client . SetAsync < T > ( key , value , expiresAt , token ) . ConfigureAwait ( false ) ;
73
+ }
74
+
75
+ async Task < bool > ICacheClientAsync . SetAsync < T > ( string key , T value , TimeSpan expiresIn , CancellationToken token )
76
+ {
77
+ await using var client = await GetCacheClientAsync ( token ) . ConfigureAwait ( false ) ;
78
+ return await client . SetAsync < T > ( key , value , expiresIn , token ) . ConfigureAwait ( false ) ;
79
+ }
80
+
81
+ async Task ICacheClientAsync . FlushAllAsync ( CancellationToken token )
82
+ {
83
+ await using var client = await GetCacheClientAsync ( token ) . ConfigureAwait ( false ) ;
84
+ await client . FlushAllAsync ( token ) . ConfigureAwait ( false ) ;
85
+ }
86
+
87
+ async Task < IDictionary < string , T > > ICacheClientAsync . GetAllAsync < T > ( IEnumerable < string > keys , CancellationToken token )
88
+ {
89
+ await using var client = await GetReadOnlyCacheClientAsync ( token ) . ConfigureAwait ( false ) ;
90
+ return await client . GetAllAsync < T > ( keys , token ) . ConfigureAwait ( false ) ;
91
+ }
92
+
93
+ async Task ICacheClientAsync . SetAllAsync < T > ( IDictionary < string , T > values , CancellationToken token )
94
+ {
95
+ await using var client = await GetCacheClientAsync ( token ) . ConfigureAwait ( false ) ;
96
+ await client . SetAllAsync < T > ( values , token ) . ConfigureAwait ( false ) ;
97
+ }
98
+
99
+ async Task < bool > ICacheClientAsync . RemoveAsync ( string key , CancellationToken token )
100
+ {
101
+ await using var client = await GetCacheClientAsync ( token ) . ConfigureAwait ( false ) ;
102
+ return await client . RemoveAsync ( key , token ) . ConfigureAwait ( false ) ;
103
+ }
104
+
105
+ async Task ICacheClientAsync . RemoveAllAsync ( IEnumerable < string > keys , CancellationToken token )
106
+ {
107
+ await using var client = await GetCacheClientAsync ( token ) . ConfigureAwait ( false ) ;
108
+ await client . RemoveAllAsync ( keys , token ) . ConfigureAwait ( false ) ;
109
+ }
110
+
111
+ async Task < long > ICacheClientAsync . IncrementAsync ( string key , uint amount , CancellationToken token )
112
+ {
113
+ await using var client = await GetCacheClientAsync ( token ) . ConfigureAwait ( false ) ;
114
+ return await client . IncrementAsync ( key , amount , token ) . ConfigureAwait ( false ) ;
115
+ }
116
+
117
+ async Task < long > ICacheClientAsync . DecrementAsync ( string key , uint amount , CancellationToken token )
118
+ {
119
+ await using var client = await GetCacheClientAsync ( token ) . ConfigureAwait ( false ) ;
120
+ return await client . DecrementAsync ( key , amount , token ) . ConfigureAwait ( false ) ;
121
+ }
122
+
123
+ async Task < bool > ICacheClientAsync . AddAsync < T > ( string key , T value , CancellationToken token )
124
+ {
125
+ await using var client = await GetCacheClientAsync ( token ) . ConfigureAwait ( false ) ;
126
+ return await client . AddAsync < T > ( key , value , token ) . ConfigureAwait ( false ) ;
127
+ }
128
+
129
+ async Task < bool > ICacheClientAsync . ReplaceAsync < T > ( string key , T value , CancellationToken token )
130
+ {
131
+ await using var client = await GetCacheClientAsync ( token ) . ConfigureAwait ( false ) ;
132
+ return await client . ReplaceAsync < T > ( key , value , token ) . ConfigureAwait ( false ) ;
133
+ }
134
+
135
+ async Task < bool > ICacheClientAsync . AddAsync < T > ( string key , T value , DateTime expiresAt , CancellationToken token )
136
+ {
137
+ await using var client = await GetCacheClientAsync ( token ) . ConfigureAwait ( false ) ;
138
+ return await client . AddAsync < T > ( key , value , expiresAt , token ) . ConfigureAwait ( false ) ;
139
+ }
140
+
141
+ async Task < bool > ICacheClientAsync . ReplaceAsync < T > ( string key , T value , DateTime expiresAt , CancellationToken token )
142
+ {
143
+ await using var client = await GetCacheClientAsync ( token ) . ConfigureAwait ( false ) ;
144
+ return await client . ReplaceAsync < T > ( key , value , expiresAt , token ) . ConfigureAwait ( false ) ;
145
+ }
146
+
147
+ async Task < bool > ICacheClientAsync . AddAsync < T > ( string key , T value , TimeSpan expiresIn , CancellationToken token )
148
+ {
149
+ await using var client = await GetCacheClientAsync ( token ) . ConfigureAwait ( false ) ;
150
+ return await client . AddAsync < T > ( key , value , expiresIn , token ) . ConfigureAwait ( false ) ;
151
+ }
152
+
153
+ async Task < bool > ICacheClientAsync . ReplaceAsync < T > ( string key , T value , TimeSpan expiresIn , CancellationToken token )
154
+ {
155
+ await using var client = await GetCacheClientAsync ( token ) . ConfigureAwait ( false ) ;
156
+ return await client . ReplaceAsync < T > ( key , value , expiresIn , token ) . ConfigureAwait ( false ) ;
157
+ }
158
+
159
+ async Task < TimeSpan ? > ICacheClientAsync . GetTimeToLiveAsync ( string key , CancellationToken token )
160
+ {
161
+ await using var client = await GetReadOnlyCacheClientAsync ( token ) . ConfigureAwait ( false ) ;
162
+ return await client . GetTimeToLiveAsync ( key , token ) . ConfigureAwait ( false ) ;
163
+ }
164
+
165
+ async IAsyncEnumerable < string > ICacheClientAsync . GetKeysByPatternAsync ( string pattern , [ EnumeratorCancellation ] CancellationToken token )
166
+ {
167
+ await using var client = await GetReadOnlyCacheClientAsync ( token ) . ConfigureAwait ( false ) ;
168
+ await foreach ( var key in client . GetKeysByPatternAsync ( pattern , token ) . ConfigureAwait ( false ) . WithCancellation ( token ) )
169
+ {
170
+ yield return key ;
171
+ }
172
+ }
173
+
174
+ async Task ICacheClientAsync . RemoveExpiredEntriesAsync ( CancellationToken token )
175
+ {
176
+ await using var client = await GetCacheClientAsync ( token ) . ConfigureAwait ( false ) ;
177
+ await client . RemoveExpiredEntriesAsync ( token ) . ConfigureAwait ( false ) ;
178
+ }
179
+ }
180
+ }
0 commit comments