Skip to content

Commit

Permalink
feat: move app cache overload methods out to extensions methods
Browse files Browse the repository at this point in the history
  • Loading branch information
alastairtree committed Mar 4, 2018
1 parent 51b766f commit b0b970e
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 175 deletions.
4 changes: 1 addition & 3 deletions CacheDatabaseQueriesApiSample/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using LazyCache;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -30,7 +29,6 @@ public void ConfigureServices(IServiceCollection services)

// Register IAppCache as a singleton CachingService
services.AddLazyCache();

}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
5 changes: 3 additions & 2 deletions LazyCache.AspNetCore/LazyCacheServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public static IServiceCollection AddLazyCache(this IServiceCollection services)
return services;
}

public static IServiceCollection AddLazyCache(this IServiceCollection services, Func<IServiceProvider, CachingService> implmentationFactory)
public static IServiceCollection AddLazyCache(this IServiceCollection services,
Func<IServiceProvider, CachingService> implmentationFactory)
{
if (services == null) throw new ArgumentNullException(nameof(services));
if (implmentationFactory == null) throw new ArgumentNullException(nameof(implmentationFactory));
Expand All @@ -37,4 +38,4 @@ public static IServiceCollection AddLazyCache(this IServiceCollection services,
return services;
}
}
}
}
14 changes: 12 additions & 2 deletions LazyCache.UnitTests/CachingServiceMemoryCacheProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private static CachingService BuildCache()
return new CachingService(new MemoryCacheProvider());
}

private CachingService sut;
private IAppCache sut;

private readonly MemoryCacheEntryOptions oneHourNonRemoveableMemoryCacheEntryOptions =
new MemoryCacheEntryOptions
Expand Down Expand Up @@ -175,6 +175,12 @@ public void AddWithSlidingThatExpiresReturnsNull()
Assert.IsNull(sut.Get<string>(TestKey));
}

[Test]
public void CacheProviderIsNotNull()
{
sut.CacheProvider.Should().NotBeNull();
}

[Test]
public void DefaultContructorThenGetOrAddFromSecondCachingServiceHasSharedUnderlyingCache()
{
Expand Down Expand Up @@ -626,7 +632,11 @@ public void GetOrAddWithPolicyAndThenGetObjectReturnsCorrectType()
[Test]
public void GetOrAddWithPolicyAndThenGetValueObjectReturnsCorrectType()
{
int Fetch() => 123;
int Fetch()
{
return 123;
}

sut.GetOrAdd(TestKey, Fetch, oneHourNonRemoveableMemoryCacheEntryOptions);
var actual = sut.Get<int>(TestKey);
Assert.AreEqual(123, actual);
Expand Down
100 changes: 100 additions & 0 deletions LazyCache/AppCacheExtenions.cs
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();
});
}
}
}
18 changes: 18 additions & 0 deletions LazyCache/CacheDefaults.cs
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)
};
}
}
}
2 changes: 1 addition & 1 deletion LazyCache/CacheItemPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace LazyCache
{
[Obsolete(
"CacheItemPolicy was part of System.Runtime.Caching which is no longer used by LazyCache. " +
"This class is a fake used to maintain backward compatibility and will be removed in a later version."+
"This class is a fake used to maintain backward compatibility and will be removed in a later version." +
"Change to MemoryCacheEntryOptions instead")]
public class CacheItemPolicy : MemoryCacheEntryOptions
{
Expand Down
82 changes: 6 additions & 76 deletions LazyCache/CachingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public CachingService(Lazy<ICacheProvider> cacheProvider)
this.cacheProvider = cacheProvider ?? throw new ArgumentNullException(nameof(cacheProvider));
}


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

/// <summary>
/// Seconds to cache objects for by default
/// </summary>
public virtual int DefaultCacheDurationSeconds { get; set; } = 60 * 20;

/// <summary>
/// Seconds to cache objects for by default
/// </summary>
[Obsolete("DefaultCacheDuration has been replaced with DefaultCacheDurationSeconds")]

public virtual int DefaultCacheDuration
{
get => DefaultCacheDurationSeconds;
set => DefaultCacheDurationSeconds = value;
}

private DateTimeOffset DefaultExpiryDateTime => DateTimeOffset.Now.AddSeconds(DefaultCacheDurationSeconds);

public virtual void Add<T>(string key, T item)
{
Add(key, item, DefaultExpiryDateTime);
}

public virtual void Add<T>(string key, T item, DateTimeOffset expires)
{
Add(key, item, new MemoryCacheEntryOptions {AbsoluteExpiration = expires});
get => DefaultCachePolicy.DefaultCacheDurationSeconds;
set => DefaultCachePolicy.DefaultCacheDurationSeconds = value;
}

public virtual void Add<T>(string key, T item, TimeSpan slidingExpiration)
{
Add(key, item, new MemoryCacheEntryOptions {SlidingExpiration = slidingExpiration});
}
/// <summary>
/// Policy defining how long items should be cached for unless specified
/// </summary>
public virtual CacheDefaults DefaultCachePolicy { get; set; } = new CacheDefaults();

public virtual void Add<T>(string key, T item, MemoryCacheEntryOptions policy)
{
Expand Down Expand Up @@ -97,30 +78,6 @@ public virtual Task<T> GetAsync<T>(string key)
return UnwrapAsyncLazys<T>(item);
}

public virtual T GetOrAdd<T>(string key, Func<T> addItemFactory)
{
return GetOrAdd(key, addItemFactory, DefaultExpiryDateTime);
}

public virtual T GetOrAdd<T>(string key, Func<T> addItemFactory, DateTimeOffset expires)
{
return GetOrAdd(key, addItemFactory, new MemoryCacheEntryOptions {AbsoluteExpiration = expires});
}

public virtual T GetOrAdd<T>(string key, Func<T> addItemFactory, TimeSpan slidingExpiration)
{
return GetOrAdd(key, addItemFactory, new MemoryCacheEntryOptions {SlidingExpiration = slidingExpiration});
}

public virtual T GetOrAdd<T>(string key, Func<T> addItemFactory, MemoryCacheEntryOptions policy)
{
return GetOrAdd(key, entry =>
{
entry.SetOptions(policy);
return addItemFactory();
});
}

public virtual T GetOrAdd<T>(string key, Func<ICacheEntry, T> addItemFactory)
{
ValidateKey(key);
Expand Down Expand Up @@ -162,33 +119,6 @@ public virtual void Remove(string key)

public virtual ICacheProvider CacheProvider => cacheProvider.Value;

public virtual Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemFactory)
{
return GetOrAddAsync(key, addItemFactory, DefaultExpiryDateTime);
}

public virtual Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemFactory, DateTimeOffset expires)
{
return GetOrAddAsync(key, addItemFactory, new MemoryCacheEntryOptions {AbsoluteExpiration = expires});
}

