Skip to content

Commit

Permalink
Issue 121 (#123)
Browse files Browse the repository at this point in the history
* Fix sliding logic.

* Fix issue #121
  • Loading branch information
chullybun authored Apr 8, 2021
1 parent aa9b984 commit 01ff795
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Beef.Core/Beef.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<RootNamespace>Beef</RootNamespace>
<Version>4.1.9</Version>
<Version>4.1.10</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Beef Developers</Authors>
<Company>Avanade</Company>
Expand Down
3 changes: 3 additions & 0 deletions src/Beef.Core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Represents the **NuGet** versions.

## v4.1.10
- *Fixed:* Issue [121](https://github.com/Avanade/Beef/issues/121). `SlidingCachePolicy` sliding logic functions as expected.

## v4.1.9
- *Fixed:* Issue [121](https://github.com/Avanade/Beef/issues/121). `SlidingCachePolicy` will now cache correctly after first expiry.

Expand Down
3 changes: 0 additions & 3 deletions src/Beef.Core/Caching/Policy/MaxHitsCachePolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ public bool IsExpired
/// </summary>
bool ICachePolicy.HasExpired()
{
if (IsExpired)
return true;

Hits++;
return IsExpired;
}
Expand Down
21 changes: 11 additions & 10 deletions src/Beef.Core/Caching/Policy/SlidingCachePolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,14 @@ public bool IsExpired
bool ICachePolicy.HasExpired()
{
var isExpired = IsExpired;
if (!isExpired)
Reset();
if (!isExpired && _maxDuration.TotalMilliseconds > 0)
{
var expiry = Entities.Cleaner.Clean(DateTime.Now).Add(_duration);
if (_maxExpiry.HasValue && expiry > _maxExpiry.Value)
expiry = _maxExpiry.Value;

Expiry = expiry;
}

Hits++;
return isExpired;
Expand All @@ -107,7 +113,6 @@ bool ICachePolicy.HasExpired()
public void Refresh()
{
Expiry = null;
_maxExpiry = null;
}

/// <summary>
Expand All @@ -116,16 +121,12 @@ public void Refresh()
public void Reset()
{
DateTime now = Entities.Cleaner.Clean(DateTime.Now);
if (_maxExpiry.HasValue && _maxExpiry <= now)
_maxExpiry = null;

DateTime? expiry = now.Add(_duration);

if (!_maxExpiry.HasValue && _maxDuration.TotalMilliseconds > 0)
if (_maxDuration.TotalMilliseconds > 0)
_maxExpiry = now.Add(_maxDuration);

if (_maxExpiry.HasValue && Expiry > _maxExpiry.Value)
expiry = _maxExpiry;
else
_maxExpiry = null;

if (_randomizerOffset.HasValue)
expiry = CachePolicyManager.AddRandomizedOffsetToTime(expiry.Value, _randomizerOffset.Value);
Expand Down
46 changes: 40 additions & 6 deletions tests/Beef.Core.UnitTest/Caching/Policy/SlidingCachePolicyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,54 @@ namespace Beef.Core.UnitTest.Caching.Policy
public class SlidingCachePolicyTest
{
[Test]
public void Slide()
public void SlideNoMax()
{
var scp = new SlidingCachePolicy { Duration = new TimeSpan(0, 0, 1) };
scp.Reset();

Assert.IsFalse(((ICachePolicy)scp).HasExpired());
Thread.Sleep(500);
Thread.Sleep(450);
Assert.IsFalse(((ICachePolicy)scp).HasExpired());
Thread.Sleep(500);
Thread.Sleep(450);
Assert.IsFalse(((ICachePolicy)scp).HasExpired());
Thread.Sleep(500);
Thread.Sleep(450);
Assert.IsTrue(((ICachePolicy)scp).HasExpired());
Thread.Sleep(450);
Assert.IsTrue(((ICachePolicy)scp).HasExpired());
}

[Test]
public void SlideWithMax()
{
var scp = new SlidingCachePolicy { Duration = new TimeSpan(0, 0, 1), MaxDuration = new TimeSpan(0, 0, 2) };
scp.Reset();

Assert.IsFalse(((ICachePolicy)scp).HasExpired());
Thread.Sleep(450);
Assert.IsFalse(((ICachePolicy)scp).HasExpired());
Thread.Sleep(450);
Assert.IsFalse(((ICachePolicy)scp).HasExpired());
Thread.Sleep(1500);
Assert.IsTrue(scp.IsExpired);
Thread.Sleep(450);
Assert.IsFalse(((ICachePolicy)scp).HasExpired());
Thread.Sleep(450);
Assert.IsFalse(((ICachePolicy)scp).HasExpired());
Thread.Sleep(450);
Assert.IsTrue(((ICachePolicy)scp).HasExpired());
}

[Test]
public void SlideWithMaxGap()
{
var scp = new SlidingCachePolicy { Duration = new TimeSpan(0, 0, 1), MaxDuration = new TimeSpan(0, 0, 2) };
scp.Reset();

Assert.IsFalse(((ICachePolicy)scp).HasExpired());
Thread.Sleep(350);
Assert.IsFalse(((ICachePolicy)scp).HasExpired());
Thread.Sleep(1100);
Assert.IsTrue(((ICachePolicy)scp).HasExpired());
Thread.Sleep(450);
Assert.IsTrue(((ICachePolicy)scp).HasExpired());
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void Exercise_SlidingCache()
Assert.AreEqual(3 + cycle, c.ActiveList.Count);
Assert.AreEqual(3 + cycle, rdc.Count);
Assert.IsFalse(rdc.IsExpired);
System.Threading.Thread.Sleep((int)Math.Floor(policy.Duration.TotalSeconds / 2));
System.Threading.Thread.Sleep((int)Math.Floor(policy.Duration.TotalMilliseconds / 2));
}

// reload collection cached.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public void Exercise_SlidingCache()
Assert.AreEqual(2, c.ActiveList.Count);
Assert.AreEqual(1, rdc.Count);
Assert.IsFalse(rdc.IsExpired);
System.Threading.Thread.Sleep((int)Math.Floor(policy.Duration.TotalSeconds / 2));
System.Threading.Thread.Sleep((int)Math.Floor(policy.Duration.TotalMilliseconds / 2));
}

// reload collection cached.
Expand Down

0 comments on commit 01ff795

Please sign in to comment.