Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.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# 这是正确的缓存吗?_C#_Asp.net_Caching - Fatal编程技术网

C# 这是正确的缓存吗?

C# 这是正确的缓存吗?,c#,asp.net,caching,C#,Asp.net,Caching,我尝试缓存我的userlist,这样当200个用户在线时,每10秒就不会有200个数据库查询 我有以下代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Business { public class UserList { private static object locker = new object()

我尝试缓存我的userlist,这样当200个用户在线时,每10秒就不会有200个数据库查询

我有以下代码:

using System;
using System.Collections.Generic;    
using System.Linq;
using System.Text;

namespace Business
{
    public class UserList
    {
        private static object locker = new object();
        public static List<DAL.OnlineList> userList;
        public static DateTime date;
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
名称空间业务
{
公共类用户列表
{
私有静态对象锁定器=新对象();
公共静态列表用户列表;
公共静态日期时间日期;
}
}
-

公共静态字符串GetOnlineList(HttpContext-con)
{
List onlineList=新列表();
if(Business.UserList.date==DateTime.MinValue | | Business.UserList.datex.Username.ToList();
Business.UserList.UserList=onlineList;
}
其他的
{
onlineList=Business.UserList.UserList;
}
//以前
//List onlineList=DAL.UserDAL.GetAllOnlineUser().OrderBy(x=>x.Username.ToList();
}
WebMethod/pageMethod/JavaScript调用每隔10秒调用一次方法GetOnlineList。 以前是:200个用户,每10秒=200 x 10 x 6=每分钟12000 db查询


那么我的代码是正确的,第一个用户将从数据库加载列表并存储它-并且它将每30秒刷新一次-正确吗?

我认为代码片段中的条件需要调整

if (Business.UserList.date == DateTime.MinValue || 
    Business.UserList.date > DateTime.Now.AddSeconds(-30))
您始终可以使用内置的缓存机制ASP.NET,该机制具有。你可以读到它

基本上,有两个选项可以缓存具有滑动过期和绝对过期的对象

使用滑动过期时,如果在设置的过期时间间隔之前检索对象,则该对象将保留在缓存中。例如,如果将时间跨度设置为2分钟,并且每1分钟检索一次对象,则该对象将永远保留在缓存中

使用绝对过期,对象将根据时间跨度保留在缓存中,无论检索了多少次

在您的示例中,您拥有绝对过期逻辑。下面是一个关于如何使用它的示例:

public List<DAL.OnlineList> Users
{
    get 
    {
        List<DAL.OnlineList> users = null;
        string CacheKey = "dal_users";

        users = HttpContext.Current.Cache[CacheKey];
        if ((users == null)) 
        {
            users = DAL.UserDAL.GetAllOnlineUser()
                    .OrderBy(x => x.Username).ToList();
            HttpContext.Current.Cache.Add(CacheKey, users, Nothing, 
                DateTime.Now.AddSeconds(30), Caching.Cache.NoSlidingExpiration, 
                CacheItemPriority.Default, null);
        }

        return users;
    }
}
公开用户列表
{
得到
{
列表用户=null;
string CacheKey=“dal_用户”;
users=HttpContext.Current.Cache[CacheKey];
如果((用户==null))
{
users=DAL.UserDAL.GetAllOnlineUser()
.OrderBy(x=>x.Username).ToList();
HttpContext.Current.Cache.Add(CacheKey,users,Nothing,
DateTime.Now.AddSeconds(30),Caching.Cache.NoSlidingExpiration,
CacheItemPriority.Default,空);
}
返回用户;
}
}

+1绝对过期
如果(Cache[“UserList”]!=null)//使用Cache else//数据库调用
或者,您可以在global.asax中设置一个循环,每30秒更新一次缓存,那么用户就不必等待数据库更新。。。它总是最新的,而不依赖于更新缓存的请求。
public List<DAL.OnlineList> Users
{
    get 
    {
        List<DAL.OnlineList> users = null;
        string CacheKey = "dal_users";

        users = HttpContext.Current.Cache[CacheKey];
        if ((users == null)) 
        {
            users = DAL.UserDAL.GetAllOnlineUser()
                    .OrderBy(x => x.Username).ToList();
            HttpContext.Current.Cache.Add(CacheKey, users, Nothing, 
                DateTime.Now.AddSeconds(30), Caching.Cache.NoSlidingExpiration, 
                CacheItemPriority.Default, null);
        }

        return users;
    }
}