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);
            else
            {
get data and add to cache
}
LVL 1
countrymeisterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
What is your question?
0
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.
0
käµfm³d 👽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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

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);
0
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.
0
käµfm³d 👽Commented:
At which point(s) in your application is the database accessed for the purpose of loading the cache?
0
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.
0
countrymeisterAuthor Commented:
Not much help provided I am closing this question
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WCF

From novice to tech pro — start learning today.