Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions src/ZiggyCreatures.FusionCache/FusionCacheBuilderExtMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,43 @@ public static IFusionCacheBuilder WithOptions(this IFusionCacheBuilder builder,
if (action is null)
throw new ArgumentNullException(nameof(action));

builder.SetupOptionsAction += action;

return builder;
}

/// <summary>
/// Set the cache key prefix to use.
/// <br/><br/>
/// <strong>EXAMPLE</strong>: if the CacheKeyPrefix specified is "MyCache:", a later call to cache.GetOrDefault("Product/123") will actually work on the cache key "MyCache:Product/123".
/// <br/><br/>
/// <strong>DOCS:</strong> <see href="https://github.com/ZiggyCreatures/FusionCache/blob/main/docs/DependencyInjection.md"/>
/// </summary>
/// <param name="builder">The <see cref="IFusionCacheBuilder" /> to act upon.</param>
/// <param name="cacheKeyPrefix">The cache key prefix to use.</param>
/// <returns>The <see cref="IFusionCacheBuilder"/> so that additional calls can be chained.</returns>
public static IFusionCacheBuilder WithCacheKeyPrefix(this IFusionCacheBuilder builder, string? cacheKeyPrefix)
builder.SetupOptionsAction += (s, o) => action(o);

return builder;
}

/// <summary>
/// Specify a custom logic to further configure the <see cref="FusionCacheOptions"/> instance to be used.
/// <br/><br/>
/// <strong>DOCS:</strong> <see href="https://github.com/ZiggyCreatures/FusionCache/blob/main/docs/DependencyInjection.md"/>
/// </summary>
/// <param name="builder">The <see cref="IFusionCacheBuilder" /> to act upon.</param>
/// <param name="action">The custom action that configure the <see cref="FusionCacheOptions"/> object.</param>
/// <returns>The <see cref="IFusionCacheBuilder"/> so that additional calls can be chained.</returns>
public static IFusionCacheBuilder WithOptions(this IFusionCacheBuilder builder, Action<IServiceProvider, FusionCacheOptions> action)
{
if (builder is null)
throw new ArgumentNullException(nameof(builder));

if (action is null)
throw new ArgumentNullException(nameof(action));

builder.SetupOptionsAction += action;

return builder;
}

/// <summary>
/// Set the cache key prefix to use.
/// <br/><br/>
/// <strong>EXAMPLE</strong>: if the CacheKeyPrefix specified is "MyCache:", a later call to cache.GetOrDefault("Product/123") will actually work on the cache key "MyCache:Product/123".
/// <br/><br/>
/// <strong>DOCS:</strong> <see href="https://github.com/ZiggyCreatures/FusionCache/blob/main/docs/DependencyInjection.md"/>
/// </summary>
/// <param name="builder">The <see cref="IFusionCacheBuilder" /> to act upon.</param>
/// <param name="cacheKeyPrefix">The cache key prefix to use.</param>
/// <returns>The <see cref="IFusionCacheBuilder"/> so that additional calls can be chained.</returns>
public static IFusionCacheBuilder WithCacheKeyPrefix(this IFusionCacheBuilder builder, string? cacheKeyPrefix)
{
if (builder is null)
throw new ArgumentNullException(nameof(builder));
Expand Down
2 changes: 1 addition & 1 deletion src/ZiggyCreatures.FusionCache/IFusionCacheBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public interface IFusionCacheBuilder
/// <summary>
/// A custom setup logic for the <see cref="FusionCacheOptions"/> object, to allow for fine-grained customization.
/// </summary>
Action<FusionCacheOptions>? SetupOptionsAction { get; set; }
Action<IServiceProvider, FusionCacheOptions>? SetupOptionsAction { get; set; }

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public FusionCacheBuilder(string cacheName, IServiceCollection services)
public bool UseCacheKeyPrefix { get; set; }
public bool AddCacheKeyPrefixSeparator { get; set; }
public string? CacheKeyPrefix { get; set; }
public Action<FusionCacheOptions>? SetupOptionsAction { get; set; }
public Action<IServiceProvider, FusionCacheOptions>? SetupOptionsAction { get; set; }

public FusionCacheEntryOptions? DefaultEntryOptions { get; set; }
public Action<FusionCacheEntryOptions>? SetupDefaultEntryOptionsAction { get; set; }
Expand Down Expand Up @@ -143,7 +143,7 @@ public IFusionCache Build(IServiceProvider serviceProvider)
}
}

SetupOptionsAction?.Invoke(options);
SetupOptionsAction?.Invoke(serviceProvider, options);

// CACHE KEY PREFIX
if (UseCacheKeyPrefix)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,15 @@ public IFusionCacheDistributedLocker DistributedLocker
}

private string GetLockName(string key)
{
return $"{key}{_options.InternalStrings.DistributedLockerLockNameSuffix}";
{
var lockName = $"{key}{_options.InternalStrings.DistributedLockerLockNameSuffix}";

if (_options.CacheKeyPrefix is not null)
{
lockName = _options.CacheKeyPrefix + lockName;
}

return lockName;
}

//private void UpdateLastError(string operationId, string key)
Expand Down
24 changes: 24 additions & 0 deletions tests/ZiggyCreatures.FusionCache.Tests/DependencyInjectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,30 @@ public void CannotSpecifyCacheNameOfNamedCacheViaOptions()
});
}

[Fact]
public void CanSpecifyCacheKeyPrefixOfNamedCacheViaOptionsAndDI()
{
var services = new ServiceCollection();

services.AddKeyedSingleton<String>("cachePrefix", "foo_prefix");

services.AddFusionCache("foo")
.WithOptions((sp, opt) =>
{
opt.CacheKeyPrefix = sp.GetRequiredKeyedService<String>("cachePrefix");
});

using var serviceProvider = services.BuildServiceProvider();

var cacheProvider = serviceProvider.GetRequiredService<IFusionCacheProvider>();

var cache = cacheProvider.GetCache("foo");

Assert.NotNull(cache);
Assert.Equal("foo", cache.CacheName);
Assert.Equal("foo_prefix", cache.GetOptions().CacheKeyPrefix);
}

[Fact]
public void CanDirectlyAddANamedCacheInstance()
{
Expand Down
Loading