public virtual Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemFactory,
TimeSpan slidingExpiration)
{
return GetOrAddAsync(key, addItemFactory,
new MemoryCacheEntryOptions {SlidingExpiration = slidingExpiration});
}

public virtual Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemFactory,
MemoryCacheEntryOptions policy)
{
return GetOrAddAsync(key, entry =>
{
entry.SetOptions(policy);
return addItemFactory();
});
}

public virtual async Task<T> GetOrAddAsync<T>(string key, Func<ICacheEntry, Task<T>> addItemFactory)
{
ValidateKey(key);
Expand Down
20 changes: 7 additions & 13 deletions LazyCache/IAppCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,21 @@ public interface IAppCache
{
ICacheProvider CacheProvider { get; }

void Add<T>(string key, T item);
void Add<T>(string key, T item, DateTimeOffset absoluteExpiration);
void Add<T>(string key, T item, TimeSpan slidingExpiration);
/// <summary>
/// Define the number of seconds to cache objects for by default
/// </summary>
CacheDefaults DefaultCachePolicy { get; }

void Add<T>(string key, T item, MemoryCacheEntryOptions policy);

T Get<T>(string key);

T GetOrAdd<T>(string key, Func<T> addItemFactory);
T GetOrAdd<T>(string key, Func<T> addItemFactory, DateTimeOffset absoluteExpiration);
T GetOrAdd<T>(string key, Func<T> addItemFactory, TimeSpan slidingExpiration);
T GetOrAdd<T>(string key, Func<T> addItemFactory, MemoryCacheEntryOptions policy);
T GetOrAdd<T>(string key, Func<ICacheEntry, T> addItemFactory);

void Remove(string key);
Task<T> GetAsync<T>(string key);

Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemFactory);
Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemFactory, MemoryCacheEntryOptions policy);
Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemFactory, DateTimeOffset expires);
Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemFactory, TimeSpan slidingExpiration);
Task<T> GetOrAddAsync<T>(string key, Func<ICacheEntry, Task<T>> addItemFactory);

Task<T> GetAsync<T>(string key);
void Remove(string key);
}
}
29 changes: 29 additions & 0 deletions LazyCache/Mocks/MockCacheEntry.cs
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; }
}
}
Loading

0 comments on commit b0b970e

Please sign in to comment.