I really think you're better off just using BerkeleyDB all the time if you know you'll have to use it sometimes. It will buffer the data in shared memory as much as possible, so while it won't be as fast as a perl hash, it won't be as slow as writing to disk either.