简体   繁体   English

缓存数据; ASP.NET

[英]Caching data; ASP.NET

NET and C# and writing an application that retrieves data from a database. NET和C#,并编写一个从数据库检索数据的应用程序。 I have attempted to cache the database locally in an attempt to speed up the application when doing a search. 我试图在本地缓存数据库,以试图在搜索时加快应用程序的速度。 I have used the following lines of code to retrieve and save the data from the database in cache. 我使用以下代码行从数据库中检索数据并将其保存在缓存中。

cached_database = HttpRuntime.Cache.Get(cacheID) as Entities;
if (cached_database == null)
{
    cached_database = InitializeDatabase(cacheID);
}



private Entities InitializeDatabase(string cacheID)
{
    var database = this.database;
    HttpRuntime.Cache.Insert(cacheID, database, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero);

    return database;
}

I can see that I am able to return the correct cached data by looking at the debugger and the application still runs however it does not have any performance improvements. 我可以看到通过查看调试器,我可以返回正确的缓存数据,并且该应用程序仍在运行,但是它没有任何性能改进。 Am I doing this wrong, or is there possibility a better way to accomplish what I would like to do? 我做错了吗,还是有更好的方法来完成我想做的事情? Thanks. 谢谢。

I think you are caching the database object, an abstraction of the database. 我认为您正在缓存数据库对象,即数据库的抽象。 This object does the actual querying into the database when you access it's methods and properties. 当您访问数据库的方法和属性时,它将对该数据库进行实际查询。 This is the expensive part - and this is why you don't get any effect from caching the database. 这是昂贵的部分-这就是为什么缓存数据库不会产生任何效果的原因。

Cache the results, as suggested in the comments. 缓存结果,如注释中所建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM