Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.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中的SQL Server数据缓存_C#_Asp.net_Sql Server_Caching - Fatal编程技术网

C# ASP.NET中的SQL Server数据缓存

C# ASP.NET中的SQL Server数据缓存,c#,asp.net,sql-server,caching,C#,Asp.net,Sql Server,Caching,我试图使用缓存来节省刷新web应用程序的时间,并在数据库发生更改时自动进行刷新,但在设置缓存时遇到了问题 我已将以下代码添加到我访问SQL的位置 SqlDependency.Start("Data Source=" + serverName + ";" + "Initial Catalog=...;" + "User Id=...;Password=...;"); 然后在SQL连接完成之后,在我从SQL中提取数据之前 SqlCacheDependenc

我试图使用缓存来节省刷新web应用程序的时间,并在数据库发生更改时自动进行刷新,但在设置缓存时遇到了问题

我已将以下代码添加到我访问SQL的位置

SqlDependency.Start("Data Source=" + serverName + ";" + 
                    "Initial Catalog=...;" + "User Id=...;Password=...;");
然后在SQL连接完成之后,在我从SQL中提取数据之前

SqlCacheDependency dependency = new SqlCacheDependency(command);

int numberOfMinutes = 3;
DateTime expires = DateTime.Now.AddMinutes(numberOfMinutes);

Current.Response.Cache.SetExpires(expires);
Current.Response.Cache.SetCacheability(HttpCacheability.Public);
Current.Response.Cache.SetValidUntilExpires(true);
Current.Response.AddCacheDependency(dependency);

在前面的代码使用的方法中,我访问了SQL并从SQL返回了一个对象列表。设置缓存还需要执行哪些步骤?我假设我必须以某种方式将数据添加到缓存中,然后以某种方式从缓存中检索数据

您可以使用
HttpRuntime.Cache
Cache对象

我将编写一个通用方法来
GET
SET
缓存对象

 public T GetOrSetCache<T>
    (string key,T obj, int cacheTime) where T:class,new()
{
    System.Web.Caching.Cache cacheContainer = HttpRuntime.Cache;
    T cacheObj = cacheContainer.Get(key) as T;

    if (cacheObj == null)
    {
        cacheContainer.Insert(key,
            obj,
            null, 
            DateTime.Now.AddMinutes(cacheTime),
            System.Web.Caching.Cache.NoSlidingExpiration);
        cacheObj = obj;
    }

    return cacheObj;
}

注意

  • 缓存
    是一个键/值`集合

  • 缓存中获取实例
    您需要提供一个密钥

  • 将实例设置为
    缓存
    您需要提供一个键和实例对象


  • 您可以使用
    HttpRuntime.Cache
    Cache对象

    我将编写一个通用方法来
    GET
    SET
    缓存对象

     public T GetOrSetCache<T>
        (string key,T obj, int cacheTime) where T:class,new()
    {
        System.Web.Caching.Cache cacheContainer = HttpRuntime.Cache;
        T cacheObj = cacheContainer.Get(key) as T;
    
        if (cacheObj == null)
        {
            cacheContainer.Insert(key,
                obj,
                null, 
                DateTime.Now.AddMinutes(cacheTime),
                System.Web.Caching.Cache.NoSlidingExpiration);
            cacheObj = obj;
        }
    
        return cacheObj;
    }
    

    注意

  • 缓存
    是一个键/值`集合

  • 缓存中获取实例
    您需要提供一个密钥

  • 将实例设置为
    缓存
    您需要提供一个键和实例对象


  • 如果我像您所描述的那样使用缓存,并且我的SQL表在某个时候得到更新,那么这个缓存会自动更新吗?不幸的是,我认为这是不可能的,因为缓存的主要目的是在内存中保留一些东西,如果您想使用数据,您可以立即获取它,而不必请求数据库,但是您的评论请求与缓存的主要意图不一样如果您的数据经常更新并且需要数据一致性,我建议您不要使用缓存。如果我像您所描述的那样使用缓存,并且我的SQL表在某个时刻得到更新,这个缓存会自动更新吗?不幸的是,我认为这是不可能的,因为缓存的主要目的是在内存中保留一些内容,如果您想使用数据,您可以立即获取它,这不需要请求db,但是您的注释请求与缓存的主要目的不同如果您的数据经常更新并且需要数据一致性,我建议您不要使用缓存。