ASP.net HttpRuntime.Cache 使用的默认序列化是什么 [英] What is the default serialization used by the ASP.net HttpRuntime.Cache

查看:26
本文介绍了ASP.net HttpRuntime.Cache 使用的默认序列化是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道是否有人确切地知道 ASP.net HttpRuntime.Cache 使用的默认序列化是什么?是二进制、XML 还是其他?

Just wondering if anyone knows definitively what the default serialization used by the ASP.net HttpRuntime.Cache is? Is it Binary, XML, something else?

我之所以这么问,是因为我遇到了一种情况,即我使用相同自定义类型的多个对象填充通用列表.自定义类型是 POCO,没有什么特别之处.它的所有属性都是公开的,带有 { get;放;},它是公共的,没有继承,没有接口.事实上,它比我们缓存的许多其他对象要简单得多,这些对象可以正常工作.我尝试将 [Serializable] 属性添加到自定义类,但没有效果.

I ask because I have a situation where I am populating a generic List with multiple objects of the same custom type. The custom type is a POCO, there is nothing special about it. All of its properties are public with { get; set; }, it is public, it has no inheritance, it has no interfaces. In fact it is much less complicated than many other objects which we are caching which work without issue. I have tried adding the [Serializable] attribute to the custom class and it has no effect.

我使用唯一键将列表添加到缓存中.该列表在插入缓存之前已被验证为已填充,列表中的对象也已被验证为已填充.但是当列表从缓存中拉回时,它是一个空列表(非空),它里面没有任何项目.这意味着列表正在被添加到缓存中并且是可检索的,但是由于某种原因缓存在序列化列表中的对象时出现问题.

I add the list to the cache with a unique key. The list has been verified as populated before it is inserted into the cache, the objects in the list have been verified as populated as well. But when the list is pulled back out of the cache it is an empty list (NOT NULL), it simply has no items in it. This means that the list is being added to the cache and is retrievable but for some reason the cache is having issues serializing the objects in the list.

我只是觉得这很奇怪,因为我有另一个更复杂的自定义对象列表(由继承、接口组成,还包含作为其他复杂对象的通用列表的属性),并且这些列表的缓存无需问题.

I just find this freaky weird since I have another list of custom objects which are much more complicated (consisting of Inheritance, Interfaces, and also containing Properties which are generic lists of other complex objects) and the caching of those lists work without issue.

工作和非工作列表都在使用缓存数据的 ASP.net 用户控件之外的 C# 类中进行管理.这两个缓存处理类调用完全相同的缓存管理器类单例实例,该实例封装了 HttpRuntime.Cache 以提供用于将对象拉入和推送到缓存中的类型化方法.

Both the working and non-working list are being managed in C# classes outside of the ASP.net user controls which consume the cached data. Both of these cache handling classes call the exact same Cache Manager class singleton instance which wraps the HttpRuntime.Cache to provide typed methods for pull and pushing objects into the cache.

任何人都知道可能导致这种情况发生的原因.我唯一能想到的就是 Document 对象的Blurb"属性可能包含 HTML,但如果 ASP.net 对缓存使用二进制序列化,我看不出这将如何做.

Anyone have any ideas what could cause this to happen. The only thing I can thin of is that the 'Blurb' property of the Document object can potentially contain HTML, but if ASP.net uses binary serialization for the cache I don't see how this would do anything.

这是课堂

public class Document
{
    public string ContentTypeId { get; set; }
    public string ContentId { get; set; }
    public bool IsCustom { get; set; }
    public Language DocLanguage { get; set; }
    public string RegularTitle { get; set; }
    public string InvertedTitle { get; set; }
    public string Blurb { get; set; }
}  

这里是语言属性中使用的子类

Here is the subclass used in the Language Property

public class Language
{
    public string Name { get; set; }
    public string Code { get; set; }
}

推荐答案

关于发生这种缓存异常的环境的更多细节.

A little more detail regarding the environment in which this caching anomaly is taking place.

我有一个 ASP.net 用户控件,它代表页面上的选项卡式容器.此控件包含用户选择的主题列表.对于用户选择的每个主题,此控件为该主题创建一个新选项卡,其中包含与该主题相关的文档.

I have an ASP.net User Control which represents a Tabbed Container on the page. This control contains a list of topics selected by the user. For each topic the user has selected this control creates a new tab for that topic containing documents related to that topic.

该控件利用 ASP.net 提供的 LoadControl 方法创建新选项卡.然后它从其列表中为新创建的选项卡控件分配一个主题.每个选项卡控件都知道如何为其指定的主题定位文档.

The control creates the new tab by utilizing the ASP.net provided LoadControl method. It then assigns the newly created Tab Control a topic from its list. Each tab control knows how to locate documents for its assigned topic.

正是在这个选项卡控件中实现了缓存.用于缓存文档列表的缓存键对于查看主题的用户的站点+主题+性别+年龄是完全唯一的.这允许所有符合该标准的用户在整个站点中缓存和检索文档列表.

It is within this tab control where the caching was implemented. The cache key used to cache the lists of documents are completely unique to the Site+Topic+Gender+Age of the user viewing the topic. This allows the document lists to be cached and retrieved across the site by all users fitting that criteria.

当文档列表被传递到缓存时,它们是由常规的旧对象引用传递的(即列表文档 = _documents).但是当从缓存中拉出时,列表是空的.

Well when the lists of documents were passed to the cache they were passed by regular old object references (i.e. List documents = _documents). but when pulled from the cache the list were empty.

尽管每个选项卡控件都是同一个用户控件的自己的实例,并且选项卡控件中使用的所有变量都是该控件的私有变量,并且应该特定于该控件.我走到了尽头,尽管各个选项卡无法覆盖彼此的私人列表,但我决定必须存在某种疯狂的参考错误,如果是这种情况,我需要停止使用缓存中的引用对象,只向缓存发送我试图存储的列表的全新副本.

Although each tab control is its own instance of the same user control and all variables used within the tab control are private to that control and should be specific to that control. I hit the end of my rope and despite the fact that there is no way the individual tabs could be over-writing each others private lists I decided there had to be some sort of crazy reference bug going on and if that was the case I needed to stop using the referenced objects in the cache and only send the cache completely new copies of the lists I was trying to store.

然后,我对 List<> 使用了以下扩展方法,并在将每个列表传递到缓存时克隆了它们.这使得传递到缓存的所有项目都是全新的对象,它们在内存中拥有自己的空间,并拥有自己对该内存的唯一引用.

I then used the following extension method for the List<> and cloned each list as they were passed to the cache. This made it so that all items passed to the cache were brand new objects with their own space in memory with their own unique references to that memory.

修复了它.缓存现在有效.这些列表现在正像添加到缓存中一样返回.我不知道为什么这会有所作为,如果有人有任何想法,我很想听听.

FIXED IT. THE CACHING NOW WORKS. The lists are now being returned exactly as they were added to the cache. I have no clue why this would make a difference, if anyone has any ideas I would love to hear them.

    /// <summary>
    /// Clones the specified list to clone.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="listToClone">The list to clone.</param>
    /// <returns></returns>
    public static IList<T> Clone<T>(this IList<T> listToClone) where T : ICloneable
    {
        return listToClone.Select(item => (T)item.Clone()).ToList();
    }

这篇关于ASP.net HttpRuntime.Cache 使用的默认序列化是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