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

Commit

Permalink
Add MemoryDistributedCacheOptions and use it to configure MemoryCache…
Browse files Browse the repository at this point in the history
… used by MemoryDistributedCache
  • Loading branch information
JunTaoLuo committed Jul 7, 2017
1 parent 45d42c2 commit bf34135
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
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)
{
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"
}
]

0 comments on commit bf34135

Please sign in to comment.