While working on the android-client for MediaWiki, I quickly hit a bottleneck. Remote calls are time-consuming, as are parsing raw JSON text into JSON objects. Like any good Mediawiki developer, I decided to throw some caching on it. However, Android lacks an object cache. There’s a more specialized CacheManager, but it’s specific to network requests only, and only caches the raw results you downloaded. That’s all fine and well, but I want to cache arbitrary stuff.

So I wrote AndCache this weekend. Fellow Android developers, object caching just got a whole lot easier for you. There’s only 3 things you’ll ever (need to) call, so I’ll explain each one of them.

AndCache.setContext( ctx );

Android SQLite databases require a context so they know where to save the data. I give a static interface for projects to set the context and leave it be, throughout their application. This serves two purposes: firstly, the cache doesn’t have to have its own context, and secondly: by having the context tied to the calling application and not tied to the cache itself, we make it work independently of other applications, securing your data.

AndCache.get( "someKey" );

To get something from the cache, just call get() Given a String, it will check the cache for whatever is stored with that key. It returns as a generic Object, so you can cast it to whatever you need. Returns null if nothing found or expired, so you can easily see if you got a cache hit (all exceptions are handled internally, so you have a clean interface).

AndCache.set( "someKey", someValue, timeToLive );

To put something into the cache, call set() with a key name, some object you want to cache, and how long to keep it in the cache (in seconds). The passed object will be serialized and stored as a blob for later retrieval (this is all handled transparently). We return true/false on a successful save to cache.

This is released under the GNU General Public License, version 2 or (at your option) any later version. Enjoy.