我正在尝试缓存我的数据表,以便自动完成。首先,我创建一个datatable,并用我的数据库中的内容填充它。然后,当我尝试缓存数据表时,我得到一个执行错误,这表明"using System.Runtime.Caching;“语句是错误的。我在项目中添加了system.runtime.caching引用。我不明白为什么我会得到这个错误。
此外,如果我删除该语句,我会得到一个异常,显示"Cache is not available“。
我是缓存方面的新手,可以随时解释这件事是如何做的。到目前为止,我每次在autoComplete框中搜索时都会向db发送查询。我想使用缓存来加快速度。下面是我的代码:
_Default obj = new _Default();
SqlConnection con = new SqlConnection(@"Data Source=db; Initial Catalog=cat;Integrated Security=True");
con.Open();
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand("select * from City", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
obj.Cache["Cache"] = dt;
发布于 2015-02-05 16:34:45
我也是DataCaching的新手,但我已经找到了适合我的解决方案。下面是我的工作示例:
private DataContext db = new DataContext();
public List<Trip> GetAll()
{
string key = "TripGetAll1";
var cac = HttpRuntime.Cache[key] as List<Trip>;
if (cac == null)
{
cac = db.Trip.Include(x => x.Hands)
.Include(x => x.TransferPoints)
.Include(x => x.Requests)
.Include(x => x.Vehicle)
.Include(x => x.Crew)
.Include(x => x.ServiceType)
.Where(x => x.StornoBy == null)
.ToList();
HttpRuntime.Cache.Insert(key, cac, null,
DateTime.Now.AddSeconds(System.Configuration.ConfigurationManager.AppSettings["ShortCache"].StringToInt()), TimeSpan.Zero);
}
return cac;
}
这部分
System.Configuration.ConfigurationManager.AppSettings["ShortCache"].StringToInt()
正在形成web.config持续时间(在开发中,稍后在生产中更容易调整)。StringToInt()是我的扩展,所以忽略它。
https://stackoverflow.com/questions/28339045
复制相似问题