The following is a list of recommendations on how a global cache should be used:
- Only cache immutable objects.
- Use serializable keys and values whenever possible. At the very least the keys should be serializable.
- Use a cache loader to populate the cache. This allows the cache to take advantage of the fine grain concurrency optimizations built into the cache provider and it does not require the user to be concerned with managing concurrent access to the cache.
- The loading of a cache without a loader (using get() and put() calls) must be avoided for two main reasons:
- Concurrency management - In this case the user is responsible for managing concurrent access to the cache while the cache is loading. The user has two choices:
- Control concurrent access to the get() and put() block of code – this approach is not recommended in a performance sensitive part of the application but it offers the guarantee that on object is only loaded once.
- Allow concurrent access to the get() and put() block of code – this approach supports higher concurrency but an object might be loaded more than once by different threads.
- Efficient data management - without a cache loader, the cache must be pre-populated with all data. With a cache loader, only required data is pulled into the cache.
- Use cache names that are prefixed with a package name unique to your project. For instance curam.cpm.myCache would be a suitable name for a cache in the Curam Provider Management™ project.