Caching that's multi-process safe
Develop a general procedure (or software) for implementing a cache that can be shared by several processes. There seems to be no readily available library that implements caching. It certainly needs to work for Unix; Windows suport would be good, but i of less importance because the OC library may take over or client role on that platform.
We have several cases where files need to be cached, both in our server software and in the client code. These include at least: files that have been decompressed and responses from the web that can be cached using the HTTP/1.1 rules. We need for the caching to be both thread safe and multi-process safe. That is, the cache needs to be shared between processes that do not, otherwise, synchronize their operation.
3 Proposed solution
3.1 There are three basic operations that must be implemented
- Read an existing file
- Write a new file
- Delete a file
- The size of the cache can only change by the number of active processes using the cache (at any given instance).
- The size of the cache will likely change only by the average size of a cached object times some constant that's less than or equal to the number of processes.
- We accept as tolerable that the cache might grow beyond its bounds by a 'little bit' but that's an OK tradeoff if we don't have to lock the cache to count its size.
- We will count the size of the cache before every attempt to write a new item.
- We can store the size of the cache in a file and access that using an exclusive lock (but that might make item #3 moot, because then we always know the exact size of the cache).