-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New InMemoryCacheService implementation of ICacheService in Spark
- InMemoryCacheService cache can be used in any .net standard project - CacheExpires class not longer has depenency on System.Web.Caching.Cache - Renamed DefaultCacheService to WebCacheService - Moved NullCacheService to Castle.MonoRail.Views project (it's the only place using it) - Moved some classes back to Spark project when possible - Moved markdown dependency back to spark
- Loading branch information
Showing
25 changed files
with
330 additions
and
176 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System; | ||
using System.Threading; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using NUnit.Framework; | ||
|
||
namespace Spark.Tests | ||
{ | ||
[TestFixture] | ||
public class InMemoryServiceTest | ||
{ | ||
[Test] | ||
public void TestStoreValueThenRetrieveIt() | ||
{ | ||
var service = new InMemoryCacheService(new MemoryCache(new MemoryCacheOptions())); | ||
|
||
var item = new { }; | ||
|
||
service.Store("identifier", CacheExpires.Empty, null, item); | ||
|
||
var retrieved = service.Get("identifier"); | ||
|
||
Assert.AreSame(item, retrieved); | ||
} | ||
|
||
[Test] | ||
public void TestStoreValueThenRetrieveItAfterAbsoluteExpiration() | ||
{ | ||
var service = new InMemoryCacheService(new MemoryCache(new MemoryCacheOptions())); | ||
|
||
var item = new { }; | ||
|
||
service.Store("identifier", new CacheExpires(DateTime.UtcNow.AddMilliseconds(50)), null, item); | ||
|
||
Thread.Sleep(100); | ||
|
||
var retrieved = service.Get("identifier"); | ||
|
||
Assert.IsNull(retrieved); | ||
} | ||
|
||
[Test] | ||
public void TestStoreValueThenRetrieveItWhenExpirationSlides() | ||
{ | ||
var service = new InMemoryCacheService(new MemoryCache(new MemoryCacheOptions())); | ||
|
||
var item = new { }; | ||
|
||
service.Store("identifier", new CacheExpires(TimeSpan.FromMilliseconds(75)), null, item); | ||
|
||
object retrieved; | ||
|
||
for (var i = 0; i < 3; i++) | ||
{ | ||
Thread.Sleep(50); | ||
|
||
retrieved = service.Get("identifier"); | ||
} | ||
|
||
retrieved = service.Get("identifier"); | ||
|
||
Assert.IsNotNull(retrieved); | ||
|
||
Assert.AreSame(item, retrieved); | ||
} | ||
|
||
[Test] | ||
public void TestStoreValueWithSignal() | ||
{ | ||
var service = new InMemoryCacheService(new MemoryCache(new MemoryCacheOptions())); | ||
|
||
var item = new { }; | ||
|
||
var signal = new CacheSignal(); | ||
|
||
service.Store("identifier", null, signal, item); | ||
|
||
var retrieved = service.Get("identifier"); | ||
|
||
Assert.AreSame(item, retrieved); | ||
|
||
signal.FireChanged(); | ||
|
||
retrieved = service.Get("identifier"); | ||
|
||
Assert.IsNull(retrieved); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -553,7 +553,6 @@ public void SignalWillExpireOutputCachingEntry() | |
<p>2</p> | ||
</div>")); | ||
Assert.That(calls, Is.EqualTo(2)); | ||
|
||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 7 additions & 7 deletions
14
src/Spark.Web/Caching/DefaultCacheService.cs → src/Spark.Web/WebCacheService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
|
||
namespace Spark | ||
{ | ||
/// <summary> | ||
/// Represents when a cached entry should expire. | ||
/// </summary> | ||
public class CacheExpires | ||
{ | ||
/// <summary> | ||
/// Constructor for a non expiring cached entry. | ||
/// </summary> | ||
public CacheExpires() | ||
{ | ||
Absolute = NoAbsoluteExpiration; | ||
Sliding = NoSlidingExpiration; | ||
} | ||
|
||
/// <summary> | ||
/// Constructor for a non cached entry expiring at a specified time. | ||
/// </summary> | ||
/// <param name="absolute">The time when to invalidate the cached entry.</param> | ||
public CacheExpires(DateTime absolute) | ||
{ | ||
Absolute = absolute; | ||
Sliding = NoSlidingExpiration; | ||
} | ||
|
||
/// <summary> | ||
/// Constructor for a cached entry that stays cached as long as it keeps being used. | ||
/// </summary> | ||
/// <param name="sliding">The timespan of sliding expirations.</param> | ||
public CacheExpires(TimeSpan sliding) | ||
{ | ||
Absolute = NoAbsoluteExpiration; | ||
Sliding = sliding; | ||
} | ||
|
||
/// <summary> | ||
/// Constructor for a cached entry that stays cached as long as it keeps being used. | ||
/// </summary> | ||
/// <param name="sliding">The number of seconds of sliding expirations.</param> | ||
public CacheExpires(double sliding) | ||
: this(TimeSpan.FromSeconds(sliding)) | ||
{ | ||
} | ||
|
||
public DateTime Absolute { get; set; } | ||
public TimeSpan Sliding { get; set; } | ||
|
||
public static DateTime NoAbsoluteExpiration => DateTime.MaxValue; | ||
|
||
public static TimeSpan NoSlidingExpiration => TimeSpan.Zero; | ||
|
||
/// <summary> | ||
/// Cached entry never to expire. | ||
/// </summary> | ||
public static CacheExpires Empty { get; } = new(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.