Skip to main content

WP7Contrib: Isolated Storage Cache Provider

15/04/2001 - The code for this example has been updated - new WP7 build of SilverlightSerializer, this has removed the explicit implementation of ISerializeObject interface from the example model classes.

WP7Contrib received it's first question on the discussions forum, it was about how to use the IsolatedStorageCacheProvider.

Rich & I have been using this for a while in development and the question highlighted the lack of examples on how to use some of the stuff we've pushed out. So I thought I would share some insight into the demo I produced to answer the question.

You'll find the demo in the 'CacheProviderDemo' in the 'Spikes' directory in the code-base. It demostrates how to add both value & reference types to the cache, how these gets persisted to isolated storage and how values are purged from the cache. Before I show some code snippets I'll give some background to our thinking about caching.

As wikipedia states 'a cache is a component that transparently stores data so that future requests for that data can be served faster...'.

This is basis for the caching mechanisms in the WP7Contrib along with the principle that any implementation should be fault tolerant, i.e. if the cache fails to add or remove items this should not cause the host application to crash, the only exhibited behaviour would be a slight degradation in application performance.

The isolated cache provider has 3 requirements for any items you want to store:
  • Value types - These can not be used directly with the cache provider, the wrapper class CacheValue has to be used with these or cast to the type of Object. The cache provider only supports storing reference types,
  • Serialization - Any item you want to add to the cache (key or value) must support serialization via the SilverlightSerializer, Mike Talbot's blog has detailed information about to mark reference types for serialization and providing custom implementation through the ISerializeObject interface,
  • Hash code - Items are stored using a key-value pair hash-table in memory (as well as being persisted to isolated storage), and the key is expected to have a predictable hash code. Therefore we recommend using the properties of the reference type to calculate the value. We provide a mechanism in the BaseModel class for this purpose, CombineHashCodes, obviously you can use what ever alogrthim best fits your requirement.

That's enough background lets get to the code, as with all my demos the code is in the 'code behind'.

The snippet below shows the setup of the cache provider. There are 3 requirements for the constructor:

  • Application name - string identifing the cache, typically we use the application name, e.g. 'CacheProviderDemo',
  • Serialization assemblies - a list of any extra assemblies required for serialization by the SilverlightSerializer, these will be the assemblies containing your references types you've marked for serialization,
  • Log - captures diagnostic info about the provider.
We also initialise a variable for cache timeout - this is how long items remain in the cache before they are purged, we set the value to 20 seconds, obviously you can set this on a per item basis as required.
private const string ApplicationName = "CacheProviderDemo";
private ICacheProvider cacheProvider;
private TimeSpan cacheTimeout;
private ILog log;

public MainPage()
{
    InitializeComponent();

    var extraSerialisationAssemblies = new List<Assembly>();
    extraSerialisationAssemblies.Add(this.GetType().Assembly);

    log = new DebugLog();
    cacheProvider = new IsolatedStorageCacheProvider(ApplicationName, extraSerialisationAssemblies, log);
    cacheTimeout = TimeSpan.FromSeconds(20);
}
The final snippet below shows adding a value type to the cache and then reading the value back, both the key & value are value types in this example.
private void addValueType_Click(object sender, RoutedEventArgs e)
{
    var key = Guid.NewGuid().ToString();
    var value =  new CacheValue(42);

    cacheProvider.Add(key, value, cacheTimeout);

    log.Write("Added value type to the cache...");

    var cacheValue = cacheProvider.Get<string, CacheValue>(key);
    var actualValue = (int)cacheValue.Value;

    log.Write("Value type read back from cache, value - '{0}'", actualValue);
}
The snippet below shows adding a reference type to the cache and then reading the value back, both the key & value are reference types in this example. The key is an instance of the 'ExampleReferenceTypeKey' class and supports serialization (see bottom of post for full class listing).
private void addReferenceType_Click(object sender, RoutedEventArgs e)
{
    var key = new ExampleReferenceTypeKey
        {
        FirstName = "Dirk",
        LastName = "Gently",
        Url = new Uri("http://wp7contrib.codeplex.com/"),
        Random = Guid.NewGuid()
        };

    var value = new List<Guid>();
    for (var i = 0; i < 100; i++)
       value.Add(Guid.NewGuid());

    cacheProvider.Add(key, value, cacheTimeout);

    log.Write("Added reference type to the cache...");

    var cacheValue = cacheProvider.Get<ExampleReferenceTypeKey, List<Guid>>(key);

    log.Write("Value type read back from cache, list count = {0}", cacheValue.Count);
}
The screenshot below shows the automatic persistence and purging working for the demo, it's a screenshot of the output window from visual studio. The timeline for this is as follows:

1. An item is added to the cache,
2. A maximum wait of 15 seconds happens before the cache is persisted(to isolated storage),
3. 20 seconds after the item is added to the cache it is purged from the cache,
4. A maximum wait of 15 seconds happens before the cache is persisted again.



That pretty much rounds up how the IsolatedStorageCacheProvider and as i said earlier the implementation and demo can be downloaded from the WP7Contrib project on CodePlex.


Full list for class ExampleReferenceTypeKey;

[Serializer(typeof(ExampleReferenceTypeKey))]
public class ExampleReferenceTypeKey : BaseModel, IEquatable<ExampleReferenceTypeKey>, ISerializeObject
{
    private string firstName;
    private string lastName;
    private Uri url;
    private Guid random;

