Skip to content

Commit

Permalink
Removed simple cache and memory cache cloning. Closes #2026
Browse files Browse the repository at this point in the history
  • Loading branch information
tidyui committed Nov 18, 2023
1 parent 1866a0e commit 5c99660
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 216 deletions.
24 changes: 2 additions & 22 deletions core/Piranha/Cache/MemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@ namespace Piranha.Cache;
/// <summary>
/// Simple in memory cache.
/// </summary>
public class MemoryCache : ICache
public sealed class MemoryCache : ICache
{
/// <summary>
/// The private memory cache.
/// </summary>
private readonly IMemoryCache _cache;

/// <summary>
/// If returned objects should be cloned.
/// </summary>
private readonly bool _clone;

/// <summary>
/// Default constructor.
/// </summary>
Expand All @@ -36,17 +31,6 @@ public MemoryCache(IMemoryCache cache)
_cache = cache;
}

/// <summary>
/// Default constructor.
/// </summary>
/// <param name="cache">The currently configured cache</param>
/// <param name="clone">If returned objects should be cloned</param>
protected MemoryCache(IMemoryCache cache, bool clone)
{
_cache = cache;
_clone = clone;
}

/// <summary>
/// Gets the model with the specified key from cache.
/// </summary>
Expand All @@ -57,11 +41,7 @@ public T Get<T>(string key)
{
if (_cache.TryGetValue<T>(key, out var obj))
{
if (!_clone)
{
return obj;
}
return Utils.DeepClone(obj);
return obj;
}
return default(T);
}
Expand Down
27 changes: 0 additions & 27 deletions core/Piranha/Cache/MemoryCacheWithClone.cs

This file was deleted.

79 changes: 0 additions & 79 deletions core/Piranha/Cache/SimpleCache.cs

This file was deleted.

25 changes: 0 additions & 25 deletions core/Piranha/Cache/SimpleCacheWithClone.cs

This file was deleted.

13 changes: 3 additions & 10 deletions core/Piranha/Extensions/PiranhaMemoryCacheExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,25 @@ public static class PiranhaMemoryCacheExtensions
/// Adds the memory cache service to the service collection.
/// </summary>
/// <param name="services">The current service collection</param>
/// <param name="clone">If returned objects should be cloned</param>
/// <returns>The updated service collection</returns>
public static IServiceCollection AddPiranhaMemoryCache(this IServiceCollection services, bool clone = false)
public static IServiceCollection AddPiranhaMemoryCache(this IServiceCollection services)
{
// Check dependent services
if (!services.Any(s => s.ServiceType == typeof(IMemoryCache)))
{
throw new NotSupportedException("You need to register a IMemoryCache service in order to use Memory Cache in Piranha");
}

if (clone)
{
return services.AddSingleton<ICache, MemoryCacheWithClone>();
}
return services.AddSingleton<ICache, MemoryCache>();
}

/// <summary>
/// Uses the memory cache service in the current application.
/// </summary>
/// <param name="serviceBuilder">The current service builder</param>
/// <param name="clone">If returned objects should be cloned</param>
/// <returns>The updated service builder</returns>
public static PiranhaServiceBuilder UseMemoryCache(this PiranhaServiceBuilder serviceBuilder, bool clone = false)
public static PiranhaServiceBuilder UseMemoryCache(this PiranhaServiceBuilder serviceBuilder)
{
serviceBuilder.Services.AddPiranhaMemoryCache(clone);
serviceBuilder.Services.AddPiranhaMemoryCache();

return serviceBuilder;
}
Expand Down
34 changes: 0 additions & 34 deletions core/Piranha/Extensions/PiranhaSimpleCacheExtensions.cs

This file was deleted.

2 changes: 1 addition & 1 deletion test/Piranha.Tests/BaseTestsAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected virtual IApi CreateApi()
new ContentGroupRepository(db),
new ContentTypeRepository(db),
new LanguageRepository(db),
new Piranha.Repositories.MediaRepository(db),
new MediaRepository(db),
new PageRepository(db, serviceFactory),
new PageTypeRepository(db),
new ParamRepository(db),
Expand Down
44 changes: 26 additions & 18 deletions test/Piranha.Tests/MemCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
*
*/

using Microsoft.Extensions.Caching.Memory;
using Xunit;

namespace Piranha.Tests;

public class MemCache
public class MemCache : BaseTestsAsync
{
private readonly Piranha.Cache.SimpleCache cache;
private readonly string id1 = Guid.NewGuid().ToString();
private readonly string id2 = Guid.NewGuid().ToString();
private readonly string id3 = Guid.NewGuid().ToString();
Expand All @@ -23,44 +23,52 @@ public class MemCache
private readonly string val3 = "My third value";
private readonly string val4 = "My fourth value";

/// <summary>
/// Initializes the test class.
/// </summary>
public MemCache() {
cache = new Piranha.Cache.SimpleCache();
public override Task InitializeAsync()
{
return Task.Run(() => {
_cache = new Cache.MemoryCache((IMemoryCache)_services.GetService(typeof(IMemoryCache)));

cache.Set(id1, val1);
cache.Set(id2, val2);
_cache.Set(id1, val1);
_cache.Set(id2, val2);
});
}

public override Task DisposeAsync()
{
return Task.Run(() => {});
}

[Fact]
public void AddEntry() {
cache.Set(id3, val3);
public void AddEntry()
{
_cache.Set(id3, val3);
}

[Fact]
public void GetEntry() {
var val = cache.Get<string>(id2);
public void GetEntry()
{
var val = _cache.Get<string>(id2);

Assert.NotNull(val);
Assert.Equal(val2, val);
}

[Fact]
public void UpdateEntry() {
cache.Set(id2, val4);
_cache.Set(id2, val4);

var val = cache.Get<string>(id2);
var val = _cache.Get<string>(id2);

Assert.NotNull(val);
Assert.Equal(val4, val);
}

[Fact]
public void RemoveEntry() {
cache.Remove(id1);
public void RemoveEntry()
{
_cache.Remove(id1);

var val = cache.Get<string>(id1);
var val = _cache.Get<string>(id1);

Assert.Null(val);
}
Expand Down

0 comments on commit 5c99660

Please sign in to comment.