Skip to content

Develop #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions HttpClient.Caching/Abstractions/EvictionReason.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
{
public enum EvictionReason
{
None,
Removed,
Replaced,
Expired,
TokenExpired,
Capacity,
None = 0,
Removed = 1,
Replaced = 2,
Expired = 3,
TokenExpired = 4,
Capacity = 5,
}
}
17 changes: 16 additions & 1 deletion HttpClient.Caching/HttpClient.Caching.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard1.2</TargetFramework>
<TargetFrameworks>net45;netstandard1.2</TargetFrameworks>
<Authors>Thomas Galliker</Authors>
<Company>superdev GmbH</Company>
<Description>HttpClient.Caching adds http response caching to HttpClient.</Description>
Expand All @@ -21,4 +21,19 @@
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Data.Linq" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.WebRequest" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="System" />
</ItemGroup>

</Project>
36 changes: 16 additions & 20 deletions HttpClient.Caching/InMemory/CacheEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Type: Microsoft.Extensions.Caching.Memory.CacheEntry
// Assembly: Microsoft.Extensions.Caching.Memory, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
// MVID: 78529ED0-C4AD-4926-BA4D-60032404EE9B
// Assembly location: C:\Users\thomas\AppData\Local\Temp\Rar$DI01.488\Microsoft.Extensions.Caching.Memory.dll

using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -45,7 +43,7 @@ public TimeSpan? AbsoluteExpirationRelativeToNow
TimeSpan zero = TimeSpan.Zero;
if ((nullable.HasValue ? (nullable.GetValueOrDefault() <= zero ? 1 : 0) : 0) != 0)
{
throw new ArgumentOutOfRangeException("AbsoluteExpirationRelativeToNow", (object)value, "The relative expiration value must be positive.");
throw new ArgumentOutOfRangeException(nameof(this.AbsoluteExpirationRelativeToNow), value, "The relative expiration value must be positive.");
}

this.absoluteExpirationRelativeToNow = value;
Expand All @@ -61,7 +59,7 @@ public TimeSpan? SlidingExpiration
TimeSpan zero = TimeSpan.Zero;
if ((nullable.HasValue ? (nullable.GetValueOrDefault() <= zero ? 1 : 0) : 0) != 0)
{
throw new ArgumentOutOfRangeException("SlidingExpiration", (object)value, "The sliding expiration value must be positive.");
throw new ArgumentOutOfRangeException(nameof(this.SlidingExpiration), value, "The sliding expiration value must be positive.");
}

this.slidingExpiration = value;
Expand All @@ -74,7 +72,7 @@ public IList<IChangeToken> ExpirationTokens
{
if (this.expirationTokens == null)
{
this.expirationTokens = (IList<IChangeToken>)new List<IChangeToken>();
this.expirationTokens = new List<IChangeToken>();
}

return this.expirationTokens;
Expand All @@ -87,7 +85,7 @@ public IList<PostEvictionCallbackRegistration> PostEvictionCallbacks
{
if (this._postEvictionCallbacks == null)
{
this._postEvictionCallbacks = (IList<PostEvictionCallbackRegistration>)new List<PostEvictionCallbackRegistration>();
this._postEvictionCallbacks = new List<PostEvictionCallbackRegistration>();
}

return this._postEvictionCallbacks;
Expand Down Expand Up @@ -161,7 +159,7 @@ private bool CheckForExpiredTime(DateTimeOffset now)
{
if (this.absoluteExpiration.HasValue && this.absoluteExpiration.Value <= now)
{
this.SetExpired((EvictionReason)3);
this.SetExpired(EvictionReason.Expired);
return true;
}

Expand All @@ -171,7 +169,7 @@ private bool CheckForExpiredTime(DateTimeOffset now)
TimeSpan? slidingExpiration = this.slidingExpiration;
if ((slidingExpiration.HasValue ? (timeSpan >= slidingExpiration.GetValueOrDefault() ? 1 : 0) : 0) != 0)
{
this.SetExpired((EvictionReason)3);
this.SetExpired(EvictionReason.Expired);
return true;
}
}
Expand All @@ -187,7 +185,7 @@ internal bool CheckForExpiredTokens()
{
if (this.expirationTokens[index].HasChanged)
{
this.SetExpired((EvictionReason)4);
this.SetExpired(EvictionReason.TokenExpired);
return true;
}
}
Expand Down Expand Up @@ -223,12 +221,12 @@ internal void AttachTokens()

private static void ExpirationTokensExpired(object obj)
{
Task.Factory.StartNew((Action<object>)(state =>
Task.Factory.StartNew(state =>
{
CacheEntry cacheEntry = (CacheEntry)state;
cacheEntry.SetExpired((EvictionReason)4);
var cacheEntry = (CacheEntry)state;
cacheEntry.SetExpired(EvictionReason.TokenExpired);
cacheEntry._notifyCacheOfExpiration(cacheEntry);
}), obj, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
}, obj, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
}

private void DetachTokens()
Expand All @@ -242,9 +240,9 @@ private void DetachTokens()
}

this.expirationTokenRegistrations = null;
for (int i = 0; i < tokenRegistrations.Count; ++i)
foreach (var disposable in tokenRegistrations)
{
tokenRegistrations[i].Dispose();
disposable.Dispose();
}
}
}
Expand All @@ -258,22 +256,20 @@ internal void InvokeEvictionCallbacks()