    public ExampleReferenceTypeKey()
    {
    }

    public object[] Serialize(object target)
    {
        var key = (ExampleReferenceTypeKey)target;
        return new object[] { key.FirstName,
            key.LastName,
            key.Url,
            key.Random };
    }

    public object Deserialize(object[] data)
    {
        var key = new ExampleReferenceTypeKey
        {
            FirstName = (string)data[0],
            LastName = (string)data[1],
            Url = (Uri)data[2],
            Random = (Guid)data[3]
        };

        return key;
    }

    public string FirstName
    {
        get { return this.firstName; }
        set { this.SetPropertyAndNotify(ref this.firstName, value, () => this.FirstName); }
    }

    public string LastName
    {
        get { return this.lastName; }
        set { this.SetPropertyAndNotify(ref this.lastName, value, () => this.LastName); }
    }

    public Uri Url
    {
        get { return this.url; }
        set { this.SetPropertyAndNotify(ref this.url, value, () => this.Url); }
    }

    public Guid Random
    {
        get { return this.random; }
        set { this.SetPropertyAndNotify(ref this.random, value, () => this.Random); }
    }
        
    public override int GetHashCode()
    {
        return this.CombineHashCodes(FirstName, LastName, Url, Random);
    }

    public static bool operator ==(ExampleReferenceTypeKey k1, ExampleReferenceTypeKey k2)
    {
        return Equals(k1, k2);
    }

    public static bool operator !=(ExampleReferenceTypeKey k1, ExampleReferenceTypeKey k2)
    {
        return !Equals(k1, k2);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj))
        {
            return false;
        }

        return obj is ExampleReferenceTypeKey && this.Equals((ExampleReferenceTypeKey)obj);
    }

    public bool Equals(ExampleReferenceTypeKey key)
    {
        if (ReferenceEquals(null, key))
        {
            return false;
        }

        if (this.FirstName != key.FirstName)
        {
            return false;
        }

        if (this.LastName != key.LastName)
        {
            return false;
        }

        if (this.Url != key.Url)
        {
            return false;
        }

        if (this.Random != key.Random)
        {
            return false;
        }

        return true;
    }
}

Comments

  1. Guys, I have to say your WP7Contrib lib still rocks :)
    Maybe a small demo app with use of the Navigation service in combination with mainview tombstoning would also be great ;)

    ReplyDelete
  2. I'll have a chat with Rich and see what we can come up with for a demo

    ReplyDelete
  3. Hi Depechie,

    Sorry for the delay Rich been at MVP11 so only manaed today to find out there is already an example using navigation service in the Spikes directory in the code base.

    Check out the spike directory - 'ServiceStyleRatherThanMessengerStyle'

    Rich also has a post on his blog about it - http://blogs.xamlninja.com/mvvm/wp7-contrib-when-messaging-becomes-messy-and-services-shine

    ReplyDelete
  4. Hey no worries about the delay ;)
    I figured it out myself already... ( thanks to the demo you mentioned above )!

    ReplyDelete

Post a Comment

Popular posts from this blog

WPF tips & tricks: Dispatcher thread performance

Not blogged for an age, and I received an email last week which provoked me back to life. It was a job spec for a WPF contract where they want help sorting out the performance of their app especially around grids and tabular data. I thought I'd shared some tips & tricks I've picked up along the way, these aren't probably going to solve any issues you might be having directly, but they might point you in the right direction when trying to find and resolve performance issues with a WPF app. First off, performance is something you shouldn't try and improve without evidence, and this means having evidence proving you've improved the performance - before & after metrics for example. Without this you're basically pissing into the wind, which can be fun from a developer point of view but bad for a project :) So, what do I mean by ' Dispatcher thread performance '? The 'dispatcher thread' or the 'UI thread' is probably the most

Showing a message box from a ViewModel in MVVM

I was doing a code review with a client last week for a WPF app using MVVM and they asked ' How can I show a message from the ViewModel? '. What follows is how I would (and have) solved the problem in the past. When I hear the words ' show a message... ' I instantly think you mean show a transient modal message box that requires the user input before continuing ' with something else ' - once the user has interacted with the message box it will disappear. The following solution only applies to this scenario. The first solution is the easiest but is very wrong from a separation perspective. It violates the ideas behind the Model-View-Controller pattern because it places View concerns inside the ViewModel - the ViewModel now knows about the type of the View and specifically it knows how to show a message box window: The second approach addresses this concern by introducing the idea of messaging\events between the ViewModel and the View. In the example below

Implementing a busy indicator using a visual overlay in MVVM

This is a technique we use at work to lock the UI whilst some long running process is happening - preventing the user clicking on stuff whilst it's retrieving or rendering data. Now we could have done this by launching a child dialog window but that feels rather out of date and clumsy, we wanted a more modern pattern similar to the way <div> overlays are done on the web. Imagine we have the following simple WPF app and when 'Click' is pressed a busy waiting overlay is shown for the duration entered into the text box. What I'm interested in here is not the actual UI element of the busy indicator but how I go about getting this to show & hide from when using MVVM. The actual UI elements are the standard Busy Indicator coming from the WPF Toolkit : The XAML behind this window is very simple, the important part is the ViewHost. As you can see the ViewHost uses a ContentPresenter element which is bound to the view model, IMainViewModel, it contains 3 child v