Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Add MemoryDistributedCacheOptions and use it to configure MemoryCache used by MemoryDistributedCache #336

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static IServiceCollection AddMemoryCache(this IServiceCollection services

if (setupAction == null)
{
throw new ArgumentNullException(nameof(services));
throw new ArgumentNullException(nameof(setupAction));
}

services.AddMemoryCache();
Expand Down Expand Up @@ -80,7 +80,44 @@ public static IServiceCollection AddDistributedMemoryCache(this IServiceCollecti
throw new ArgumentNullException(nameof(services));
}

services.TryAddSingleton<IDistributedCache>(new MemoryDistributedCache(new MemoryCache(new MemoryCacheOptions())));
services.AddOptions();
services.TryAdd(ServiceDescriptor.Singleton<IDistributedCache, MemoryDistributedCache>());

return services;
}

/// <summary>
/// Adds a default implementation of <see cref="IDistributedCache"/> that stores items in memory
/// to the <see cref="IServiceCollection" />. Frameworks that require a distributed cache to work
/// can safely add this dependency as part of their dependency list to ensure that there is at least
/// one implementation available.
/// </summary>
/// <remarks>
/// <see cref="AddDistributedMemoryCache(IServiceCollection)"/> should only be used in single
/// server scenarios as this cache stores items in memory and doesn't expand across multiple machines.
/// For those scenarios it is recommended to use a proper distributed cache that can expand across
/// multiple machines.
/// </remarks>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <param name="setupAction">
/// The <see cref="Action{MemoryDistributedCacheOptions}"/> to configure the provided <see cref="MemoryDistributedCacheOptions"/>.
/// </param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection AddDistributedMemoryCache(this IServiceCollection services, Action<MemoryDistributedCacheOptions> setupAction)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AddDistributedMemoryCache vs MemoryDistributedCache we should normalize the naming here. Pick one and go with it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ok. Both names are already used publicly, you can't change it without breaking APIs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's good to keep things consistent and if we are going to break things, we should break them in 2.0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shrug yesterday maybe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well we are branching on Monday now so... it's functionally yesterday.

{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}

if (setupAction == null)
{
throw new ArgumentNullException(nameof(setupAction));
}

services.AddDistributedMemoryCache();
services.Configure(setupAction);

return services;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;

namespace Microsoft.Extensions.Caching.Distributed
{
Expand All @@ -14,14 +15,14 @@ public class MemoryDistributedCache : IDistributedCache

private readonly IMemoryCache _memCache;

public MemoryDistributedCache(IMemoryCache memoryCache)
public MemoryDistributedCache(IOptions<MemoryDistributedCacheOptions> optionsAccessor)
{
if (memoryCache == null)
if (optionsAccessor == null)
{
throw new ArgumentNullException(nameof(memoryCache));
throw new ArgumentNullException(nameof(optionsAccessor));
}

_memCache = memoryCache;
_memCache = new MemoryCache(optionsAccessor.Value);
}

public byte[] Get(string key)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.Extensions.Caching.Memory
{
public class MemoryDistributedCacheOptions : MemoryCacheOptions
{
public MemoryDistributedCacheOptions()
: base()
{
// Default size limit of 200 MB
SizeLimit = 200 * 1024 * 1024;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@
"TypeId": "public class Microsoft.Extensions.Caching.Distributed.MemoryDistributedCache : Microsoft.Extensions.Caching.Distributed.IDistributedCache",
"MemberId": "public System.Threading.Tasks.Task<System.Byte[]> GetAsync(System.String key)",
"Kind": "Removal"
},
{
"TypeId": "public class Microsoft.Extensions.Caching.Distributed.MemoryDistributedCache : Microsoft.Extensions.Caching.Distributed.IDistributedCache",
"MemberId": "public .ctor(Microsoft.Extensions.Caching.Memory.IMemoryCache memoryCache)",
"Kind": "Removal"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ namespace Microsoft.Extensions.Caching.Memory
{
public class CapacityTests
{
[Fact]
public void MemoryDistributedCacheOptionsDefaultsTo200MBSizeLimit()
{
Assert.Equal(200 * 1024 * 1024, new MemoryDistributedCacheOptions().SizeLimit);
}

[Fact]
public void NegativeSizeOnMemoryCacheEntryOptionsThrows()
{
Expand Down