共享asp.net对象为静态或缓存 [英] shared asp.net object as static or cache

查看:44
本文介绍了共享asp.net对象为静态或缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在asp.net中存储共享对象的最佳方法是什么?每个请求上的每个请求都会被多次调用.香港专业教育学院一直在使用这两种方法,但我想知道是否有更好的方法.我每小时刷新一次该对象.

What is the best method of storing a shared object in asp.net? It will get called multiple times per request on every request. Ive been using these two methods but Id like to know if there is a better way. I refresh this object once an hour.

public static List<ResourceObject> SharedResources = new List<ResourceObject>()

//OR

public static List<ResourceObject> SharedResources
{
    get
    {
        List<ResourceObject> _sharedResources = HttpContext.Current.Cache["RedirectRoutes"] as List<ResourceObject>;
        if (_sharedResources == null)
        {
            _sharedResources = LoadNewSharedResource();
            HttpContext.Current.Cache["RedirectRoutes"] = _sharedResources;
        }

        return _redirectRoutes;
    }
    set
    {
        HttpContext.Current.Cache["RedirectRoutes"] = value;
    }
}

推荐答案

如果您的对象经常更改(即您提到的每小时更改),那么最好使用缓存,因为它可以处理刷新(假设您在将值添加到缓存时传递了正确的参数).如果您使用静态值,则不会每小时自动清除该值,因此您需要自己实施检查.

If your object is changing frequently (i.e. hourly as you mentioned) then you'll be best to use the cache as it will be able to take care of flushing for you (assuming you pass the correct parameters when adding the value to the cache). If you use a static value it will not be cleared out every hour automatically so you'd need to implement the check yourself.

这篇关于共享asp.net对象为静态或缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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