C# 从函数返回空值的数据缓存机会

C# 从函数返回空值的数据缓存机会,c#,asp.net,caching,C#,Asp.net,Caching,我有一个用C#编写的函数,它在检查并插入缓存后返回业务实体(make)的集合。 public static Collection<CProductMakesProps> GetCachedSmartPhoneMake(HttpContext context) { var allMake = context.Cache["SmartPhoneMake"] as Collection<CProductMakesProps>; if

我有一个用C#编写的函数,它在检查并插入缓存后返回业务实体(make)的集合。

  public static Collection<CProductMakesProps> GetCachedSmartPhoneMake(HttpContext context)
    {
        var allMake = context.Cache["SmartPhoneMake"] as Collection<CProductMakesProps>;
        if (allMake == null)
        {
            context.Cache.Insert("SmartPhoneMake", new CModelRestrictionLogic().GetTopMakes(), null,
                                 DateTime.Now.AddHours(Int32.Parse(ConfigurationManager.AppSettings["MakeCacheTime"])),
                                 Cache.NoSlidingExpiration);

            allMake = context.Cache["SmartPhoneMake"] as Collection<CProductMakesProps>;
        }
        return allMake;
    }
是否可能在
lobjprodMakeCol

谢谢


编辑

注意
new CModelRestrictionLogic()。GetTopMakes()
是一个从数据库获取记录的函数。

它将返回计数为0或更多的集合天气。

是的,如果强制转换为集合失败,则将为
allMake
分配空值,因此这在很大程度上取决于您从
新的CModelRestrictionLogic().GetTopMakes()返回的内容。

基于大多数缓存和/或字典集合都允许您检查特定密钥的存在的假设,我建议使用一种稍微简化的编写方法:

public static Collection<CProductMakesProps> GetCachedSmartPhoneMake(HttpContext context)
{
    if (!context.Cache.ContainsKey("SmartPhoneMake") || context.Cache["SmartPhoneMake"] == null)
    {
        context.Cache.Insert("SmartPhoneMake"
                             , new CModelRestrictionLogic().GetTopMakes()
                             , null
                             , DateTime.Now.AddHours(Int32.Parse(ConfigurationManager.AppSettings["MakeCacheTime"]))
                             , Cache.NoSlidingExpiration);
    }

    return context.Cache["SmartPhoneMake"] as Collection<CProductMakesProps>;
}
公共静态集合GetCachedSmartPhoneMake(HttpContext上下文)
{
if(!context.Cache.ContainsKey(“SmartPhoneMake”)| | context.Cache[“SmartPhoneMake”]==null)
{
context.Cache.Insert(“SmartPhoneMake”
,新的CModelRestrictionLogic().GetTopMakes()
无效的
,DateTime.Now.AddHours(Int32.Parse(ConfigurationManager.AppSettings[“MakeCacheTime”]))
,Cache.NoSlidingExpiration);
}
返回context.Cache[“SmartPhoneMake”]作为集合;
}

函数返回null的可能性很小-它们是

  • GetTopMakes
    函数自身返回null
  • 缓存过期时间为零(
    MakeCacheTime
    config条目的值为零)
  • 转换为集合
    失败-如果
    GetTopMakes
    返回一些不同的类型 我更喜欢下面的版本,它在上述所有情况下都不会返回null

    var allMake = context.Cache["SmartPhoneMake"] as Collection<CProductMakesProps>;
    if (allMake == null)
    {
       allMake = new CModelRestrictionLogic().GetTopMakes();
       context.Cache.Insert("SmartPhoneMake", allMake, 
          null, DateTime.UtcNow.AddHours(Int32.Parse(
          ConfigurationManager.AppSettings["MakeCacheTime"])), Cache.NoSlidingExpiration);
    }
    return allMake;
    
    var allMake=context.Cache[“SmartPhoneMake”]作为集合;
    if(allMake==null)
    {
    allMake=new CModelRestrictionLogic().GetTopMakes();
    context.Cache.Insert(“SmartPhoneMake”,allMake,
    null,DateTime.UtcNow.AddHours(Int32.Parse(
    ConfigurationManager.AppSettings[“MakeCacheTime”]),Cache.NoSlidingExpiration);
    }
    返回allMake;
    

    还请注意,使用
    DateTime.UtcNow
    可以避免任何意外情况,如日光节约等。

    如果强制转换为
    集合失败,则会为allMake true分配空值,但之后空值检查将为上下文分配值。缓存我说得对吗?@krshekhar您有两个不同的强制转换实例
    作为集合
    在您的代码中,如果应用它所针对的类型不能以这种方式强制转换,则此操作将(静默)失败并返回null。你的问题是,我是否可能在lobjprodMakeCol?中得到空值,答案是肯定的,这是可能的,因为你拥有代码的方式。谢谢你的建议和回答。但我想知道您的方法和@slugster方法有什么区别吗?@krshekhar,正如我所说的,即使缓存过期时间为零(表示不需要缓存),上面的代码也不会返回null。另一方面,它消除了缓存未命中场景中不必要的缓存查找,但这在方案中并不重要!此外,您可以进一步重新考虑上述代码,以从静态构造函数中的配置读取缓存过期时间,但再次。。。
    var allMake = context.Cache["SmartPhoneMake"] as Collection<CProductMakesProps>;
    if (allMake == null)
    {
       allMake = new CModelRestrictionLogic().GetTopMakes();
       context.Cache.Insert("SmartPhoneMake", allMake, 
          null, DateTime.UtcNow.AddHours(Int32.Parse(
          ConfigurationManager.AppSettings["MakeCacheTime"])), Cache.NoSlidingExpiration);
    }
    return allMake;