TaskFactory factory = Task.Factory;
CancellationToken none = CancellationToken.None;
int num = 8;
TaskScheduler scheduler = TaskScheduler.Default;
factory.StartNew((Action<object>)(state => InvokeCallbacks((CacheEntry)state)), (object)this, none, (TaskCreationOptions)num, scheduler);
factory.StartNew((Action<object>)(state => InvokeCallbacks((CacheEntry)state)), (object)this, none, TaskCreationOptions.DenyChildAttach, scheduler);
}

private static void InvokeCallbacks(CacheEntry entry)
{
IList<PostEvictionCallbackRegistration> callbackRegistrationList = Interlocked.Exchange<IList<PostEvictionCallbackRegistration>>(ref entry._postEvictionCallbacks, (IList<PostEvictionCallbackRegistration>)null);
var callbackRegistrationList = Interlocked.Exchange(ref entry._postEvictionCallbacks, null);
if (callbackRegistrationList == null)
{
return;
}

for (int index = 0; index < ((ICollection<PostEvictionCallbackRegistration>)callbackRegistrationList).Count; ++index)
foreach (var callbackRegistration in callbackRegistrationList)
{
PostEvictionCallbackRegistration callbackRegistration = callbackRegistrationList[index];
try
{
PostEvictionDelegate evictionCallback = callbackRegistration.EvictionCallback;
Expand Down
13 changes: 6 additions & 7 deletions HttpClient.Caching/InMemory/CacheEntryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Caching.Abstractions;

namespace Microsoft.Extensions.Caching.InMemory
Expand Down Expand Up @@ -30,7 +29,7 @@ public static ICacheEntry AddExpirationToken(this ICacheEntry entry, IChangeToke
{
if (expirationToken == null)
{
throw new ArgumentNullException("expirationToken");
throw new ArgumentNullException(nameof(expirationToken));
}

entry.ExpirationTokens.Add(expirationToken);
Expand All @@ -42,7 +41,7 @@ public static ICacheEntry AddExpirationToken(this ICacheEntry entry, IChangeToke
/// <param name="relative"></param>
public static ICacheEntry SetAbsoluteExpiration(this ICacheEntry entry, TimeSpan relative)
{
entry.AbsoluteExpirationRelativeToNow = new TimeSpan?(relative);
entry.AbsoluteExpirationRelativeToNow = relative;
return entry;
}

Expand All @@ -51,7 +50,7 @@ public static ICacheEntry SetAbsoluteExpiration(this ICacheEntry entry, TimeSpan
/// <param name="absolute"></param>
public static ICacheEntry SetAbsoluteExpiration(this ICacheEntry entry, DateTimeOffset absolute)
{
entry.AbsoluteExpiration = new DateTimeOffset?(absolute);
entry.AbsoluteExpiration = absolute;
return entry;
}

Expand All @@ -63,7 +62,7 @@ public static ICacheEntry SetAbsoluteExpiration(this ICacheEntry entry, DateTime
/// <param name="offset"></param>
public static ICacheEntry SetSlidingExpiration(this ICacheEntry entry, TimeSpan offset)
{
entry.SlidingExpiration = new TimeSpan?(offset);
entry.SlidingExpiration = offset;
return entry;
}

Expand Down Expand Up @@ -125,12 +124,12 @@ public static ICacheEntry SetOptions(this ICacheEntry entry, MemoryCacheEntryOpt
entry.AbsoluteExpirationRelativeToNow = options.AbsoluteExpirationRelativeToNow;
entry.SlidingExpiration = options.SlidingExpiration;
entry.Priority = options.Priority;
foreach (IChangeToken expirationToken in (IEnumerable<IChangeToken>)options.ExpirationTokens)
foreach (var expirationToken in options.ExpirationTokens)
{
entry.AddExpirationToken(expirationToken);
}

foreach (PostEvictionCallbackRegistration evictionCallback in (IEnumerable<PostEvictionCallbackRegistration>)options.PostEvictionCallbacks)
foreach (var evictionCallback in options.PostEvictionCallbacks)
{
entry.RegisterPostEvictionCallback(evictionCallback.EvictionCallback, evictionCallback.State);
}
Expand Down
31 changes: 14 additions & 17 deletions HttpClient.Caching/InMemory/MemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@ private void SetEntry(CacheEntry entry)
}

entry.LastAccessed = utcNow;
CacheEntry cacheEntry;
if (this.entries.TryGetValue(entry.Key, out cacheEntry))
if (this.entries.TryGetValue(entry.Key, out var cacheEntry))
{
cacheEntry.SetExpired((EvictionReason)2);
cacheEntry.SetExpired(EvictionReason.Replaced);
}

if (!entry.CheckExpired(utcNow))
Expand Down Expand Up @@ -142,7 +141,7 @@ public bool TryGetValue(object key, out object result)
}

this.CheckDisposed();
result = (object)null;
result = null;
DateTimeOffset utcNow = this.clock.UtcNow;
bool flag = false;
CacheEntry entry;
Expand Down Expand Up @@ -176,7 +175,7 @@ public void Remove(object key)
CacheEntry cacheEntry;
if (this.entries.TryRemove(key, out cacheEntry))
{
cacheEntry.SetExpired((EvictionReason)1);
cacheEntry.SetExpired(EvictionReason.Removed);
cacheEntry.InvokeEvictionCallbacks();
}

Expand All @@ -189,10 +188,9 @@ public void Clear()
var keys = this.entries.Keys.ToList();
foreach (var key in keys)
{
CacheEntry cacheEntry;
if (this.entries.TryRemove(key, out cacheEntry))
if (this.entries.TryRemove(key, out var cacheEntry))
{
cacheEntry.SetExpired((EvictionReason)1);
cacheEntry.SetExpired(EvictionReason.Removed);
cacheEntry.InvokeEvictionCallbacks();
}
}
Expand Down Expand Up @@ -227,15 +225,14 @@ private void StartScanForExpiredItems()
this.lastExpirationScan = utcNow;
TaskFactory factory = Task.Factory;
CancellationToken none = CancellationToken.None;
int num = 8;
TaskScheduler scheduler = TaskScheduler.Default;
factory.StartNew((Action<object>)(state => ScanForExpiredItems((MemoryCache)state)), (object)this, none, (TaskCreationOptions)num, scheduler);
factory.StartNew(state => ScanForExpiredItems((MemoryCache)state), this, none, TaskCreationOptions.DenyChildAttach, scheduler);
}

private static void ScanForExpiredItems(MemoryCache cache)
{
DateTimeOffset utcNow = cache.clock.UtcNow;
foreach (CacheEntry entry in (IEnumerable<CacheEntry>)cache.entries.Values)
foreach (var entry in cache.entries.Values)
{
if (entry.CheckExpired(utcNow))
{
Expand Down Expand Up @@ -279,11 +276,11 @@ public void Compact(double percentage)
}
}

int removalCountTarget = (int)((double)this.entries.Count * percentage);
int removalCountTarget = (int)(this.entries.Count * percentage);
this.ExpirePriorityBucket(removalCountTarget, entriesToRemove, priorityEntries1);
this.ExpirePriorityBucket(removalCountTarget, entriesToRemove, priorityEntries2);
this.ExpirePriorityBucket(removalCountTarget, entriesToRemove, priorityEntries3);
foreach (CacheEntry entry in entriesToRemove)
foreach (var entry in entriesToRemove)
{
this.RemoveEntry(entry);
}
Expand All @@ -300,16 +297,16 @@ private void ExpirePriorityBucket(int removalCountTarget, List<CacheEntry> entri
{
foreach (var priorityEntry in priorityEntries)
{
priorityEntry.SetExpired((EvictionReason)5);
priorityEntry.SetExpired(EvictionReason.Capacity);
}

entriesToRemove.AddRange((IEnumerable<CacheEntry>)priorityEntries);
entriesToRemove.AddRange(priorityEntries);
}
else
{
foreach (var cacheEntry in priorityEntries.OrderBy(entry => entry.LastAccessed))
{
cacheEntry.SetExpired((EvictionReason)5);
cacheEntry.SetExpired(EvictionReason.Capacity);
entriesToRemove.Add(cacheEntry);
if (removalCountTarget <= entriesToRemove.Count)
{
Expand All @@ -333,7 +330,7 @@ protected virtual void Dispose(bool disposing)

if (disposing)
{
GC.SuppressFinalize((object)this);
GC.SuppressFinalize(this);
}

this.disposed = true;
Expand Down
8 changes: 4 additions & 4 deletions HttpClient.Caching/InMemory/MemoryCacheEntryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public class MemoryCacheEntryOptions
/// entry to
/// expire.
/// </summary>
public IList<IChangeToken> ExpirationTokens { get; } = (IList<IChangeToken>)new List<IChangeToken>();
public IList<IChangeToken> ExpirationTokens { get; } = new List<IChangeToken>();

/// <summary>
/// Gets or sets the callbacks will be fired after the cache entry is evicted from the cache.
/// </summary>
public IList<PostEvictionCallbackRegistration> PostEvictionCallbacks { get; } = (IList<PostEvictionCallbackRegistration>)new List<PostEvictionCallbackRegistration>();
public IList<PostEvictionCallbackRegistration> PostEvictionCallbacks { get; } = new List<PostEvictionCallbackRegistration>();

/// <summary>
/// Gets or sets the priority for keeping the cache entry in the cache during a
Expand All @@ -45,7 +45,7 @@ public TimeSpan? AbsoluteExpirationRelativeToNow
TimeSpan zero = TimeSpan.Zero;
if ((nullable.HasValue ? (nullable.GetValueOrDefault() <= zero ? 1 : 0) : 0) != 0)
{
throw new ArgumentOutOfRangeException("AbsoluteExpirationRelativeToNow", (object)value, "The relative expiration value must be positive.");
throw new ArgumentOutOfRangeException(nameof(this.AbsoluteExpirationRelativeToNow), value, "The relative expiration value must be positive.");
}

this.absoluteExpirationRelativeToNow = value;
Expand All @@ -65,7 +65,7 @@ public TimeSpan? SlidingExpiration
TimeSpan zero = TimeSpan.Zero;
if ((nullable.HasValue ? (nullable.GetValueOrDefault() <= zero ? 1 : 0) : 0) != 0)
{
throw new ArgumentOutOfRangeException("SlidingExpiration", (object)value, "The sliding expiration value must be positive.");
throw new ArgumentOutOfRangeException(nameof(this.SlidingExpiration), value, "The sliding expiration value must be positive.");
}

this.slidingExpiration = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">
<Reference Include="System.Net.Http" />
</ItemGroup>

<!-- Include files in the Resources directory to be used across the test suite -->
<ItemGroup>
<EmbeddedResource Include="Resources\**\*.*" />
Expand Down
3 changes: 3 additions & 0 deletions Tests/HttpClient.Caching.Tests/MemoryCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public void ShouldSetCache()
}

// Assert
cache.TryGetValue("1", out var result1);
result1.Should().NotBeNull();
result1.Should().BeOfType<TestPayload>().Which.Id.Should().Be(1);
cache.Count.Should().Be(10);
}
}
Expand Down