WCF and caching

I have implemented caching in a WCF service hosted in a windows service
When my windows service starts I cache a dataset.

Now when my client calls the WCF service hosted in the windows service.
I noticed that the very first call  takes a long time because it cannot find the cache.

 ObjectCache cache = MemoryCache.Default;
            if (cache.Contains(pCacheKey))
             listPortfolios = (List<P>)cache.Get(pCacheKey);
get data and add to cache
Who is Participating?
käµfm³d 👽Connect With a Mentor Commented:
You'll probably need to clarify what you mean by "a long time," but think about it like this:

The first time you make a call, there is nothing in the cache. This means the code has to go and look at the primary source of the data, retrieve that data, and then populate the cache (and return the data that was requested). Once the cache has been populated, there is no need to look in the primary data source.

Now you said that, "my windows service loads the cache every morning." The implication I perceive is that you expect that by the time the first client makes a request, the cache has already been populated. Are you certain this is happening? I know that in IIS the worker process for hosted web services are spun down after a period of idle, which would cause any cache to be dumped. I didn't think this happened in a self-hosted WCF, but it might. If you are pulling from a database, then you might try watching the database to see if there is your initial morning hit, and then subsequently a hit when the first client makes a request. If there is, then your cache has possibly been dumped. You might also look at any configuration settings if you are using some component to handle the caching. It could be that your cache has a timeout set.
käµfm³d 👽Commented:
What is your question?
countrymeisterAuthor Commented:
The question is I am pretty sure the cache has the data, because my windows service loads the cache every morning.

But the very first call from the client takes a long time, the subsequent calls get the data back quickly .
My question is why does the very first call take a long time.
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

countrymeisterAuthor Commented:
here is my code, I am using System.Runtime.Caching, I have done the checks  you mentioned.
The wcf service is hosted in a windows service

Checking further on this

          ObjectCache cache = MemoryCache.Default;
if (!cache.Contains(pCacheKey))
                CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();
      cacheItemPolicy.AbsoluteExpiration = DateTime.Now.AddHours(24.0);

                cache.Add(pCacheKey, data, cacheItemPolicy);
countrymeisterAuthor Commented:
On another note, I also tested adding debug in my windows hosted wcf service, verifying the dat is in the cache.
Yet the first call from my WPF client tries to get data from the db instead of the cache.
käµfm³d 👽Commented:
At which point(s) in your application is the database accessed for the purpose of loading the cache?
countrymeisterAuthor Commented:
When the window service starts , I have  a background thread that loads all the required data in cache.

So in my testing in debug mode, I verified that the wcf service hosted in windows service has the cached data with the respective cache keys.

Once I verified that, I started my client and then made a service call to get the data and when I step through in debug mode again in my client, I notice that it tries to relaod the cached data.

Now is this the issue in debug mode per se, or should I be installing my service using install util and checking if this behaviour exists.
countrymeisterAuthor Commented:
Not much help provided I am closing this question
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.