EggTaskCache

One of the things that has generally been a pain when using GTask is multiplexing results. For example, say you are doing a fairly complex operation, like compiling a source file, or fetching something from the internet. It's nice that you can do the work asynchronously, but you probably don't want to do it 3 times, at the same time. GTask doesn't give you anything in the way of chaining results (something we should probably add). But even more so it can be handy to cache the results for later use. But now you are writing a cache, so you need eviction too.

So today I put together EggTaskCache. We are using it in Builder to simplify all of our code paths that need access to the translation unit. It works like a transparent cache, but asynchronously and uses GTask. You also get a callback which can asynchronously load the content for the cache. Upon completion, the error or result object is multiplexed to all of the other waiting tasks who queued for the same cache key.

To make eviction easier, you can set a TTL on the EggTaskCache. EggTaskCache comes with a built-in GSource that connects to the threads default GMainContext. So cache eviction "just works".

Also, it comes with counters using EggCounter so you can access information about the cache out of process.

One caveat is that it requires that your tasks return GObjects. It just made things simpler a the time. That may change though.

-- Christian Hergert 2015-05-13

Back to Index