Keep Cache when application exits (Resolved)

Topics: Caching Application Block
Apr 21, 2011 at 1:48 PM

Hi,

 

I am using Cache Application Block (Entreprise Library 4.1) to keep objects. I was able to cache and retrieve items from the cache until application exits but then, when I restart my application, I can't get my objects back. I tried with various parameters in the Add method (like CacheItemPriority.NotRemovable and SlidingTime configured with a 4 hours timespan) but it didn't work.

 

Am I wrong thinking that CAB can keep objects after application exit ?

Do I need a special parameter ?

Will I be able to get objects back from the cache with another application running on the same server (web application or windows service applications) ?

 

Thank you.

Apr 24, 2011 at 10:19 AM

As far as I know this should be. Could you post your related code and configuration here for us to further investigate? Thanks.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 26, 2011 at 7:10 AM
Edited Apr 26, 2011 at 7:11 AM

I used a console application.

I created an app.config file with Entreprise library configuration editor. Then I changed expirationPollFrequencyInSeconds to 3600 (also with configuration editor).

The resulting app.config is :

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        <section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </configSections>
    <cachingConfiguration defaultCacheManager="Cache Manager">
        <cacheManagers>
            <add expirationPollFrequencyInSeconds="3600" maximumElementsInCacheBeforeScavenging="1000"
                numberToRemoveWhenScavenging="10" backingStoreName="Null Storage"
                type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                name="Cache Manager" />
        </cacheManagers>
        <backingStores>
            <add encryptionProviderName="" type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                name="Null Storage" />
        </backingStores>
    </cachingConfiguration>
</configuration>

Here is the code of Console application, it is quite straightforward :

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;

namespace TestEntLib
{
	class Program
	{
		static void Main(string[] args)
		{
			const string KEY = "Key1";

			for (int i = 0; i < 10; i++)
			{
				ICacheManager cacheManager = CacheFactory.GetCacheManager();

				if (cacheManager.Contains(KEY))
				{
					Console.WriteLine("Contains key1 with item {0}", ((foo)cacheManager["Key1"])._bar);
				}
				else
				{
					// I tried this
					// cacheManager.Add(KEY, new foo("lol"));

					// Then this
					cacheManager.Add(KEY, new foo("lol"), CacheItemPriority.NotRemovable, null, new SlidingTime(new TimeSpan(4, 0, 0))); 
					
					Console.WriteLine("New key");
				}
			}
			Console.ReadKey();
		}

		public class foo
		{
			public string _bar = "132";

			public foo(string a)
			{
				_bar = a;
			}
		}
	}
}


 
When I executed it, I had this result :
New key
Contains key1 with item lol
Contains key1 with item lol
Contains key1 with item lol
Contains key1 with item lol
Contains key1 with item lol
Contains key1 with item lol
Contains key1 with item lol
Contains key1 with item lol
Contains key1 with item lol

Then I closed the application and ran it again, I got the same result but I expected only "Contains key1 with item lol".
Apr 26, 2011 at 7:58 AM

Hi,

Looking at your configuration, you didn't explicitly specify a backing storage to persist the cached data thus it will be place in the memory only. Once the application exits, the cached data will be lost. If you want to persist data accross application and system restarts, you need to add a persistent backing store (either Database or Isolated Storage) to your config. For more information, you can refer here.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 26, 2011 at 10:37 AM

OK, I tried with IsolatedStorage and added a serializable attribute to class foo and it worked.

Thanks a lot