This repository was archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
1 parent
8746fae
commit 9c181b5
Showing
14 changed files
with
1,786 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/Microsoft.AspNetCore.Mvc.TagHelpers/Cache/DistributedCacheTagHelperFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Microsoft.AspNetCore.Mvc.TagHelpers/Cache/DistributedCacheTagHelperFormattingContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/Microsoft.AspNetCore.Mvc.TagHelpers/Cache/DistributedCacheTagHelperStorage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Microsoft.AspNetCore.Mvc.TagHelpers/Cache/IDistributedCacheTagHelperFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Microsoft.AspNetCore.Mvc.TagHelpers/Cache/IDistributedCacheTagHelperStorage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <distributed-cache> | ||
/// 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); | ||
} | ||
} |
Oops, something went wrong.