Then we add the object in the isolated storage. For each key, a directory with the key as name is created, then we create a file named with the expiration date with the Windows file time UTC format (A Windows file time is a 64-bit value that represents the number of intervals of 100 nanoseconds that have elapsed since 12: 00 midnight, 1 January 1601).

Now that we have the foundations for a cache system, we can use it easily with a bit of Rx. In this example, we have a GetAll method that returns data from a WCF service. The MyServiceClient was generated by Visual Studio.

publicvoid GetAll(Action<ObservableCollection<MyEntity>> callback)
{
// We check if the cache has not expiredif (Cache.Current.Contains("KEY1"))
{
// W read the cache in a new thread then we call the callback in the dispatcher thread
Observable.Start(() =>
Cache.Current.Get<ObservableCollection<MyEntity>>("KEY1"), Scheduler.ThreadPool).
ObserveOn(Scheduler.Dispatcher).Subscribe(callback);
return;
}
// New WCF client
MyServiceClient client = new MyServiceClient();
Observable.FromEvent<GetAllCompletedEventArgs>(client, "GetAllCompleted")
.ObserveOn(Scheduler.ThreadPool)
.Select(s =>
{
// In a new thread, we had the object received by WCF in the cache,//the life time is 1 dayif (s.EventArgs.Error == null)
{
Cache.Current.Add("KEY1", s.EventArgs.Result, Cache.NoAbsoluteExpiration, TimeSpan.FromDays(1));
}
return s;
})
.ObserveOn(Scheduler.Dispatcher).Subscribe(s =>
{
// Then in the dispacher thread, we call the callback
callback(s.EventArgs.Result);
});
// Async call for the GetAll method
client.GetAllAsync();
}