1
- using System ;
2
- using System . Linq ;
3
- using Enyim . Caching . Configuration ;
1
+ using Enyim . Caching . Configuration ;
4
2
using Enyim . Caching . Memcached ;
5
- using System . Collections . Generic ;
6
- using System . Threading ;
7
- using System . Net ;
8
- using System . Diagnostics ;
9
3
using Enyim . Caching . Memcached . Results ;
10
- using Enyim . Caching . Memcached . Results . Factories ;
11
4
using Enyim . Caching . Memcached . Results . Extensions ;
12
- using System . Threading . Tasks ;
5
+ using Enyim . Caching . Memcached . Results . Factories ;
6
+ using Microsoft . Extensions . Caching . Distributed ;
13
7
using Microsoft . Extensions . Logging ;
14
8
using Microsoft . Extensions . Options ;
15
- using Microsoft . Extensions . Caching . Distributed ;
9
+ using System ;
10
+ using System . Collections . Generic ;
11
+ using System . Diagnostics ;
12
+ using System . Linq ;
13
+ using System . Net ;
14
+ using System . Threading ;
15
+ using System . Threading . Tasks ;
16
16
17
17
namespace Enyim . Caching
18
18
{
@@ -113,7 +113,6 @@ public async Task SetAsync(string key, object value, int cacheSeconds)
113
113
/// </summary>
114
114
/// <param name="key">The identifier for the item to retrieve.</param>
115
115
/// <returns>The retrieved item, or <value>null</value> if the key was not found.</returns>
116
- [ Obsolete ]
117
116
public object Get ( string key )
118
117
{
119
118
object tmp ;
@@ -129,95 +128,85 @@ public object Get(string key)
129
128
[ Obsolete ]
130
129
public T Get < T > ( string key )
131
130
{
132
- var hashedKey = this . keyTransformer . Transform ( key ) ;
133
- var node = this . pool . Locate ( hashedKey ) ;
131
+ var result = PerformGet < T > ( key ) ;
132
+ return result . Success ? result . Value : default ( T ) ;
133
+ }
134
134
135
- if ( node != null )
135
+ public IGetOperationResult < T > PerformGet < T > ( string key )
136
+ {
137
+ if ( ! CreateGetCommand < T > ( key , out var result , out var node , out var command ) )
136
138
{
137
- try
138
- {
139
- var command = this . pool . OperationFactory . Get ( hashedKey ) ;
140
- var commandResult = node . Execute ( command ) ;
139
+ return result ;
140
+ }
141
+
142
+ try
143
+ {
144
+ var commandResult = node . Execute ( command ) ;
145
+ return BuildGetCommandResult < T > ( result , command , commandResult ) ;
141
146
142
- if ( commandResult . Success )
143
- {
144
- if ( typeof ( T ) . GetTypeCode ( ) == TypeCode . Object && typeof ( T ) != typeof ( Byte [ ] ) )
145
- {
146
- return this . transcoder . Deserialize < T > ( command . Result ) ;
147
- }
148
- else
149
- {
150
- var tempResult = this . transcoder . Deserialize ( command . Result ) ;
151
- if ( tempResult != null )
152
- {
153
- if ( typeof ( T ) == typeof ( Guid ) )
154
- {
155
- return ( T ) ( object ) new Guid ( ( string ) tempResult ) ;
156
- }
157
- else
158
- {
159
- return ( T ) tempResult ;
160
- }
161
- }
162
- }
163
- }
164
- }
165
- catch ( Exception ex )
166
- {
167
- _logger . LogError ( 0 , ex , $ "{ nameof ( GetAsync ) } (\" { key } \" )") ;
168
- throw ex ;
169
- }
170
147
}
171
- else
148
+ catch ( Exception ex )
172
149
{
173
- _logger . LogError ( $ "Unable to locate memcached node") ;
150
+ _logger . LogError ( 0 , ex , $ "{ nameof ( PerformGet ) } (\" { key } \" )") ;
151
+ result . Fail ( ex . Message ) ;
152
+ return result ;
174
153
}
175
-
176
- return default ( T ) ;
177
154
}
178
155
179
- public async Task < IGetOperationResult < T > > GetAsync < T > ( string key )
156
+ private bool CreateGetCommand < T > ( string key , out IGetOperationResult < T > result , out IMemcachedNode node , out IGetOperation command )
180
157
{
181
- var result = new DefaultGetOperationResultFactory < T > ( ) . Create ( ) ;
182
-
158
+ result = new DefaultGetOperationResultFactory < T > ( ) . Create ( ) ;
183
159
var hashedKey = this . keyTransformer . Transform ( key ) ;
184
- var node = this . pool . Locate ( hashedKey ) ;
185
160
186
- if ( node != null )
161
+ node = this . pool . Locate ( hashedKey ) ;
162
+ if ( node == null )
187
163
{
188
- try
189
- {
190
- var command = this . pool . OperationFactory . Get ( hashedKey ) ;
191
- var commandResult = await node . ExecuteAsync ( command ) ;
164
+ var errorMessage = $ "Unable to locate node with \" { key } \" key";
165
+ _logger . LogError ( errorMessage ) ;
166
+ result . Fail ( errorMessage ) ;
167
+ command = null ;
168
+ return false ;
169
+ }
192
170
193
- if ( commandResult . Success )
194
- {
195
- result . Success = true ;
196
- result . Value = transcoder . Deserialize < T > ( command . Result ) ;
197
- return result ;
198
- }
199
- else
200
- {
201
- commandResult . Combine ( result ) ;
171
+ command = this . pool . OperationFactory . Get ( hashedKey ) ;
172
+ return true ;
173
+ }
202
174
203
- return result ;
204
- }
205
- }
206
- catch ( Exception ex )
207
- {
208
- _logger . LogError ( 0 , ex , $ "{ nameof ( GetAsync ) } (\" { key } \" )") ;
209
- throw ex ;
210
- }
175
+ private IGetOperationResult < T > BuildGetCommandResult < T > ( IGetOperationResult < T > result , IGetOperation command , IOperationResult commandResult )
176
+ {
177
+ if ( commandResult . Success )
178
+ {
179
+ result . Value = transcoder . Deserialize < T > ( command . Result ) ;
180
+ result . Pass ( ) ;
211
181
}
212
182
else
213
183
{
214
- _logger . LogError ( $ "Unable to locate memcached node" ) ;
184
+ commandResult . Combine ( result ) ;
215
185
}
216
186
217
- result . Fail ( "Unable to locate node" ) ;
218
187
return result ;
219
188
}
220
189
190
+ public async Task < IGetOperationResult < T > > GetAsync < T > ( string key )
191
+ {
192
+ if ( ! CreateGetCommand < T > ( key , out var result , out var node , out var command ) )
193
+ {
194
+ return result ;
195
+ }
196
+
197
+ try
198
+ {
199
+ var commandResult = await node . ExecuteAsync ( command ) ;
200
+ return BuildGetCommandResult < T > ( result , command , commandResult ) ;
201
+ }
202
+ catch ( Exception ex )
203
+ {
204
+ _logger . LogError ( 0 , ex , $ "{ nameof ( GetAsync ) } (\" { key } \" )") ;
205
+ result . Fail ( ex . Message ) ;
206
+ return result ;
207
+ }
208
+ }
209
+
221
210
public async Task < T > GetValueAsync < T > ( string key )
222
211
{
223
212
var result = await GetAsync < T > ( key ) ;
@@ -246,7 +235,7 @@ public async Task<T> GetValueOrCreateAsync<T>(string key, int cacheSeconds, Func
246
235
{
247
236
await AddAsync ( key , value , cacheSeconds ) ;
248
237
}
249
- catch ( Exception ex )
238
+ catch ( Exception ex )
250
239
{
251
240
_logger . LogError ( ex , $ "{ nameof ( AddAsync ) } (\" { key } \" , ..., { cacheSeconds } )") ;
252
241
}
@@ -260,21 +249,18 @@ public async Task<T> GetValueOrCreateAsync<T>(string key, int cacheSeconds, Func
260
249
/// <param name="key">The identifier for the item to retrieve.</param>
261
250
/// <param name="value">The retrieved item or null if not found.</param>
262
251
/// <returns>The <value>true</value> if the item was successfully retrieved.</returns>
263
- [ Obsolete ]
264
252
public bool TryGet ( string key , out object value )
265
253
{
266
254
ulong cas = 0 ;
267
255
268
256
return this . PerformTryGet ( key , out cas , out value ) . Success ;
269
257
}
270
258
271
- [ Obsolete ]
272
259
public CasResult < object > GetWithCas ( string key )
273
260
{
274
261
return this . GetWithCas < object > ( key ) ;
275
262
}
276
263
277
- [ Obsolete ]
278
264
public CasResult < T > GetWithCas < T > ( string key )
279
265
{
280
266
CasResult < object > tmp ;
@@ -284,7 +270,6 @@ public CasResult<T> GetWithCas<T>(string key)
284
270
: new CasResult < T > { Cas = tmp . Cas , Result = default ( T ) } ;
285
271
}
286
272
287
- [ Obsolete ]
288
273
public bool TryGetWithCas ( string key , out CasResult < object > value )
289
274
{
290
275
object tmp ;
0 commit comments