Skip to content

Commit

Permalink
Added caching to content service. Fixes #1697
Browse files Browse the repository at this point in the history
  • Loading branch information
tidyui committed Aug 23, 2021
1 parent 9ae3f49 commit 04b4fff
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions core/Piranha/Services/ContentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ public async Task<T> GetByIdAsync<T>(Guid id, Guid? languageId = null) where T :
// First, try to get the model from cache
if (typeof(IDynamicContent).IsAssignableFrom(typeof(T)))
{
model = null; // TODO: _cache?.Get<T>($"DynamicContent_{ id.ToString() }");
model = _cache?.Get<T>($"DynamicContent_{ id.ToString() }");
}
else
{
model = null; // TODO: _cache?.Get<T>(id.ToString());
model = _cache?.Get<T>(id.ToString());
}

// If we have a model, let's initialize it
Expand Down
22 changes: 22 additions & 0 deletions test/Piranha.Tests/Services/ContentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Xunit;
using Piranha.AttributeBuilder;
using Piranha.Extend;
Expand All @@ -18,6 +20,26 @@

namespace Piranha.Tests.Services
{
[Collection("Integration tests")]
public class ContentTestsMemoryCache : ContentTests
{
public override async Task InitializeAsync()
{
_cache = new Cache.MemoryCache((IMemoryCache)_services.GetService(typeof(IMemoryCache)));
await base.InitializeAsync();
}
}

[Collection("Integration tests")]
public class ContentTestsDistributedCache : ContentTests
{
public override async Task InitializeAsync()
{
_cache = new Cache.DistributedCache((IDistributedCache)_services.GetService(typeof(IDistributedCache)));
await base.InitializeAsync();
}
}

[Collection("Integration tests")]
public class ContentTests : BaseTestsAsync
{
Expand Down

0 comments on commit 04b4fff

Please sign in to comment.