private static MvcHtmlString GetMvcHtmlBySpOrCache(Func<MvcHtmlString> fun, string cacheKey, string spName)
{
if (CacheHelper.IsCached(cacheKey))
{
return CacheHelper.GetCacheValue(cacheKey) as MvcHtmlString;
}
SqlCacheDependency myDependency;
string conString = WebConfigurationManager.ConnectionStrings["Db"].ConnectionString;
using (SqlConnection connection = new SqlConnection(conString))
{
using (SqlCommand cmd = connection.CreateCommand())
{
cmd.CommandText = spName;
cmd.CommandType = CommandType.StoredProcedure;
myDependency = new SqlCacheDependency(cmd);
}
}
MvcHtmlString mvcHtmlString = fun();
CacheHelper.AddCacheForever(cacheKey, mvcHtmlString, myDependency);
return mvcHtmlString;
}
以上代码不起作用,缓存后,更新数据后,缓存并不失效。:(
存储过程类似这样:
--SET NOCOUNT ON;
select top 24 a.x, COUNT(b.y) as count
from dbo.XX a
inner join dbo.YY b on b.mm = a.mm
group by a.x
order by a.Id desc;