加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 在ASP.Net中,在system.web.caching中存储int的最佳方法是

发布时间:2020-12-16 01:50:51 所属栏目:百科 来源:网络整理
导读:目前,我必须将int转换为字符串并存储在缓存中,非常复杂 int test = 123;System.Web.HttpContext.Current.Cache.Insert("key",test.ToString()); // to save the cachetest = Int32.Parse(System.Web.HttpContext.Current.Cache.Get("key").ToString()); // t
目前,我必须将int转换为字符串并存储在缓存中,非常复杂

int test = 123;
System.Web.HttpContext.Current.Cache.Insert("key",test.ToString()); // to save the cache
test = Int32.Parse(System.Web.HttpContext.Current.Cache.Get("key").ToString()); // to get the cache

这是一个一次又一次没有改变类型的更快的方式吗?

解决方法

您可以在缓存中存储任何类型的对象.方法签名是:

Cache.Insert(string,object)

所以,在插入之前不需要转换为字符串.但是,从缓存中检索时,您需要进行强制转换:

int test = 123;
HttpContext.Current.Cache.Insert("key",test); 
object cacheVal = HttpContext.Current.Cache.Get("key");
if(cacheVal != null)
{
    test = (int)cacheVal;
}

这将导致原始类型的装箱/拆箱惩罚,但每次都比通过字符串少得多.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读