Skip to content

Commit b0b970e

Browse files
committed
feat: move app cache overload methods out to extensions methods
1 parent 51b766f commit b0b970e

12 files changed

+227
-175
lines changed

CacheDatabaseQueriesApiSample/Startup.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using LazyCache;
2-
using Microsoft.AspNetCore.Builder;
1+
using Microsoft.AspNetCore.Builder;
32
using Microsoft.AspNetCore.Hosting;
43
using Microsoft.EntityFrameworkCore;
54
using Microsoft.Extensions.Configuration;
@@ -30,7 +29,6 @@ public void ConfigureServices(IServiceCollection services)
3029

3130
// Register IAppCache as a singleton CachingService
3231
services.AddLazyCache();
33-
3432
}
3533

3634
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

LazyCache.AspNetCore/LazyCacheServiceCollectionExtensions.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public static IServiceCollection AddLazyCache(this IServiceCollection services)
2323
return services;
2424
}
2525

26-
public static IServiceCollection AddLazyCache(this IServiceCollection services, Func<IServiceProvider, CachingService> implmentationFactory)
26+
public static IServiceCollection AddLazyCache(this IServiceCollection services,
27+
Func<IServiceProvider, CachingService> implmentationFactory)
2728
{
2829
if (services == null) throw new ArgumentNullException(nameof(services));
2930
if (implmentationFactory == null) throw new ArgumentNullException(nameof(implmentationFactory));
@@ -37,4 +38,4 @@ public static IServiceCollection AddLazyCache(this IServiceCollection services,
3738
return services;
3839
}
3940
}
40-
}
41+
}

LazyCache.UnitTests/CachingServiceMemoryCacheProviderTests.cs

+12-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ private static CachingService BuildCache()
2424
return new CachingService(new MemoryCacheProvider());
2525
}
2626

27-
private CachingService sut;
27+
private IAppCache sut;
2828

2929
private readonly MemoryCacheEntryOptions oneHourNonRemoveableMemoryCacheEntryOptions =
3030
new MemoryCacheEntryOptions
@@ -175,6 +175,12 @@ public void AddWithSlidingThatExpiresReturnsNull()
175175
Assert.IsNull(sut.Get<string>(TestKey));
176176
}
177177

