Skip to content

Commit 3cfdb0f

Browse files
Run automatic code cleanup
1 parent aca58ed commit 3cfdb0f

File tree

16 files changed

+78
-95
lines changed

16 files changed

+78
-95
lines changed

HttpClient.Caching/Abstractions/CacheDataExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ public static class CacheDataExtensions
77
{
88
public static byte[] Serialize(this CacheData cacheData)
99
{
10-
string json = JsonConvert.SerializeObject(cacheData);
11-
byte[] bytes = new byte[json.Length * sizeof(char)];
10+
var json = JsonConvert.SerializeObject(cacheData);
11+
var bytes = new byte[json.Length * sizeof(char)];
1212
Buffer.BlockCopy(json.ToCharArray(), 0, bytes, 0, bytes.Length);
1313
return bytes;
1414
}
@@ -17,9 +17,9 @@ public static CacheData Deserialize(this byte[] cacheData)
1717
{
1818
try
1919
{
20-
char[] chars = new char[cacheData.Length / sizeof(char)];
20+
var chars = new char[cacheData.Length / sizeof(char)];
2121
Buffer.BlockCopy(cacheData, 0, chars, 0, cacheData.Length);
22-
string json = new string(chars);
22+
var json = new string(chars);
2323
var data = JsonConvert.DeserializeObject<CacheData>(json);
2424
return data;
2525
}

HttpClient.Caching/Abstractions/CacheExtensions.cs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,19 @@ public static class CacheExtensions
88
{
99
public static object Get(this IMemoryCache cache, object key)
1010
{
11-
object obj = null;
12-
cache.TryGetValue(key, out obj);
11+
cache.TryGetValue(key, out var obj);
1312
return obj;
1413
}
1514

1615
public static TItem Get<TItem>(this IMemoryCache cache, object key)
1716
{
18-
TItem obj;
19-
cache.TryGetValue(key, out obj);
17+
cache.TryGetValue(key, out TItem obj);
2018
return obj;
2119
}
2220

2321
public static bool TryGetValue<TItem>(this IMemoryCache cache, object key, out TItem value)
2422
{
25-
object obj;
26-
if (cache.TryGetValue(key, out obj))
23+
if (cache.TryGetValue(key, out var obj))
2724
{
2825
value = (TItem)obj;
2926
return true;
@@ -35,15 +32,15 @@ public static bool TryGetValue<TItem>(this IMemoryCache cache, object key, out T
3532

3633
public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value)
3734
{
38-
ICacheEntry entry = cache.CreateEntry(key);
35+
var entry = cache.CreateEntry(key);
3936
entry.Value = value;
4037
entry.Dispose();
4138
return value;
4239
}
4340

4441
public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value, DateTimeOffset absoluteExpiration)
4542
{
46-
ICacheEntry entry = cache.CreateEntry(key);
43+
var entry = cache.CreateEntry(key);
4744
DateTimeOffset? nullable = absoluteExpiration;
4845
entry.AbsoluteExpiration = nullable;
4946
entry.Value = value;
@@ -53,7 +50,7 @@ public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value,
5350

5451
public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value, TimeSpan absoluteExpirationRelativeToNow)
5552
{
56-
ICacheEntry entry = cache.CreateEntry(key);
53+
var entry = cache.CreateEntry(key);
5754
TimeSpan? nullable = absoluteExpirationRelativeToNow;
5855
entry.AbsoluteExpirationRelativeToNow = nullable;
5956
entry.Value = value;
@@ -63,8 +60,8 @@ public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value,
6360

6461
public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value, IChangeToken expirationToken)
6562
{
66-
ICacheEntry entry = cache.CreateEntry(key);
67-
IChangeToken expirationToken1 = expirationToken;
63+
var entry = cache.CreateEntry(key);
64+
var expirationToken1 = expirationToken;
6865
entry.AddExpirationToken(expirationToken1);
6966
entry.Value = value;
7067
entry.Dispose();
@@ -73,7 +70,7 @@ public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value,
7370

7471
public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value, MemoryCacheEntryOptions options)
7572
{
76-
using (ICacheEntry entry = cache.CreateEntry(key))
73+
using (var entry = cache.CreateEntry(key))
7774
{
7875
if (options != null)
7976
{
@@ -88,10 +85,9 @@ public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value,
8885

8986
public static TItem GetOrCreate<TItem>(this IMemoryCache cache, object key, Func<ICacheEntry, TItem> factory)
9087
{
91-
object obj;
92-
if (!cache.TryGetValue(key, out obj))
88+
if (!cache.TryGetValue(key, out var obj))
9389
{
94-
ICacheEntry entry = cache.CreateEntry(key);
90+
var entry = cache.CreateEntry(key);
9591
obj = factory(entry);
9692
entry.SetValue(obj);
9793
entry.Dispose();
@@ -102,10 +98,9 @@ public static TItem GetOrCreate<TItem>(this IMemoryCache cache, object key, Func
10298

10399
public static async Task<TItem> GetOrCreateAsync<TItem>(this IMemoryCache cache, object key, Func<ICacheEntry, Task<TItem>> factory)
104100
{
105-
object obj;
106-
if (!cache.TryGetValue(key, out obj))
101+
if (!cache.TryGetValue(key, out var obj))
107102
{
108-
ICacheEntry entry = cache.CreateEntry(key);
103+
var entry = cache.CreateEntry(key);
109104
obj = await factory(entry);
110105
entry.SetValue(obj);
111106
entry.Dispose();

HttpClient.Caching/Abstractions/StatusCodeExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ public static class StatusCodeExtensions
1717
/// <returns>A TimeSpan that should be used for the given status code when putting it into the cache.</returns>
1818
public static TimeSpan GetAbsoluteExpirationRelativeToNow(this HttpStatusCode statusCode, IDictionary<HttpStatusCode, TimeSpan> mapping)
1919
{
20-
int code = (int)statusCode;
21-
TimeSpan expiration;
20+
var code = (int)statusCode;
2221

2322
// get the expiration settings for the given status code
24-
if (mapping.TryGetValue(statusCode, out expiration))
23+
if (mapping.TryGetValue(statusCode, out var expiration))
2524
{
2625
return expiration;
2726
}

HttpClient.Caching/InMemory/CacheEntry.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ private bool CheckForExpiredTime(DateTimeOffset now)
159159

160160
if (this.slidingExpiration.HasValue)
161161
{
162-
TimeSpan timeSpan = now - this.LastAccessed;
163-
TimeSpan? slidingExpiration = this.slidingExpiration;
162+
var timeSpan = now - this.LastAccessed;
163+
var slidingExpiration = this.slidingExpiration;
164164
if ((slidingExpiration.HasValue ? (timeSpan >= slidingExpiration.GetValueOrDefault() ? 1 : 0) : 0) != 0)
165165
{
166166
this.SetExpired(EvictionReason.Expired);
@@ -175,7 +175,7 @@ internal bool CheckForExpiredTokens()
175175
{
176176
if (this.expirationTokens != null)
177177
{
178-
for (int index = 0; index < ((ICollection<IChangeToken>)this.expirationTokens).Count; ++index)
178+
for (var index = 0; index < this.expirationTokens.Count; ++index)
179179
{
180180
if (this.expirationTokens[index].HasChanged)
181181
{
@@ -197,9 +197,9 @@ internal void AttachTokens()
197197

198198
lock (this.Lock)
199199
{
200-
for (int i = 0; i < this.expirationTokens.Count; ++i)
200+
for (var i = 0; i < this.expirationTokens.Count; ++i)
201201
{
202-
IChangeToken expirationToken = this.expirationTokens[i];
202+
var expirationToken = this.expirationTokens[i];
203203
if (expirationToken.ActiveChangeCallbacks)
204204
{
205205
if (this.expirationTokenRegistrations == null)
@@ -248,10 +248,10 @@ internal void InvokeEvictionCallbacks()
248248
return;
249249
}
250250

251-
TaskFactory factory = Task.Factory;
252-
CancellationToken none = CancellationToken.None;
253-
TaskScheduler scheduler = TaskScheduler.Default;
254-
factory.StartNew((Action<object>)(state => InvokeCallbacks((CacheEntry)state)), (object)this, none, TaskCreationOptions.DenyChildAttach, scheduler);
251+
var factory = Task.Factory;
252+
var none = CancellationToken.None;
253+
var scheduler = TaskScheduler.Default;
254+
factory.StartNew(state => InvokeCallbacks((CacheEntry)state), this, none, TaskCreationOptions.DenyChildAttach, scheduler);
255255
}
256256

257257
private static void InvokeCallbacks(CacheEntry entry)

HttpClient.Caching/InMemory/CacheEntryExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static ICacheEntry RegisterPostEvictionCallback(this ICacheEntry entry, P
7878
throw new ArgumentNullException("callback");
7979
}
8080

81-
return entry.RegisterPostEvictionCallback(callback, (object)null);
81+
return entry.RegisterPostEvictionCallback(callback, null);
8282
}
8383

8484
/// <summary>

HttpClient.Caching/InMemory/IMemoryCacheExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ public static Task<CacheData> TryGetAsync(this IMemoryCache cache, string key)
1919
{
2020
try
2121
{
22-
byte[] binaryData = null;
23-
if (cache.TryGetValue(key, out binaryData))
22+
if (cache.TryGetValue(key, out byte[] binaryData))
2423
{
2524
return Task.FromResult(binaryData.Deserialize());
2625
}
@@ -49,7 +48,7 @@ public static Task TrySetAsync(this IMemoryCache cache, string key, CacheData va
4948
cache.Set(key, value.Serialize(), absoluteExpirationRelativeToNow);
5049
return Task.FromResult(true);
5150
}
52-
catch (Exception ex)
51+
catch (Exception)
5352
{
5453
// ignore all exceptions
5554
return Task.FromResult(false);

HttpClient.Caching/InMemory/InMemoryCacheHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class InMemoryCacheHandler : DelegatingHandler
2323
/// Cache key provider being used
2424
/// </summary>
2525
public ICacheKeysProvider CacheKeysProvider { get; }
26-
26+
2727

2828
/// <summary>
2929
/// Create a new InMemoryCacheHandler.
@@ -93,7 +93,7 @@ public void InvalidateCache(Uri uri, HttpMethod method = null)
9393
foreach (var m in methods)
9494
{
9595
var request = new HttpRequestMessage(m, uri);
96-
var key = CacheKeysProvider.GetKey(request);
96+
var key = this.CacheKeysProvider.GetKey(request);
9797
this.responseCache.Remove(key);
9898
}
9999
}

HttpClient.Caching/InMemory/Internal/SystemClock.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ namespace Microsoft.Extensions.Caching.InMemory.Internal
88
public class SystemClock : ISystemClock
99
{
1010
/// <summary>Retrieves the current system time in UTC.</summary>
11-
public DateTimeOffset UtcNow
12-
{
13-
get { return DateTimeOffset.UtcNow; }
14-
}
11+
public DateTimeOffset UtcNow => DateTimeOffset.UtcNow;
1512
}
1613
}

HttpClient.Caching/InMemory/MemoryCache.cs

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,9 @@ public class MemoryCache : IMemoryCache
1919
private readonly TimeSpan expirationScanFrequency;
2020
private DateTimeOffset lastExpirationScan;
2121

22-
public int Count
23-
{
24-
get { return this.entries.Count; }
25-
}
22+
public int Count => this.entries.Count;
2623

27-
private ICollection<KeyValuePair<object, CacheEntry>> EntriesCollection
28-
{
29-
get { return this.entries; }
30-
}
24+
private ICollection<KeyValuePair<object, CacheEntry>> EntriesCollection => this.entries;
3125

3226
public MemoryCache() : this(new MemoryCacheOptions())
3327
{
@@ -66,12 +60,12 @@ private void SetEntry(CacheEntry entry)
6660
return;
6761
}
6862

69-
DateTimeOffset utcNow = this.clock.UtcNow;
70-
DateTimeOffset? nullable = new DateTimeOffset?();
63+
var utcNow = this.clock.UtcNow;
64+
var nullable = new DateTimeOffset?();
7165
if (entry.absoluteExpirationRelativeToNow.HasValue)
7266
{
73-
DateTimeOffset dateTimeOffset = utcNow;
74-
TimeSpan? expirationRelativeToNow = entry.absoluteExpirationRelativeToNow;
67+
var dateTimeOffset = utcNow;
68+
var expirationRelativeToNow = entry.absoluteExpirationRelativeToNow;
7569
nullable = expirationRelativeToNow.HasValue ? dateTimeOffset + expirationRelativeToNow.GetValueOrDefault() : new DateTimeOffset?();
7670
}
7771
else if (entry.absoluteExpiration.HasValue)
@@ -142,10 +136,9 @@ public bool TryGetValue(object key, out object result)
142136

143137
this.CheckDisposed();
144138
result = null;
145-
DateTimeOffset utcNow = this.clock.UtcNow;
146-
bool flag = false;
147-
CacheEntry entry;
148-
if (this.entries.TryGetValue(key, out entry))
139+
var utcNow = this.clock.UtcNow;
140+
var flag = false;
141+
if (this.entries.TryGetValue(key, out var entry))
149142
{
150143
if (entry.CheckExpired(utcNow) && entry.EvictionReason != EvictionReason.Replaced)
151144
{
@@ -172,8 +165,7 @@ public void Remove(object key)
172165
}
173166

174167
this.CheckDisposed();
175-
CacheEntry cacheEntry;
176-
if (this.entries.TryRemove(key, out cacheEntry))
168+
if (this.entries.TryRemove(key, out var cacheEntry))
177169
{
178170
cacheEntry.SetExpired(EvictionReason.Removed);
179171
cacheEntry.InvokeEvictionCallbacks();
@@ -216,22 +208,22 @@ private void EntryExpired(CacheEntry entry)
216208

217209
private void StartScanForExpiredItems()
218210
{
219-
DateTimeOffset utcNow = this.clock.UtcNow;
211+
var utcNow = this.clock.UtcNow;
220212
if (!(this.expirationScanFrequency < utcNow - this.lastExpirationScan))
221213
{
222214
return;
223215
}
224216

225217
this.lastExpirationScan = utcNow;
226-
TaskFactory factory = Task.Factory;
227-
CancellationToken none = CancellationToken.None;
228-
TaskScheduler scheduler = TaskScheduler.Default;
218+
var factory = Task.Factory;
219+
var none = CancellationToken.None;
220+
var scheduler = TaskScheduler.Default;
229221
factory.StartNew(state => ScanForExpiredItems((MemoryCache)state), this, none, TaskCreationOptions.DenyChildAttach, scheduler);
230222
}
231223

232224
private static void ScanForExpiredItems(MemoryCache cache)
233225
{
234-
DateTimeOffset utcNow = cache.clock.UtcNow;
226+
var utcNow = cache.clock.UtcNow;
235227
foreach (var entry in cache.entries.Values)
236228
{
237229
if (entry.CheckExpired(utcNow))
@@ -243,13 +235,13 @@ private static void ScanForExpiredItems(MemoryCache cache)
243235

244236
public void Compact(double percentage)
245237
{
246-
List<CacheEntry> entriesToRemove = new List<CacheEntry>();
247-
List<CacheEntry> priorityEntries1 = new List<CacheEntry>();
248-
List<CacheEntry> priorityEntries2 = new List<CacheEntry>();
249-
List<CacheEntry> priorityEntries3 = new List<CacheEntry>();
250-
DateTimeOffset utcNow = this.clock.UtcNow;
238+
var entriesToRemove = new List<CacheEntry>();
239+
var priorityEntries1 = new List<CacheEntry>();
240+
var priorityEntries2 = new List<CacheEntry>();
241+
var priorityEntries3 = new List<CacheEntry>();
242+
var utcNow = this.clock.UtcNow;
251243

252-
foreach (CacheEntry cacheEntry in this.entries.Values)
244+
foreach (var cacheEntry in this.entries.Values)
253245
{
254246
if (cacheEntry.CheckExpired(utcNow))
255247
{
@@ -271,12 +263,12 @@ public void Compact(double percentage)
271263
case 3:
272264
continue;
273265
default:
274-
throw new NotSupportedException("Not implemented: " + (object)cacheEntry.Priority);
266+
throw new NotSupportedException("Not implemented: " + cacheEntry.Priority);
275267
}
276268
}
277269
}
278270

279-
int removalCountTarget = (int)(this.entries.Count * percentage);
271+
var removalCountTarget = (int)(this.entries.Count * percentage);
280272
this.ExpirePriorityBucket(removalCountTarget, entriesToRemove, priorityEntries1);
281273
this.ExpirePriorityBucket(removalCountTarget, entriesToRemove, priorityEntries2);
282274
this.ExpirePriorityBucket(removalCountTarget, entriesToRemove, priorityEntries3);

HttpClient.Caching/InMemory/MemoryCacheEntryOptions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public TimeSpan? AbsoluteExpirationRelativeToNow
4141
get { return this.absoluteExpirationRelativeToNow; }
4242
set
4343
{
44-
TimeSpan? nullable = value;
45-
TimeSpan zero = TimeSpan.Zero;
44+
var nullable = value;
45+
var zero = TimeSpan.Zero;
4646
if ((nullable.HasValue ? (nullable.GetValueOrDefault() <= zero ? 1 : 0) : 0) != 0)
4747
{
4848
throw new ArgumentOutOfRangeException(nameof(this.AbsoluteExpirationRelativeToNow), value, "The relative expiration value must be positive.");
@@ -61,8 +61,8 @@ public TimeSpan? SlidingExpiration
6161
get { return this.slidingExpiration; }
6262
set
6363
{
64-
TimeSpan? nullable = value;
65-
TimeSpan zero = TimeSpan.Zero;
64+
var nullable = value;
65+
var zero = TimeSpan.Zero;
6666
if ((nullable.HasValue ? (nullable.GetValueOrDefault() <= zero ? 1 : 0) : 0) != 0)
6767
{
6868
throw new ArgumentOutOfRangeException(nameof(this.SlidingExpiration), value, "The sliding expiration value must be positive.");

0 commit comments

Comments
 (0)