My case is use WCF to return datatable from sqlserver database. some wcf method use in high frequency. so it will give much more stress to database. can we use cache in wcf,like http cache,set a expire time. in expire time ,return cached datatable,out expire time ,select the data from sqlserver again. thx, i'm the first time to use stackoverflow ,and I'm from china,sorry for my poor english. ps: my wcf case use winform/console as host,not iis.
Asked
Active
Viewed 4,172 times
0
-
try using System.Caching, very easy to use. Also, if you want you can look into some of the persistent caching libraries like escent. – SutharMonil May 24 '13 at 10:20
2 Answers
4
If you are using .NET 4.0 or later, you can use the MemoryCache class. For example:
ObjectCache cache = MemoryCache.Default;
You can store your DataSet
in the cache with a line like this:
cache["MyDataSet"] = myDataSet; // myDataSet is your DataSet
You can then use the following logic to retrieve the DataSet
:
if (cache["MyDataSet"] != null)
{
// Get your DataSet from the database
}
else
{
myDataSet = (DataSet)cache["MyDataSet"];
}
Also, take a look at CacheItemPolicy, which will allow you to set eviction and expiration policies for a given item in the cache.

Tim
- 28,212
- 8
- 63
- 76
-
thx,I find that method later,like your post.It's hard to decide which wcf method use cache ,and some other sync problem.if there is any way to retrive database change,if has change then select again, else use cache,then no sync problem.Hope can find a way to hash a table in sqlserver to find if table has changed. – wjlJack May 24 '13 at 06:25
-
Take a look at [SqlChangeMonitor Class](http://msdn.microsoft.com/en-us/library/system.runtime.caching.sqlchangemonitor.aspx) for one way to monitor changes to the underlying SQL. You'll probably need to monitor a table and/or columns, not the DataSet itself (similar to SqlCacheDepdency). – Tim May 24 '13 at 06:40
-
See this example [.NET 4.0 MemoryCache with SqlChangeMonitor](http://www.codeproject.com/Articles/167282/NET-4-0-MemoryCache-with-SqlChangeMonitor) for one way to do this (MSDN is a little light on how to use it). – Tim May 24 '13 at 06:43
-
On your first line of code, I get a compilation error: "Cannot implicitly convert type 'System.Runtime.Caching.MemoryCache' to 'venuefinder.controls.ObjectCache'" :( – NickG Mar 11 '14 at 17:32
-
1@NickG - What is `venuefinder.controls.ObjectCache`? You'll get more help if you post a question and give relevant code/information. – Tim Mar 11 '14 at 18:13
-
@Tim You've made me realise what the problem is! For some reason, another developer created a totally empty class called "ObjectCache" within my project (about 2 years ago according to source control). I hadn't realised the significance of own projects namespace in the error. No idea why that class is there as it's empty. Doh. – NickG Mar 11 '14 at 23:23
0
Would something like this meet your needs? This is pseudo code, but I think it gets the point across.
static DataTable dt;
static DateTime LastPulled;
public static void GetData()
{
if(LastPulled==null || DateTime.Now-LastPulled > new TimeSpan(5))
{
//Retrieve DataTable from database
}
return dt;
}
Also, see Caching in WCF?
-
use this can work,but it's too redundant,is there a common class to achieve this goal? – wjlJack May 24 '13 at 04:16
-
Not that I'm aware of. If you need one, it would be pretty simple to make yourself. I would use a delegate so that you can specify the action for your class to perform to update the data. – mason May 24 '13 at 04:20
-
Try looking at http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx for existing classes. – mason May 24 '13 at 04:21