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

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring CacheTagHelper
Browse files Browse the repository at this point in the history
- Introducing a new distributed cache tag helper
- Sharing base implementation for both cache tag helper
- Preventing concurrent execution of cache tag helpers

Fixes #4147 , Fixes #3867
sebastienros committed Mar 7, 2016
1 parent 8746fae commit 9c181b5
Showing 14 changed files with 1,786 additions and 287 deletions.
Original file line number Diff line number Diff line change
@@ -9,7 +9,9 @@
using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.PlatformAbstractions;
@@ -133,6 +135,7 @@ internal static void AddRazorViewEngineServices(IServiceCollection services)

// Consumed by the Cache tag helper to cache results across the lifetime of the application.
services.TryAddSingleton<IMemoryCache, MemoryCache>();
services.TryAddSingleton<IDistributedCache, LocalCache>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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.

using System;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Microsoft.AspNetCore.Mvc.TagHelpers.Cache
{
/// <summary>
/// Implements <see cref="IDistributedCacheTagHelperFormatter"/> by serializing the content
/// in UTF8.
/// </summary>
public class DistributedCacheTagHelperFormatter : IDistributedCacheTagHelperFormatter
{

/// <inheritdoc />
public Task<byte[]> SerializeAsync(DistributedCacheTagHelperFormattingContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if (context.HtmlString == null)
{
throw new ArgumentNullException(nameof(context.HtmlString));
}

var serialized = Encoding.UTF8.GetBytes(context.HtmlString.ToString());
return Task.FromResult(serialized);
}

/// <inheritdoc />
public Task<HtmlString> DeserializeAsync(byte[] value)
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}

var content = Encoding.UTF8.GetString(value);
return Task.FromResult(new HtmlString(content));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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.

using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace Microsoft.AspNetCore.Mvc.TagHelpers.Cache
{
/// <summary>
/// Represents an object containing the information to serialize with <see cref="IDistributedCacheTagHelperFormatter" />.
/// </summary>
public class DistributedCacheTagHelperFormattingContext
{
/// <summary>
/// Gets the <see cref="TagHelperContext"/> instance.
/// </summary>
public TagHelperContext Context {get; set; }

/// <summary>
/// Gets the <see cref="HtmlString"/> instance.
/// </summary>
public HtmlString HtmlString { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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.

using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;

namespace Microsoft.AspNetCore.Mvc.TagHelpers.Cache
{
/// <summary>
/// Implements <see cref="IDistributedCacheTagHelperStorage"/> by storing the content
/// in using <see cref="IDistributedCache"/> as the store.
/// </summary>
public class DistributedCacheTagHelperStorage : IDistributedCacheTagHelperStorage
{
private readonly IDistributedCache _distributedCache;

/// <summary>
/// Creates a new <see cref="DistributedCacheTagHelperStorage"/>.
/// </summary>
/// <param name="distributedCache">The <see cref="IDistributedCache"/> to use.</param>
public DistributedCacheTagHelperStorage(IDistributedCache distributedCache)
{
_distributedCache = distributedCache;
}

/// <inheritdoc />
public Task<byte[]> GetAsync(string key)
{
return _distributedCache.GetAsync(key);
}

/// <inheritdoc />
public Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}

if (value == null)
{
throw new ArgumentNullException(nameof(value));
}

return _distributedCache.SetAsync(key, value, options);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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.

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Microsoft.AspNetCore.Mvc.TagHelpers.Cache
{
/// <summary>
/// An implementation of this interface provides a service to
/// serialize html fragments for being store by <see cref="IDistributedCacheTagHelperStorage" />
/// </summary>
public interface IDistributedCacheTagHelperFormatter
{
/// <summary>
/// Serializes some html content.
/// </summary>
/// <param name="context">The <see cref="DistributedCacheTagHelperFormattingContext" /> to serialize.</param>
/// <returns>The serialized result.</returns>
Task<byte[]> SerializeAsync(DistributedCacheTagHelperFormattingContext context);

/// <summary>
/// Deserialize some html content.
/// </summary>
/// <param name="value">The value to deserialize.</param>
/// <returns>The deserialized content, <value>null</value> otherwise.</returns>
Task<HtmlString> DeserializeAsync(byte[] value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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.

using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;

namespace Microsoft.AspNetCore.Mvc.TagHelpers.Cache
{
/// <summary>
/// An implementation of this interface provides a service to
/// cache distributed html fragments from the &lt;distributed-cache&gt;
/// tag helper.
/// </summary>
public interface IDistributedCacheTagHelperStorage
{
/// <summary>
/// Gets the content from the cache and deserializes it.
/// </summary>
/// <param name="key">The unique key to use in the cache.</param>
/// <returns>The stored value if it exists, <value>null</value> otherwise.</returns>
Task<byte[]> GetAsync(string key);

/// <summary>
/// Sets the content in the cache and serialized it.
/// </summary>
/// <param name="key">The unique key to use in the cache.</param>
/// <param name="value">The value to cache.</param>
/// <param name="options">The cache entry options.</param>
Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options);
}
}
Loading

0 comments on commit 9c181b5

Please sign in to comment.