178+
[Test]
179+
public void CacheProviderIsNotNull()
180+
{
181+
sut.CacheProvider.Should().NotBeNull();
182+
}
183+
178184
[Test]
179185
public void DefaultContructorThenGetOrAddFromSecondCachingServiceHasSharedUnderlyingCache()
180186
{
@@ -626,7 +632,11 @@ public void GetOrAddWithPolicyAndThenGetObjectReturnsCorrectType()
626632
[Test]
627633
public void GetOrAddWithPolicyAndThenGetValueObjectReturnsCorrectType()
628634
{
629-
int Fetch() => 123;
635+
int Fetch()
636+
{
637+
return 123;
638+
}
639+
630640
sut.GetOrAdd(TestKey, Fetch, oneHourNonRemoveableMemoryCacheEntryOptions);
631641
var actual = sut.Get<int>(TestKey);
632642
Assert.AreEqual(123, actual);

LazyCache/AppCacheExtenions.cs

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.Caching.Memory;
4+
5+
namespace LazyCache
6+
{
7+
public static class AppCacheExtenions
8+
{
9+
public static void Add<T>(this IAppCache cache, string key, T item)
10+
{
11+
if (cache == null) throw new ArgumentNullException(nameof(cache));
12+
13+
cache.Add(key, item, cache.DefaultCachePolicy.BuildOptions());
14+
}
15+
16+
public static void Add<T>(this IAppCache cache, string key, T item, DateTimeOffset expires)
17+
{
18+
if (cache == null) throw new ArgumentNullException(nameof(cache));
19+
20+
cache.Add(key, item, new MemoryCacheEntryOptions {AbsoluteExpiration = expires});
21+
}
22+
23+
public static void Add<T>(this IAppCache cache, string key, T item, TimeSpan slidingExpiration)
24+
{
25+
if (cache == null) throw new ArgumentNullException(nameof(cache));
26+
27+
cache.Add(key, item, new MemoryCacheEntryOptions {SlidingExpiration = slidingExpiration});
28+
}
29+
30+
public static T GetOrAdd<T>(this IAppCache cache, string key, Func<T> addItemFactory)
31+
{
32+
if (cache == null) throw new ArgumentNullException(nameof(cache));
33+
34+
return cache.GetOrAdd(key, addItemFactory, cache.DefaultCachePolicy.BuildOptions());
35+
}
36+
37+
public static T GetOrAdd<T>(this IAppCache cache, string key, Func<T> addItemFactory, DateTimeOffset expires)
38+
{
39+
if (cache == null) throw new ArgumentNullException(nameof(cache));
40+
41+
return cache.GetOrAdd(key, addItemFactory, new MemoryCacheEntryOptions {AbsoluteExpiration = expires});
42+
}
43+
44+
public static T GetOrAdd<T>(this IAppCache cache, string key, Func<T> addItemFactory,
45+
TimeSpan slidingExpiration)
46+
{
47+
return cache.GetOrAdd(key, addItemFactory,
48+
new MemoryCacheEntryOptions {SlidingExpiration = slidingExpiration});
49+
}
50+
51+
public static T GetOrAdd<T>(this IAppCache cache, string key, Func<T> addItemFactory,
52+
MemoryCacheEntryOptions policy)
53+
{
54+
if (cache == null) throw new ArgumentNullException(nameof(cache));
55+
56+
return cache.GetOrAdd(key, entry =>
57+
{
58+
entry.SetOptions(policy);
59+
return addItemFactory();
60+
});
61+
}
62+
63+
public static Task<T> GetOrAddAsync<T>(this IAppCache cache, string key, Func<Task<T>> addItemFactory)
64+
{
65+
if (cache == null) throw new ArgumentNullException(nameof(cache));
66+
67+
return cache.GetOrAddAsync(key, addItemFactory, cache.DefaultCachePolicy.BuildOptions());
68+
}
69+
70+
71+
public static Task<T> GetOrAddAsync<T>(this IAppCache cache, string key, Func<Task<T>> addItemFactory,
72+
DateTimeOffset expires)
73+
{
74+
if (cache == null) throw new ArgumentNullException(nameof(cache));
75+
76+
return cache.GetOrAddAsync(key, addItemFactory, new MemoryCacheEntryOptions {AbsoluteExpiration = expires});
77+
}
78+
79+
public static Task<T> GetOrAddAsync<T>(this IAppCache cache, string key, Func<Task<T>> addItemFactory,
80+
TimeSpan slidingExpiration)
81+
{
82+
if (cache == null) throw new ArgumentNullException(nameof(cache));
83+
84+
return cache.GetOrAddAsync(key, addItemFactory,
85+
new MemoryCacheEntryOptions {SlidingExpiration = slidingExpiration});
86+
}
87+
88+
public static Task<T> GetOrAddAsync<T>(this IAppCache cache, string key, Func<Task<T>> addItemFactory,
89+
MemoryCacheEntryOptions policy)
90+
{
91+
if (cache == null) throw new ArgumentNullException(nameof(cache));
92+
93+
return cache.GetOrAddAsync(key, entry =>
94+
{
95+
entry.SetOptions(policy);
96+
return addItemFactory();
97+
});
98+
}
99+
}
100+
}

LazyCache/CacheDefaults.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using Microsoft.Extensions.Caching.Memory;
3+
4+
namespace LazyCache
5+
{
6+
public class CacheDefaults
7+
{
8+
public virtual int DefaultCacheDurationSeconds { get; set; } = 60 * 20;
9+
10+
internal MemoryCacheEntryOptions BuildOptions()
11+
{
12+
return new MemoryCacheEntryOptions
13+
{
14+
AbsoluteExpiration = DateTimeOffset.UtcNow.AddSeconds(DefaultCacheDurationSeconds)
15+
};
16+
}
17+
}
18+
}

LazyCache/CacheItemPolicy.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace LazyCache
66
{
77
[Obsolete(
88
"CacheItemPolicy was part of System.Runtime.Caching which is no longer used by LazyCache. " +
9-
"This class is a fake used to maintain backward compatibility and will be removed in a later version."+
9+
"This class is a fake used to maintain backward compatibility and will be removed in a later version." +
1010
"Change to MemoryCacheEntryOptions instead")]
1111
public class CacheItemPolicy : MemoryCacheEntryOptions
1212
{

LazyCache/CachingService.cs

+6-76
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public CachingService(Lazy<ICacheProvider> cacheProvider)
2222
this.cacheProvider = cacheProvider ?? throw new ArgumentNullException(nameof(cacheProvider));
2323
}
2424

25-
2625
public CachingService(Func<ICacheProvider> cacheProviderFactory)
2726
{
2827
if (cacheProviderFactory == null) throw new ArgumentNullException(nameof(cacheProviderFactory));
@@ -37,38 +36,20 @@ public CachingService(ICacheProvider cache) : this(() => cache)
3736
public static Lazy<ICacheProvider> DefaultCacheProvider { get; set; }
3837
= new Lazy<ICacheProvider>(() => new MemoryCacheProvider());
3938

40-
/// <summary>
41-
/// Seconds to cache objects for by default
42-
/// </summary>
43-
public virtual int DefaultCacheDurationSeconds { get; set; } = 60 * 20;
44-
4539
/// <summary>
4640
/// Seconds to cache objects for by default
4741
/// </summary>
4842
[Obsolete("DefaultCacheDuration has been replaced with DefaultCacheDurationSeconds")]
49-
5043
public virtual int DefaultCacheDuration
5144
{
52-
get => DefaultCacheDurationSeconds;
53-
set => DefaultCacheDurationSeconds = value;
54-
}
55-
56-
private DateTimeOffset DefaultExpiryDateTime => DateTimeOffset.Now.AddSeconds(DefaultCacheDurationSeconds);
57-
58-
public virtual void Add<T>(string key, T item)
59-
{
60-
Add(key, item, DefaultExpiryDateTime);
61-
}
62-
63-
public virtual void Add<T>(string key, T item, DateTimeOffset expires)
64-
{
65-
Add(key, item, new MemoryCacheEntryOptions {AbsoluteExpiration = expires});
45+
get => DefaultCachePolicy.DefaultCacheDurationSeconds;
46+
set => DefaultCachePolicy.DefaultCacheDurationSeconds = value;
6647
}
6748

68-
public virtual void Add<T>(string key, T item, TimeSpan slidingExpiration)
69-
{
70-
Add(key, item, new MemoryCacheEntryOptions {SlidingExpiration = slidingExpiration});
71-
}
49+
/// <summary>
50+
/// Policy defining how long items should be cached for unless specified
51+
/// </summary>
52+
public virtual CacheDefaults DefaultCachePolicy { get; set; } = new CacheDefaults();
7253

7354
public virtual void Add<T>(string key, T item, MemoryCacheEntryOptions policy)
7455
{
@@ -97,30 +78,6 @@ public virtual Task<T> GetAsync<T>(string key)
9778
return UnwrapAsyncLazys<T>(item);
9879
}
9980

100-
public virtual T GetOrAdd<T>(string key, Func<T> addItemFactory)
101-
{
102-
return GetOrAdd(key, addItemFactory, DefaultExpiryDateTime);
103-
}
104-
105-
public virtual T GetOrAdd<T>(string key, Func<T> addItemFactory, DateTimeOffset expires)
106-
{
107-
return GetOrAdd(key, addItemFactory, new MemoryCacheEntryOptions {AbsoluteExpiration = expires});
108-
}
109-
110-
public virtual T GetOrAdd<T>(string key, Func<T> addItemFactory, TimeSpan slidingExpiration)
111-
{
112-
return GetOrAdd(key, addItemFactory, new MemoryCacheEntryOptions {SlidingExpiration = slidingExpiration});
113-
}
114-
115-
public virtual T GetOrAdd<T>(string key, Func<T> addItemFactory, MemoryCacheEntryOptions policy)
116-
{
117-
return GetOrAdd(key, entry =>
118-
{
119-
entry.SetOptions(policy);
120-
return addItemFactory();
121-
});
122-
}
123-
12481
public virtual T GetOrAdd<T>(string key, Func<ICacheEntry, T> addItemFactory)
12582
{
12683
ValidateKey(key);
@@ -162,33 +119,6 @@ public virtual void Remove(string key)
162119

163120
public virtual ICacheProvider CacheProvider => cacheProvider.Value;
164121

165-
public virtual Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemFactory)
166-
{
167-
return GetOrAddAsync(key, addItemFactory, DefaultExpiryDateTime);
168-
}
169-
170-
public virtual Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemFactory, DateTimeOffset expires)
171-
{
172-
return GetOrAddAsync(key, addItemFactory, new MemoryCacheEntryOptions {AbsoluteExpiration = expires});
173-
}
174-
175-
public virtual Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemFactory,
176-
TimeSpan slidingExpiration)
177-
{
178-
return GetOrAddAsync(key, addItemFactory,
179-
new MemoryCacheEntryOptions {SlidingExpiration = slidingExpiration});
180-
}
181-
182-
public virtual Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemFactory,
183-
MemoryCacheEntryOptions policy)
184-
{
185-
return GetOrAddAsync(key, entry =>
186-
{
187-
entry.SetOptions(policy);
188-
return addItemFactory();
189-
});
190-
}
191-
192122
public virtual async Task<T> GetOrAddAsync<T>(string key, Func<ICacheEntry, Task<T>> addItemFactory)
193123
{
194124
ValidateKey(key);

LazyCache/IAppCache.cs

+7-13
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,21 @@ public interface IAppCache
88
{
99
ICacheProvider CacheProvider { get; }
1010

11-
void Add<T>(string key, T item);
12-
void Add<T>(string key, T item, DateTimeOffset absoluteExpiration);
13-
void Add<T>(string key, T item, TimeSpan slidingExpiration);
11+
/// <summary>
12+
/// Define the number of seconds to cache objects for by default
13+
/// </summary>
14+
CacheDefaults DefaultCachePolicy { get; }
15+
1416
void Add<T>(string key, T item, MemoryCacheEntryOptions policy);
1517

1618
T Get<T>(string key);
1719

18-
T GetOrAdd<T>(string key, Func<T> addItemFactory);
19-
T GetOrAdd<T>(string key, Func<T> addItemFactory, DateTimeOffset absoluteExpiration);
20-
T GetOrAdd<T>(string key, Func<T> addItemFactory, TimeSpan slidingExpiration);
21-
T GetOrAdd<T>(string key, Func<T> addItemFactory, MemoryCacheEntryOptions policy);
2220
T GetOrAdd<T>(string key, Func<ICacheEntry, T> addItemFactory);
2321

24-
void Remove(string key);
22+
Task<T> GetAsync<T>(string key);
2523

26-
Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemFactory);
27-
Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemFactory, MemoryCacheEntryOptions policy);
28-
Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemFactory, DateTimeOffset expires);
29-
Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemFactory, TimeSpan slidingExpiration);
3024
Task<T> GetOrAddAsync<T>(string key, Func<ICacheEntry, Task<T>> addItemFactory);
3125

32-
Task<T> GetAsync<T>(string key);
26+
void Remove(string key);
3327
}
3428
}

LazyCache/Mocks/MockCacheEntry.cs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.Extensions.Caching.Memory;
4+
using Microsoft.Extensions.Primitives;
5+
6+
namespace LazyCache.Mocks
7+
{
8+
public class MockCacheEntry : ICacheEntry
9+
{
10+
public MockCacheEntry(string key)
11+
{
12+
Key = key;
13+
}
14+
15+
public void Dispose()
16+
{
17+
}
18+
19+
public object Key { get; }
20+
public object Value { get; set; }
21+
public DateTimeOffset? AbsoluteExpiration { get; set; }
22+
public TimeSpan? AbsoluteExpirationRelativeToNow { get; set; }
23+
public TimeSpan? SlidingExpiration { get; set; }
24+
public IList<IChangeToken> ExpirationTokens { get; }
25+
public IList<PostEvictionCallbackRegistration> PostEvictionCallbacks { get; }
26+
public CacheItemPriority Priority { get; set; }
27+
public long? Size { get; set; }
28+
}
29+
}

0 commit comments

Comments
 (0)