I'm currently testing AppFabric caching, but I'm having a strange problem with my caching. Even though I say he needs to cache a simple object for 3 minutes, he only caches it for approximately 3 seconds.
The settings for my cache are as follows:
PS C:> Get-CacheConfig default CacheName : default TimeToLive : 100 mins CacheType : Partitioned Secondaries : 0 IsExpirable : True EvictionType : LRU NotificationsEnabled : False
I built this cache using the following 3 commandlets:
Use-CacheCluster
Start-CacheCluster
Grant-CacheAllowedClientAccount <myAccount>
Afterwards I try to run the following program (program.cs):
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press any key to continue, Q to quit");
while (Console.ReadKey() != new ConsoleKeyInfo('q', ConsoleKey.Q, false, false, false))
{
DoStuff();
}
}
static void DoStuff()
{
Person p = null;
DataCacheItem cacheItem = CacheUtility.CurrentCache.GetCacheItem("jebas");
if (cacheItem != null)
{
Console.WriteLine("V - Got my stuff from cache");
p = (Person)cacheItem.Value;
}
if (p == null)
{
Console.WriteLine("! - I just did an expensive call");
p = GetPerson();
CacheUtility.CurrentCache.Add("jebas", p, new TimeSpan(0, 30, 0));
}
}
private static Person GetPerson()
{
return new Person() { FirstName = "Jeroen", LastName = "Van Bastelaere", Id = Guid.NewGuid() };
}
}
CacheUtility class:
sealed class CacheUtility
{
private static DataCacheFactory _factory;
private static DataCache _cache;
private static readonly object _lock = new object();
CacheUtility() { }
public static DataCache CurrentCache
{
get
{
lock (_lock)
{
if (_cache == null)
{
List<DataCacheServerEndpoint> servers = new List<DataCacheServerEndpoint>();
servers.Add(new DataCacheServerEndpoint("demo2010a", 22233));
DataCacheFactoryConfiguration config = new DataCacheFactoryConfiguration()
{
Servers = servers
};
_factory = new DataCacheFactory(config);
_cache = _factory.GetDefaultCache();
}
return _cache;
}
}
}
}
Using this program, I just run it and press a random key for an amount of time: Most of the time it says "I got it from cache", but once in every 3 seconds it does the expensive call. When I check with Get-CacheStatistics default when he misses the cache he has this:
PS C:> Get-CacheStatistics default
Size : 0 ItemCount : 0 RegionCount : 1 RequestCount : 1543 MissCount : 75
When it is in cache it gives me this:
Size : 566 ItemCount : 1 RegionCount : 1 RequestCount : 1553 MissCount : 77
Using perfmon I was able to tell that my object gets evicted, because the counter Total Evicted Objects rises when I run my program (and it's suddenly not in cache anymore).
I have no idea why it is being evicted though, that's the only object I'm caching at the moment and the server has got plenty of memory left (4GB+)
Can anybody help me to get my object cached for the 30 minutes that I ask it to cache instead of 3 seconds?
Help is very appreciated. :-)
Thanks in advance,
Jeroen