Skip to content
Advertisement

Fastest way to cast Java Objects from memcache

We have a webapp which uses memcache to store objects temporally. We are adding background jobs to cleanup the cache (not based on time based expiration that comes with memcache). Since we have multiple instances of the webapp running, we want to make sure that only one instance is doing the cleanup at any point. So we decided to keep a flag in memcached allowing only one instance to run. Now the question.

I am just storing a flag, it could be an int, String, boolean, heck…even just a char. However queries from memcached returns Objects, which will have to casted to whatever type I pick. If I pick boolean than the object will have to be casted to lang.Boolean which might be more work than just a String.

Which type would you pick?

Thanks.

Advertisement

Answer

The type is pretty much irrelevant. You might want to try encoding it as null/not-null. That way you don’t have to cast.

It probably will amount to a rounding error on the total time of the call, especially with a cache look up. If it’s a big deal, I’d look to reducing the number of times it’s called rather than shorting the execution time.

Advertisement