0
我想在包含WCF Service
的Web角色项目中使用System.Runtime.Caching.MemoryCache
。我可以在云网络角色项目中使用System.Runtime.Caching.MemoryCache吗?
有人可以请让我知道我们是否可以在Cloud web
角色项目中使用System.Runtime.Caching.MemoryCache
?
如果是,请让我知道内存和其他约束。
我想在包含WCF Service
的Web角色项目中使用System.Runtime.Caching.MemoryCache
。我可以在云网络角色项目中使用System.Runtime.Caching.MemoryCache吗?
有人可以请让我知道我们是否可以在Cloud web
角色项目中使用System.Runtime.Caching.MemoryCache
?
如果是,请让我知道内存和其他约束。
是的,你可以。
您应该将对System.Runtime.Caching的引用添加到Web Role项目中,然后使用类似下面的代码的东西(它几乎没有做任何事情,当然也不是最佳实践)。
刚用ASP.NET MVC在Azure模拟器的云网络角色中试用过它,它就可以工作。
关于限制 - 您可以使用两个CacheMemoryLimit和PhysicalMemoryLimit属性来检索所需的值。它显示了以字节为单位的限制。我不知道Azure云服务中的内存缓存方面是否存在任何限制。
private static object _lock = new Object();
private static MemoryCache _cache = new MemoryCache("ThisIsMyCache");
public static object GetItem(string key)
{
lock (_lock)
{
var item = _cache.Get(key);
if (item == null)
{
item = InitiaizeItem(key);
_cache.Set(key, item, new CacheItemPolicy());
}
return item;
}
}
private static object InitiaizeItem(string key)
{
return new { Value = key };
}