Skip to content

Instantly share code, notes, and snippets.

Created Dec 19, 2012

What would you like to do?

I would suggest two changes:

Remove the Mono for Android Dependency

Most of the DirectoryCache code is trivially cross-platform and usable in MonoTouch/desktop .NET/etc.; the Bitmap dependency isn't required. There are only two members that use Bitmap -- AddOrUpdate() and TryGet() -- and a Bitmap isn't required; they could instead take/provide a Stream.

Instead, I would suggest making it a strict cache management type for storing "blobs" (Streams) of data, and let higher-level logic deal with what the blobs are (Bitmaps, databases, etc.):

partial class DirectoryCache {
    // Replaces: AddOrUpdate(string,Bitmap,TimeSpan)
    public void AddOrUpdate (string key, Stream stream, TimeSpan? duration = null);

    // Replaces: TryGet(string, out Bitmap)
    public void WithEntryPath (string key, Action<string> action)
      key = SanitizeKey (key);
    	lock (entries) {
    		if (!entries.ContainsKey (key))
    		action (Path.Combine (basePath, key));

Reducing Memory Use

When it comes to Bitmaps and other large Java-side objects, you want to keep the Bitmap C# instance lifetime as short as possible. There are two ways to do this:

  1. Rely on the user to do it:

     using (var bitmap = SomeMethodThatReturnsABitmap ())

    This works, but requires "overhead" on consuming code. Furthermore, this isn't possible with the original DirectoryCache.TryGet() method, which would require that the caller explicitly call .Dispose().

  2. Do it for them by requiring use of a closure.

    This can be done with an extension method in terms of the above DirectoryCache.WithEntryPath() method:

     public static void WithBitmap(this DirectoryCache cache, string key, Action<Bitmap> action)
     	cache.WithEntryPath(key, path => {
     			using (var bitmap = BitmapFactory.DecodeFile (path))
     				action (bitmap);
     // ...
     cache.WithBitmap ("name", bitmap => 

Personally, I'd suggest (2).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.