Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.net缓存-正确使用_C#_Asp.net_Performance_Caching - Fatal编程技术网

C# ASP.net缓存-正确使用

C# ASP.net缓存-正确使用,c#,asp.net,performance,caching,C#,Asp.net,Performance,Caching,我正在创建一个web应用程序,但缓存有问题 我的应用程序中有大量数据,我希望在每次需要这些信息时都尝试从sql数据库调用这些数据,而不是调用它们 我已尝试以以下方式使用缓存: public static List<DAL.EntityClasses.AccountsEntity> Accounts { get { if (HttpContext.Current.Cache["Account"] == nu

我正在创建一个web应用程序,但缓存有问题

我的应用程序中有大量数据,我希望在每次需要这些信息时都尝试从sql数据库调用这些数据,而不是调用它们

我已尝试以以下方式使用缓存:

        public static List<DAL.EntityClasses.AccountsEntity> Accounts
    {
        get
        {
            if (HttpContext.Current.Cache["Account"] == null)
            {
                HttpContext.Current.Cache.Insert("Account", LoadAccounts(), null, DateTime.Now.AddHours(4), System.Web.Caching.Cache.NoSlidingExpiration);
            }

            return (List<DAL.EntityClasses.AccountsEntity>)HttpContext.Current.Cache["Account"];
        }
    }
公共静态列表帐户
{
得到
{
if(HttpContext.Current.Cache[“Account”]==null)
{
HttpContext.Current.Cache.Insert(“Account”,LoadAccounts(),null,DateTime.Now.AddHours(4),System.Web.Caching.Cache.NoSlidingExpiration);
}
返回(列表)HttpContext.Current.Cache[“Account”];
}
}
问题在于,在向缓存添加项时,已缓存的项似乎会被删除

因此,大多数调用都是调用数据库来获取缓存的数据

我哪里出错了

谢谢

这对于缓存来说是正常的-当缓存填满容量时,最少使用的项目会被推出

这对于缓存来说是正常的-当缓存填满容量时,最少使用的项会被推出

更多数据。

仅供参考: 您对Accounts属性的实现存在一个问题,该问题与原始问题无关,但可能会在将来导致问题:

在这条线之间会发生什么

if(HttpContext.Current.Cache[“Account”]==null)

这一行:

 return (List<DAL.EntityClasses.AccountsEntity>)HttpContext.Current.Cache["Account"]; 
返回(列表)HttpContext.Current.Cache[“Account”];
可以清除缓存/可以从缓存中删除帐户条目

更好的实施方式是:

public static List<DAL.EntityClasses.AccountsEntity> Accounts             
{             
    get             
    {  
      List<DAL.EntityClasses.AccountsEntity> accounts = 
       HttpContext.Current.Cache["Account"] as List<DAL.EntityClasses.AccountsEntity> 

      if(accounts == null)
      {
        accounts = LoadAccounts();
        HttpContext.Current.Cache.Insert("Account", accounts, null, DateTime.Now.AddHours(4), System.Web.Caching.Cache.NoSlidingExpiration);          
      }  
      return accounts;
   }
}
公共静态列表帐户
{             
得到
{  
列出帐户=
HttpContext.Current.Cache[“帐户”]作为列表
如果(帐户==null)
{
accounts=LoadAccounts();
HttpContext.Current.Cache.Insert(“Account”,accounts,null,DateTime.Now.AddHours(4),System.Web.Caching.Cache.NoSlidingExpiration);
}  
归还账户;
}
}
仅供参考: 您对Accounts属性的实现存在一个问题,该问题与原始问题无关,但可能会在将来导致问题:

在这条线之间会发生什么

if(HttpContext.Current.Cache[“Account”]==null)

这一行:

 return (List<DAL.EntityClasses.AccountsEntity>)HttpContext.Current.Cache["Account"]; 
返回(列表)HttpContext.Current.Cache[“Account”];
可以清除缓存/可以从缓存中删除帐户条目

更好的实施方式是:

public static List<DAL.EntityClasses.AccountsEntity> Accounts             
{             
    get             
    {  
      List<DAL.EntityClasses.AccountsEntity> accounts = 
       HttpContext.Current.Cache["Account"] as List<DAL.EntityClasses.AccountsEntity> 

      if(accounts == null)
      {
        accounts = LoadAccounts();
        HttpContext.Current.Cache.Insert("Account", accounts, null, DateTime.Now.AddHours(4), System.Web.Caching.Cache.NoSlidingExpiration);          
      }  
      return accounts;
   }
}
公共静态列表帐户
{             
得到
{  
列出帐户=
HttpContext.Current.Cache[“帐户”]作为列表
如果(帐户==null)
{
accounts=LoadAccounts();
HttpContext.Current.Cache.Insert(“Account”,accounts,null,DateTime.Now.AddHours(4),System.Web.Caching.Cache.NoSlidingExpiration);
}  
归还账户;
}
}

谢谢。假设服务器能够完成任务,那么如果我将一些gig加载到缓存中会有问题吗?我在想所有的账户、用户、客户等等。有时我不想添加更改的订单。如果你有内存的话,这不是一个真正的问题。不要缓存整个数据库,缓存不会更改的内容,并确保在缓存项上设置正确的过期时间。谢谢。假设服务器能够完成任务,那么如果我将一些gig加载到缓存中会有问题吗?我在想所有的账户、用户、客户等等。有时我不想添加更改的订单。如果你有内存的话,这不是一个真正的问题。不要缓存整个数据库,缓存不会更改的内容,并确保在缓存项上设置适当的过期时间。