Skip to content

Commit

Permalink
Make CacheKey member of base CachedQuery class abstract so that imple…
Browse files Browse the repository at this point in the history
…mentors don't forget to set the VaryBy property
  • Loading branch information
shaynevanasperen committed Nov 3, 2020
1 parent d2f30ba commit 660d877
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
2 changes: 2 additions & 0 deletions samples/Samples/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ protected override Task<User> TransformCachedResult(User[] cachedResult, Cancell

public class AlbumsByUserId : SyncTransformedCachedQuery<(IFileProvider, ILogger<AlbumsByUserId>), MemoryCacheEntryOptions, Album[], Album[]>
{
protected override void CacheKey(IKeyConfig keyConfig) => keyConfig.VaryByNothing();

protected override MemoryCacheEntryOptions CacheEntryOptions((IFileProvider, ILogger<AlbumsByUserId>) context)
{
var (fileProvider, logger) = context;
Expand Down
5 changes: 2 additions & 3 deletions src/Magneto/Core/CachedQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,10 @@ public abstract class CachedQuery<TContext, TCacheEntryOptions, TCachedResult> :
internal readonly Store State;

/// <summary>
/// <para>Configures details for constructing a cache key.</para>
/// <para>Implementors can choose not to override this method if the cache key doesn't need to vary by anything.</para>
/// Configures details for constructing a cache key.
/// </summary>
/// <param name="keyConfig">The configuration object.</param>
protected virtual void CacheKey(IKeyConfig keyConfig) { }
protected abstract void CacheKey(IKeyConfig keyConfig);

/// <summary>
/// <para>Returns options pertaining to the cache entry (such as expiration policy).</para>
Expand Down
16 changes: 14 additions & 2 deletions src/Magneto/IKeyConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.ComponentModel;
using System.Linq;

namespace Magneto
{
Expand Down Expand Up @@ -33,6 +35,7 @@ public interface IKeyConfig
/// <summary>
/// An extension class for fluent configuration of <see cref="IKeyConfig"/> instances.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class KeyConfigExtensions
{
/// <summary>
Expand All @@ -59,10 +62,19 @@ public static IKeyConfig UsePrefix(this IKeyConfig keyConfig, string value)
/// keyConfig.VaryBy($"{Foo}_{Baz.Id}")<br/>
/// </para>
/// </summary>
public static IKeyConfig VaryBy(this IKeyConfig keyConfig, params object[] value)
public static IKeyConfig VaryBy(this IKeyConfig keyConfig, object firstValue, params object[] additionalValues)
{
if (keyConfig == null) throw new ArgumentNullException(nameof(keyConfig));
keyConfig.VaryBy = new[] { firstValue }.Concat(additionalValues);
return keyConfig;
}

/// <summary>
/// A convenience method to express that the cache key should not vary by anything.
/// </summary>
public static IKeyConfig VaryByNothing(this IKeyConfig keyConfig)
{
if (keyConfig == null) throw new ArgumentNullException(nameof(keyConfig));
keyConfig.VaryBy = value;
return keyConfig;
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/Magneto.Tests/Core/OperationTests/OperationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,25 +306,29 @@ public class AsyncQueryWithoutProperties : AsyncQuery<object, object>

public class SyncCachedQueryWithoutProperties : SyncCachedQuery<object, object, object>
{
protected override void CacheKey(IKeyConfig keyConfig) => throw new NotImplementedException();
protected override object CacheEntryOptions(object context) => throw new NotImplementedException();
protected override object Query(object context) => throw new NotImplementedException();
}

public class AsyncCachedQueryWithoutProperties : AsyncCachedQuery<object, object, object>
{
protected override void CacheKey(IKeyConfig keyConfig) => throw new NotImplementedException();
protected override object CacheEntryOptions(object context) => throw new NotImplementedException();
protected override Task<object> Query(object context, CancellationToken cancellationToken) => throw new NotImplementedException();
}

public class SyncTransformedCachedQueryWithoutProperties : SyncTransformedCachedQuery<object, object, object, object>
{
protected override void CacheKey(IKeyConfig keyConfig) => throw new NotImplementedException();
protected override object CacheEntryOptions(object context) => throw new NotImplementedException();
protected override object Query(object context) => throw new NotImplementedException();
protected override object TransformCachedResult(object cachedResult) => throw new NotImplementedException();
}

public class AsyncTransformedCachedQueryWithoutProperties : AsyncTransformedCachedQuery<object, object, object, object>
{
protected override void CacheKey(IKeyConfig keyConfig) => throw new NotImplementedException();
protected override object CacheEntryOptions(object context) => throw new NotImplementedException();
protected override Task<object> Query(object context, CancellationToken cancellationToken) => throw new NotImplementedException();
protected override Task<object> TransformCachedResult(object cachedResult, CancellationToken cancellationToken) => throw new NotImplementedException();
Expand Down

0 comments on commit 660d877

Please sign in to comment.