Asp.net core 静态Id-ASP.NET核心分布式缓存标记帮助器

Asp.net core 静态Id-ASP.NET核心分布式缓存标记帮助器,asp.net-core,razor-pages,distributed-caching,Asp.net Core,Razor Pages,Distributed Caching,我们将ASP.NET Core分布式缓存标记帮助程序与SQL server一起使用 <distributed-cache name="MyCacheItem1" expires-after="TimeSpan.FromDays(1)"> <p>Something that will be cached</p> @DateTime.Now.ToString() </distributed-cache>

我们将ASP.NET Core分布式缓存标记帮助程序与SQL server一起使用

<distributed-cache name="MyCacheItem1" expires-after="TimeSpan.FromDays(1)">
   <p>Something that will be cached</p>
   @DateTime.Now.ToString()
</distributed-cache>

将被缓存的东西

@DateTime.Now.ToString()
它存储如下。

问题是Id列是自动散列的。我们希望Id列中有一些有意义的字符串。 可能吗

问题是Id列是自动散列的

从源代码中,我们可以发现它似乎是一种按设计的行为:

try
{
    var serializedKey = Encoding.UTF8.GetBytes(key.GenerateKey());
    var storageKey = key.GenerateHashedKey();
    var value = await _storage.GetAsync(storageKey);

    //...

    //...

    await _storage.SetAsync(storageKey, encodeValue, options);
我们希望Id列中有一些有意义的字符串。可能吗


如果您有特定的场景/需求,需要为
Id
列提供未删除的数据,您可以参考
DistributedCacheTagHelper
DistributedCacheTagHelperService
的源代码,然后实现一个自定义标记helper。

谢谢@Fei Han。我从你的回答中得到了线索

在Id列中存储自定义字符串。按照步骤操作

  • 使用IDistributedCacheTagHelperService实现自定义类(我像HtmlDistributedCacheTagHelperService一样创建)

  • 在启动时注入此自定义类。 services.addScope()

  • 复制DistributedCacheTagHelperService()的实际源代码。并粘贴到HtmlDistributedCacheTagHelperService中

  • 我添加了自定义属性htmlcache uniqueid。

  • 在HtmlDistributedCacheTagHelperService类中,我添加了下面的代码以获取自定义属性

     if (output.Attributes != null && output.Attributes.Count > 0 &&
     string.Equals(output.Attributes[0].Name, "htmlcache-uniqueid") && 
     output.Attributes[0].Value != null)
    {
     storageKey = Convert.ToString(output.Attributes[0].Value);
    }
    
  • 最后,您可以看到存储在db中的id,如下所示。