Skip to content

Commit

Permalink
Added configuration option for setting VM controller expiration time …
Browse files Browse the repository at this point in the history
…per issue dsuryd#31.
dicky authored and dicky committed Jul 14, 2017
1 parent 09621da commit bb0ee98
Showing 5 changed files with 35 additions and 7 deletions.
7 changes: 7 additions & 0 deletions DotNetifyLib.Core/IDotNetifyConfiguration.cs
Original file line number Diff line number Diff line change
@@ -5,6 +5,11 @@ namespace DotNetify
{
public interface IDotNetifyConfiguration
{
/// <summary>
/// How long to keep a view model controller in memory after it hasn't been accessed for a while. Default to never expire.
/// </summary>
TimeSpan? VMControllerCacheExpiration { get; set; }

/// <summary>
/// Provides a factory method to create view model instances.
/// The method accepts a class type and constructor arguments, and returns an instance of that type.
@@ -20,6 +25,8 @@ public interface IDotNetifyConfiguration

public class DotNetifyConfiguration : IDotNetifyConfiguration
{
public TimeSpan? VMControllerCacheExpiration { get; set; }

public void SetFactoryMethod(Func<Type, object[], object> factoryMethod) => VMController.CreateInstance = (type, args) => factoryMethod(type, args);

public void RegisterAssembly(Assembly assembly) => VMController.RegisterAssembly(assembly);
7 changes: 7 additions & 0 deletions DotNetifyLib.Core/IVMControllerFactory.cs
Original file line number Diff line number Diff line change
@@ -14,13 +14,20 @@ You may obtain a copy of the License at
limitations under the License.
*/

using System;

namespace DotNetify
{
/// <summary>
/// Provides view model controllers.
/// </summary>
public interface IVMControllerFactory
{
/// <summary>
/// How long to keep a view model controller in memory after it hasn't been accessed for a while. Default to never expire.
/// </summary>
TimeSpan? CacheExpiration { get; set; }

/// <summary>
/// Delegate to return the response back to the client.
/// </summary>
18 changes: 13 additions & 5 deletions DotNetifyLib.Core/VMControllerFactory.cs
Original file line number Diff line number Diff line change
@@ -30,15 +30,19 @@ public class VMControllerFactory : IVMControllerFactory
private readonly IMemoryCache _controllersCache;

/// <summary>
/// How long to keep a view model controller in memory after it hasn't been accessed for a while.
/// How long to keep a view model controller in memory after it hasn't been accessed for a while. Default to never expire.
/// </summary>
public TimeSpan CacheExpiration { get; set; } = new TimeSpan(0, 20, 0);
public TimeSpan? CacheExpiration { get; set; }

/// <summary>
/// Delegate to return the response back to the client.
/// </summary>
public VMController.VMResponseDelegate ResponseDelegate { get; set; }

/// <summary>
/// Constructor.
/// </summary>
/// <param name="memoryCache">Memory cache for storing the view model controllers.</param>
public VMControllerFactory(IMemoryCache memoryCache)
{
if (memoryCache == null)
@@ -89,9 +93,13 @@ public bool Remove(string key)
/// <returns>Cache entry options.</returns>
private MemoryCacheEntryOptions GetCacheEntryOptions()
{
return new MemoryCacheEntryOptions()
.SetSlidingExpiration(CacheExpiration)
.RegisterPostEvictionCallback((key, value, reason, substate) => ((value as Lazy<VMController>).Value as IDisposable).Dispose());
var options = new MemoryCacheEntryOptions()
.RegisterPostEvictionCallback((key, value, reason, substate) => ((value as Lazy<VMController>).Value as IDisposable).Dispose());

if (CacheExpiration.HasValue)
options.SetSlidingExpiration(CacheExpiration.Value);

return options;
}
}
}
8 changes: 7 additions & 1 deletion DotNetifyLib.SignalR/Extensions/AppBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -34,7 +34,9 @@ public static IApplicationBuilder UseDotNetify(this IApplicationBuilder appBuild
// Make sure all the required services are there.
if (provider.GetService<IMemoryCache>() == null)
throw new InvalidOperationException("No service of type IMemoryCache has been registered. Please add the service by calling 'IServiceCollection.AddMemoryCache()' in the startup class.");
if (provider.GetService<IVMControllerFactory>() == null)

var vmControllerFactory = provider.GetService<IVMControllerFactory>();
if (vmControllerFactory == null)
throw new InvalidOperationException("Please call 'IServiceCollection.AddDotNetify()' inside the ConfigureServices() of the startup class.");

// Use ASP.NET Core DI to provide view model instances by default.
@@ -60,6 +62,10 @@ public static IApplicationBuilder UseDotNetify(this IApplicationBuilder appBuild
if (!dotNetifyConfig.HasAssembly)
dotNetifyConfig.RegisterEntryAssembly();

// Sets how long to keep a view model controller in memory after it hasn't been accessed for a while.
if (dotNetifyConfig.VMControllerCacheExpiration.HasValue)
vmControllerFactory.CacheExpiration = dotNetifyConfig.VMControllerCacheExpiration;

// Add middleware factories to the hub.
var middlewareFactories = provider.GetService<IList<Func<IMiddleware>>>();
_middlewareTypes.ForEach(t => middlewareFactories?.Add(() => (IMiddleware)factoryMethod(t, null)));
2 changes: 1 addition & 1 deletion WebApplication.Core.React/WebApplication.Core.React.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp1.0</TargetFramework>

0 comments on commit bb0ee98

Please sign in to comment.