Writing messages to (individual) files
I'm writing a small caching node, that takes in messages (which include an index) and provides a service that (given the index) returns the appropriate message.
I'm caching because storing all of these messages in memory is too expensive; my cache keeps a recent subset in memory, and writes the rest out to disk (and then does on-demand loading, should that be necessary).
I was doing this with a datatype that's easily serialized by hand, and uniquely-named files. I'm now adding more data to my cache type, and this will no longer work.
So: is there a straightforward way to say "Here's a message and a filename, dump the binary blob to that file"? Or is the correct way to generate a new bagfile (again, uniquely named) for each message I want to cache?
(Storing every message in one bagfile won't work, unless bags provide constant-time indexing, which I don't believe they do).