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:
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:
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;
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;
}
}
Guys, I have to say your WP7Contrib lib still rocks :)
ReplyDeleteMaybe a small demo app with use of the Navigation service in combination with mainview tombstoning would also be great ;)
I'll have a chat with Rich and see what we can come up with for a demo
ReplyDeleteHi Depechie,
ReplyDeleteSorry 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
Hey no worries about the delay ;)
ReplyDeleteI figured it out myself already... ( thanks to the demo you mentioned above )!