0
我有一個項目,我們使用服務體系結構,與Ninject解決我們的服務。幾乎所有的服務都使用異步。如何使用IoC緩存異步服務?
現在,我想添加緩存到幾個服務。
我從來沒有在異步方法中使用緩存,所以我不確定如何去做。我只知道作爲HttpContext.Current爲null,我目前得到這個錯誤:
所以,我應該去什麼樣的戰略,使這項工作?
我的IoC設置:NinjectWebCommon.cs:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<Context>().To<Context>().InRequestScope();
kernel.Bind<UserManager<User>>().To<UserManager>().InTransientScope();
kernel.Bind<IUserTokenProvider<User, string>>().ToMethod((x) =>
{
var provider = OwinConfig.DataProtectionProvider;
return new DataProtectorTokenProvider<User>(provider.Create("ASP.NET Identity"));
}).InTransientScope();
kernel.Bind<ICacheService>().To<HttpCache>();
kernel.Bind<IUserService>().To<UserService>();
// Lots of nice services
}
我ICacheService:
public interface ICacheService
{
object GetById(string cacheKey);
object Create(string cacheKey, object obj);
object Create(string cacheKey, object obj, DateTime expire);
void Delete(string cacheKey);
void DeleteByContaining(string containing);
string GetCacheKey(string methodName, string value);
bool ContainsKey(string cacheKey);
}
CacheService:
public class HttpCache : ICacheService
{
private static readonly ILog logger = LogManager.GetLogger(typeof (HttpCache));
public object GetById(string cacheKey)
{
if (ContainsKey(cacheKey))
{
return HttpContext.Current.Cache[cacheKey];
}
return null;
}
public object Create(string cacheKey, object obj)
{
return Create(cacheKey, obj, DateTime.UtcNow.AddHours(5));
}
public object Create(string cacheKey, object obj, DateTime expire)
{
if (!ContainsKey(cacheKey))
{
HttpContext.Current.Cache.Insert(cacheKey,
obj,
null,
expire,
Cache.NoSlidingExpiration);
}
return GetById(cacheKey);
}
public void Delete(string cacheKey)
{
HttpContext.Current.Cache.Remove(cacheKey);
}
public void DeleteByContaining(string containing)
{
List<string> deleteList = new List<string>();
HttpContext oc = HttpContext.Current;
// find all cache keys in the system... maybe insane? I don't know lol
IDictionaryEnumerator en = oc.Cache.GetEnumerator();
while (en.MoveNext())
{
var k = en.Key.ToString();
if (k.Contains(containing))
{
deleteList.Add(k);
}
}
foreach (var del in deleteList)
{
Delete(del);
}
}
public bool ContainsKey(string cacheKey)
{
return HttpContext.Current.Cache[cacheKey] != null;
}
public string GetCacheKey(string methodName, string value)
{
return string.Format("{0}_{1}", methodName, value);
}
}
手機號碼:
public async Task<City> GetById(int id)
{
var cacheKey = _cacheService.GetCacheKey(MethodBase.GetCurrentMethod().Name, id.ToString());
if (!_cacheService.ContainsKey(cacheKey))
{
var city = await _db.Cities.FirstAsync(c => c.Id == id);
_cacheService.Create(cacheKey, city);
return city;
}
return (City)_cacheService.GetById(cacheKey);
}
@AlexeiLevenkov可能因爲我很愚蠢 - 有很高的可能性!感謝您的鏈接 –
@AlexeiLevenkov所以,你說,如果我直接使用System.Web.HttpRuntime.Cache,我會安全嗎?請發表一個答案 - 也歡迎您取笑我的愚蠢:D –
或者,使用'System.Runtime.Caching'。 – howcheng