从静态方法读取和写入ASP.NET缓存

时间:2013-06-11 12:11:29

标签: asp.net caching

我在一个名为helper.getdiscount()的辅助类中有一个静态方法。此类是ASP.NET前端代码,由UI页面使用。

在这个方法中,我检查一些数据是否在ASP.NET缓存中然后返回它,否则它进行服务调用并将结果存储在缓存中,然后返回该值。

考虑到多个线程可能同时访问它,这是一个问题吗?

if (HttpContext.Current.Cache["GenRebateDiscountPercentage"] == null)
{ 
    IShoppingService service = ServiceFactory.Instance.GetService<IShoppingService>();
    rebateDiscountPercentage= service.GetGenRebateDiscountPercentage().Result;


    if (rebateDiscountPercentage > 0)
    {
        HttpContext.Current.Cache.Add("GenRebateDiscountPercentage", rebateDiscountPercentage, null, DateTime.Now.AddDays(1), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);
    }
}
else
{      
    decimal.TryParse(HttpContext.Current.Cache["GenRebateDiscountPercentage"].ToString(), out rebateDiscountPercentage);
}

请告知是否可以使用或更好的方法。

2 个答案:

答案 0 :(得分:0)

使用锁定对象尝试类似的事情。

static readonly object objectToBeLocked= new object();

        lock( objectToBeLocked)
        { 
             if (HttpContext.Current.Cache["GenRebateDiscountPercentage"] == null)
            { 
                IShoppingService service = ServiceFactory.Instance.GetService<IShoppingService>();
                rebateDiscountPercentage= service.GetGenRebateDiscountPercentage().Result;


                if (rebateDiscountPercentage > 0)
                {
                    HttpContext.Current.Cache.Add("GenRebateDiscountPercentage", rebateDiscountPercentage, null, DateTime.Now.AddDays(1), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);
                }
            }
            else
            {      
                decimal.TryParse(HttpContext.Current.Cache["GenRebateDiscountPercentage"].ToString(), out rebateDiscountPercentage);
            }
        }

您也可以查看以下帖子。

What is the best way to lock cache in asp.net?

答案 1 :(得分:0)

使用这些通用方法将缓存用于任何类型:

<div id="letters">
<h3>Some pictures</h3>

<p>Pic 1<br />
<img src="./letters/a.png" width="100" height="100" />
</p>
<p>Pic 2<br />
<img src="./letters/b.png" width="100" height="100" />
</p>
<p>Pic 3<br />
<img src="./letters/c.png" width="100" height="100" />
</p>
<p>Pic 4<br />
<img src="./letters/d.png" width="100" height="100" />
</p>
<p>Pic 5<br />
<img src="./letters/e.png" width="100" height="100" />
</p>
<p>Pic 6<br />
<img src="./letters/f.png" width="100" height="100" />
</p>
<p>Pic 7<br />
<img src="./letters/g.png" width="100" height="100" />
</p>
<p>Pic 8<br />
<img src="./letters/h.png" width="100" height="100" />
</p>
<p>Pic 9<br />
<img src="./letters/i.png" width="100" height="100" />
</p>
<p>Pic 10<br />
<img src="./letters/j.png" width="100" height="100" />
</p>
<p>Pic 11<br />
<img src="./letters/k.png" width="100" height="100" />
</p>
<p>Pic 13<br />
<img src="./letters/l.png" width="100" height="100" />
</p>
<p>Pic 14<br />
<img src="./letters/m.png" width="100" height="100" />
</p>
<p>Pic 15<br />
<img src="./letters/n.png" width="100" height="100" />
</p>
<p>Pic 16<br />
<img src="./letters/o.png" width="100" height="100" />
</p>
<p>Pic 17<br />
<img src="./letters/p.png" width="100" height="100" />
</p>
<p>Pic 18<br />
<img src="./letters/q.png" width="100" height="100" />
</p>
<p>Pic 19<br />
<img src="./letters/r.png" width="100" height="100" />
</p>
<p>Pic 20<br />
<img src="./letters/s.png" width="100" height="100" />
</p>
<p>Pic 21<br />
<img src="./letters/t.png" width="100" height="100" />
</p>
<p>Pic 22<br />
<img src="./letters/u.png" width="100" height="100" />
</p>
<p>Pic 23<br />
<img src="./letters/v.png" width="100" height="100" />
</p>
<p>Pic 24<br />
<img src="./letters/w.png" width="100" height="100" />
</p>
<p>Pic 25<br />
<img src="./letters/x.png" width="100" height="100" />
</p>
<p>Pic 26<br />
<img src="./letters/y.png" width="100" height="100" />
</p>

</div>

现在解决你的问题:

`public static void AddCache(string key, object Data, int minutesToLive = 1440)
{
    if (Data == null)
        return;
    HttpContext.Current.Cache.Insert(key, Data, null, DateTime.Now.AddMinutes(minutesToLive), Cache.NoSlidingExpiration);
}

public static T GetCache<T>(string key)
{
    return (T)HttpContext.Current.Cache.Get(key);
} `

`