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

c# – 在ASP.Net中,在system.web.caching中存储int的最佳方法是什么?,第1张

概述目前,我必须将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" 目前,我必须将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()); // 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;}

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

总结

以上是内存溢出为你收集整理的c# – 在ASP.Net中,在system.web.caching中存储int的最佳方法是什么?全部内容,希望文章能够帮你解决c# – 在ASP.Net中,在system.web.caching中存储int的最佳方法是什么?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1217627.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存