Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduced MemoryCacheOptions for ICachingConfiguration #6543

Merged
merged 1 commit into from Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions doc/WebSite/Caching.md
Expand Up @@ -106,6 +106,17 @@ first request). Configuration is not restricted to
DefaultSlidingExpireTime only, since the cache object is an **ICacheOptions**, you can
use it's properties to freely configure and initialize it.

#### MemoryCache Configuration

You can configure memory cache options using ASP.NET Core's options pattern as shown below;

```csharp
Configuration.Caching.MemoryCacheOptions = new MemoryCacheOptions
{
SizeLimit = 2048
};
```

### Entity Caching

While ASP.NET Boilerplate's cache system is for general purposes, there is an
Expand Down
4 changes: 4 additions & 0 deletions src/Abp/Runtime/Caching/Configuration/CachingConfiguration.cs
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using Abp.Configuration.Startup;
using Microsoft.Extensions.Caching.Memory;

namespace Abp.Runtime.Caching.Configuration
{
Expand All @@ -13,6 +14,9 @@ public IReadOnlyList<ICacheConfigurator> Configurators
{
get { return _configurators.ToImmutableList(); }
}

public MemoryCacheOptions MemoryCacheOptions { get; set; }

private readonly List<ICacheConfigurator> _configurators;

public CachingConfiguration(IAbpStartupConfiguration abpConfiguration)
Expand Down
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using Abp.Configuration.Startup;
using Microsoft.Extensions.Caching.Memory;

namespace Abp.Runtime.Caching.Configuration
{
Expand All @@ -19,6 +20,11 @@ public interface ICachingConfiguration
/// </summary>
IReadOnlyList<ICacheConfigurator> Configurators { get; }

/// <summary>
/// Options for memory cache
/// </summary>
MemoryCacheOptions MemoryCacheOptions { get; set; }

/// <summary>
/// Used to configure all caches.
/// </summary>
Expand Down
15 changes: 11 additions & 4 deletions src/Abp/Runtime/Caching/Memory/AbpMemoryCache.cs
Expand Up @@ -11,15 +11,20 @@ namespace Abp.Runtime.Caching.Memory
public class AbpMemoryCache : CacheBase
{
private MemoryCache _memoryCache;

private readonly MemoryCacheOptions _memoryCacheOptions;

/// <summary>
/// Constructor.
/// </summary>
/// <param name="name">Unique name of the cache</param>
public AbpMemoryCache(string name)
/// <param name="memoryCacheOptions">MemoryCacheOptions</param>
public AbpMemoryCache(string name, MemoryCacheOptions memoryCacheOptions = null)
: base(name)
{
_memoryCache = new MemoryCache(new OptionsWrapper<MemoryCacheOptions>(new MemoryCacheOptions()));
_memoryCacheOptions = memoryCacheOptions;
_memoryCache = new MemoryCache(new OptionsWrapper<MemoryCacheOptions>(
memoryCacheOptions ?? new MemoryCacheOptions()
));
}

public override bool TryGetValue(string key, out object value)
Expand Down Expand Up @@ -64,7 +69,9 @@ public override void Remove(string key)
public override void Clear()
{
_memoryCache.Dispose();
_memoryCache = new MemoryCache(new OptionsWrapper<MemoryCacheOptions>(new MemoryCacheOptions()));
_memoryCache = new MemoryCache(new OptionsWrapper<MemoryCacheOptions>(
_memoryCacheOptions ?? new MemoryCacheOptions()
));
}

public override void Dispose()
Expand Down
10 changes: 7 additions & 3 deletions src/Abp/Runtime/Caching/Memory/AbpMemoryCacheManager.cs
@@ -1,5 +1,9 @@
using Abp.Runtime.Caching.Configuration;
using System.Linq;
using Abp.Dependency;
using Abp.Runtime.Caching.Configuration;
using Castle.Core.Logging;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;

namespace Abp.Runtime.Caching.Memory
{
Expand All @@ -9,7 +13,7 @@ namespace Abp.Runtime.Caching.Memory
public class AbpMemoryCacheManager : CacheManagerBase<ICache>, ICacheManager
{
public ILogger Logger { get; set; }

/// <summary>
/// Constructor.
/// </summary>
Expand All @@ -21,7 +25,7 @@ public AbpMemoryCacheManager(ICachingConfiguration configuration)

protected override ICache CreateCacheImplementation(string name)
{
return new AbpMemoryCache(name)
return new AbpMemoryCache(name, Configuration?.AbpConfiguration?.Caching?.MemoryCacheOptions)
{
Logger = Logger
};
Expand Down
@@ -0,0 +1,43 @@
using System.Linq;
using Abp.Dependency;
using Abp.Runtime.Caching;
using Abp.Runtime.Caching.Memory;
using AutoMapper.Internal;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using Shouldly;
using Xunit;

namespace AbpAspNetCoreDemo.IntegrationTests.Tests
{
public class MemoryCacheOptions_Test: IClassFixture<WebApplicationFactory<Startup>>
{
private readonly WebApplicationFactory<Startup> _applicationFactory;

public MemoryCacheOptions_Test()
{
_applicationFactory = new WebApplicationFactory<Startup>();
_applicationFactory.CreateClient();
}

[Fact]
public void MemoryCacheOption_Size_Test()
{
var memoryCacheManager = _applicationFactory.Services.GetService(typeof(ICacheManager)) as ICacheManager;

memoryCacheManager.ShouldNotBeNull();
memoryCacheManager.GetType().ShouldBe(typeof(AbpMemoryCacheManager));

var memoryCache= memoryCacheManager.GetCache("Test");

memoryCache.ShouldNotBeNull();
memoryCache.GetType().ShouldBe(typeof(AbpMemoryCache));

var memberPath = ReflectionHelper.GetMemberPath(typeof(AbpMemoryCache), "_memoryCacheOptions").First();
var memoryCacheOptions = memberPath.GetMemberValue(memoryCache) as IOptions<MemoryCacheOptions>;
memoryCacheOptions.ShouldNotBeNull();
memoryCacheOptions.Value.SizeLimit.ShouldBe(2048);
}
}
}
Expand Up @@ -17,6 +17,7 @@
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.Extensions.Caching.Memory;

namespace AbpAspNetCoreDemo
{
Expand Down Expand Up @@ -50,6 +51,11 @@ public override void PreInitialize()
endpoints.MapRazorPages();
});

Configuration.Caching.MemoryCacheOptions = new MemoryCacheOptions
{
SizeLimit = 2048
};

ConfigurationAction.Value?.Invoke(Configuration);
}

Expand Down
1 change: 1 addition & 0 deletions test/aspnet-core-demo/AbpAspNetCoreDemo/Startup.cs
Expand Up @@ -19,6 +19,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.OData;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand Down