[英]Can I temporarily store data in my C#.Net app to reduce the need for calls for data from SQL Server?
I created a C#.net app that uses dates from a SQL Server 2008 database table. 我创建了一个C#.net应用程序,它使用SQL Server 2008数据库表中的日期。 Is there a way for me to temporarily store the data so that my program does not have to repeatedly make server calls for the same set of information?
有没有办法暂时存储数据,以便我的程序不必为同一组信息重复进行服务器调用? I know how to pull the info I need and create a temporary dataset, however, it is only accessible to the particular method or class and then goes away.
我知道如何提取我需要的信息并创建一个临时数据集,但是,只有特定方法或类可以访问它然后消失。 I need the results to be universally available until the program closes.
在程序结束之前,我需要将结果普遍可用。
This is what I have so far and I am not sure where to go next: 这是我到目前为止所做的,我不知道下一步该怎么做:
SqlConnection ReportConnect = new SqlConnection(ConnectionString);
String reportQuery = @"SELECT DISTINCT DATE FROM dbo.myTable ORDER BY DATE DESC";
ReportConnect.Open();
SqlCommand cmd = ReportConnect.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.Connection = ReportConnect;
cmd.CommandText = reportQuery.ToString();
SqlDataReader rdr = cmd.ExecuteReader();
while(rdr.Read()) {
//I can access the results here
}
//how do I add this data for the life of the program instance to my current
//dataset. Let's say the dataset is named "activeDataset"
If you are going to use key/value pair caching, I recommend you use HttpRuntime.Cache (available outside ASP.NET applications) since it already does alot of work for you. 如果您打算使用键/值对缓存,我建议您使用HttpRuntime.Cache (在ASP.NET应用程序之外可用),因为它已经为您做了很多工作。
In it's simplest implementation: 在它最简单的实现:
public IList<DateTime> GetUniqueDates()
{
const string CacheKey = "RepositoryName.UniqueDates";
Cache cache = HttpRuntime.Cache;
List<DateTime> result = cache.Get[CacheKey] as List<DateTime>;
if (result == null)
{
// If you're application has multithreaded access to data, you might want to
// put a double lock check in here
using (SqlConnection reportConnect = new SqlConnection(ConnectionString))
{
// ...
result = new List<DateTime>();
while(reader.Read())
{
result.Add((DateTime)reader["Value"]);
}
}
// You can specify various timeout options here
cache.Insert(CacheKey, result);
}
return result;
}
Having said that, I usually use IoC trickery to create a caching layer in front of my repository for the sake of cohesion. 话虽如此,为了凝聚力,我通常使用IoC技巧在我的存储库前面创建一个缓存层。
You could create a singleton object, and store the data in this object. 您可以创建单个对象,并将数据存储在此对象中。
Be aware that there is a lot more to single ton objects that you will have to think about. 请注意,您需要考虑的单吨物体还有很多。
Have a look at 看一下
You could store the datatable in a static variable that would be accesible from any part of your code (is this necessary?). 您可以将数据表存储在一个静态变量中,该变量可以从代码的任何部分访问(这是必要的吗?)。
public class MyDataSetCache
{
public static DataSet MyDataSet { get; set; }
}
Some other code... 其他一些代码......
// SQL Statements....
MyDataSetCache.MyDataSet = activeDataset // Editted to follow OP :-)
You can definately use Cache to reduce database hits, Besides using SqlDependency you can have a cache based on time. 您可以肯定使用Cache来减少数据库命中,除了使用SqlDependency之外,您还可以根据时间使用缓存。 You can invalidate your cache let's say every 4 hours,and hit the database again.
您可以每隔4小时使缓存无效,然后再次访问数据库。 Check out Cache.Insert()
查看Cache.Insert()
我通常将整个对象序列化为一个文件,并在转到数据库之前尝试先读取它。
You can use a set of implementation hooks to achieve result: 您可以使用一组实现挂钩来实现结果:
ContainsKey
method) whether data is already fetched or needs sql-server call. ContainsKey
方法)数据是否已被提取或需要sql-server调用。 This can be useful when you need different DataSet
s. DataSet
时,这可能很有用。 Dictionary works pretty fast.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.