-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move app cache overload methods out to extensions methods
- Loading branch information
1 parent
51b766f
commit b0b970e
Showing
12 changed files
with
227 additions
and
175 deletions.
There are no files selected for viewing
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
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,100 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Caching.Memory; | ||
|
||
namespace LazyCache | ||
{ | ||
public static class AppCacheExtenions | ||
{ | ||
public static void Add<T>(this IAppCache cache, string key, T item) | ||
{ | ||
if (cache == null) throw new ArgumentNullException(nameof(cache)); | ||
|
||
cache.Add(key, item, cache.DefaultCachePolicy.BuildOptions()); | ||
} | ||
|
||
public static void Add<T>(this IAppCache cache, string key, T item, DateTimeOffset expires) | ||
{ | ||
if (cache == null) throw new ArgumentNullException(nameof(cache)); | ||
|
||
cache.Add(key, item, new MemoryCacheEntryOptions {AbsoluteExpiration = expires}); | ||
} | ||
|
||
public static void Add<T>(this IAppCache cache, string key, T item, TimeSpan slidingExpiration) | ||
{ | ||
if (cache == null) throw new ArgumentNullException(nameof(cache)); | ||
|
||
cache.Add(key, item, new MemoryCacheEntryOptions {SlidingExpiration = slidingExpiration}); | ||
} | ||
|
||
public static T GetOrAdd<T>(this IAppCache cache, string key, Func<T> addItemFactory) | ||
{ | ||
if (cache == null) throw new ArgumentNullException(nameof(cache)); | ||
|
||
return cache.GetOrAdd(key, addItemFactory, cache.DefaultCachePolicy.BuildOptions()); | ||
} | ||
|
||
public static T GetOrAdd<T>(this IAppCache cache, string key, Func<T> addItemFactory, DateTimeOffset expires) | ||
{ | ||
if (cache == null) throw new ArgumentNullException(nameof(cache)); | ||
|
||
return cache.GetOrAdd(key, addItemFactory, new MemoryCacheEntryOptions {AbsoluteExpiration = expires}); | ||
} | ||
|
||
public static T GetOrAdd<T>(this IAppCache cache, string key, Func<T> addItemFactory, | ||
TimeSpan slidingExpiration) | ||
{ | ||
return cache.GetOrAdd(key, addItemFactory, | ||
new MemoryCacheEntryOptions {SlidingExpiration = slidingExpiration}); | ||
} | ||
|
||
public static T GetOrAdd<T>(this IAppCache cache, string key, Func<T> addItemFactory, | ||
MemoryCacheEntryOptions policy) | ||
{ | ||
if (cache == null) throw new ArgumentNullException(nameof(cache)); | ||
|
||
return cache.GetOrAdd(key, entry => | ||
{ | ||
entry.SetOptions(policy); | ||
return addItemFactory(); | ||
}); | ||
} | ||
|
||
public static Task<T> GetOrAddAsync<T>(this IAppCache cache, string key, Func<Task<T>> addItemFactory) | ||
{ | ||
if (cache == null) throw new ArgumentNullException(nameof(cache)); | ||
|
||
return cache.GetOrAddAsync(key, addItemFactory, cache.DefaultCachePolicy.BuildOptions()); | ||
} | ||
|
||
|
||
public static Task<T> GetOrAddAsync<T>(this IAppCache cache, string key, Func<Task<T>> addItemFactory, | ||
DateTimeOffset expires) | ||
{ | ||
if (cache == null) throw new ArgumentNullException(nameof(cache)); | ||
|
||
return cache.GetOrAddAsync(key, addItemFactory, new MemoryCacheEntryOptions {AbsoluteExpiration = expires}); | ||
} | ||
|
||
public static Task<T> GetOrAddAsync<T>(this IAppCache cache, string key, Func<Task<T>> addItemFactory, | ||
TimeSpan slidingExpiration) | ||
{ | ||
if (cache == null) throw new ArgumentNullException(nameof(cache)); | ||
|
||
return cache.GetOrAddAsync(key, addItemFactory, | ||
new MemoryCacheEntryOptions {SlidingExpiration = slidingExpiration}); | ||
} | ||
|
||
public static Task<T> GetOrAddAsync<T>(this IAppCache cache, string key, Func<Task<T>> addItemFactory, | ||
MemoryCacheEntryOptions policy) | ||
{ | ||
if (cache == null) throw new ArgumentNullException(nameof(cache)); | ||
|
||
return cache.GetOrAddAsync(key, entry => | ||
{ | ||
entry.SetOptions(policy); | ||
return addItemFactory(); | ||
}); | ||
} | ||
} | ||
} |
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,18 @@ | ||
using System; | ||
using Microsoft.Extensions.Caching.Memory; | ||
|
||
namespace LazyCache | ||
{ | ||
public class CacheDefaults | ||
{ | ||
public virtual int DefaultCacheDurationSeconds { get; set; } = 60 * 20; | ||
|
||
internal MemoryCacheEntryOptions BuildOptions() | ||
{ | ||
return new MemoryCacheEntryOptions | ||
{ | ||
AbsoluteExpiration = DateTimeOffset.UtcNow.AddSeconds(DefaultCacheDurationSeconds) | ||
}; | ||
} | ||
} | ||
} |
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
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,29 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace LazyCache.Mocks | ||
{ | ||
public class MockCacheEntry : ICacheEntry | ||
{ | ||
public MockCacheEntry(string key) | ||
{ | ||
Key = key; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
} | ||
|
||
public object Key { get; } | ||
public object Value { get; set; } | ||
public DateTimeOffset? AbsoluteExpiration { get; set; } | ||
public TimeSpan? AbsoluteExpirationRelativeToNow { get; set; } | ||
public TimeSpan? SlidingExpiration { get; set; } | ||
public IList<IChangeToken> ExpirationTokens { get; } | ||
public IList<PostEvictionCallbackRegistration> PostEvictionCallbacks { get; } | ||
public CacheItemPriority Priority { get; set; } | ||
public long? Size { get; set; } | ||
} | ||
} |
Oops, something went wrong.