Record and publish cache and pool statistics using a consistent structure.
Systems that make heavy use of caching and pooling facilities can significantly reduce database overhead. Various statistics on the cache can provide a measure of how effective these optimizations are and provide clues for further changes and debugging. A common statistic is the number of cache hits, that is the number of times data has been found in the cache without requiring additional database operations. Another common statistic is pool size where under normal usage, a pool should not grow continuously.
During development it may be sufficient to dump this information to a log file, but ultimately access will be required by way of an interface. Data obtained from this interface can then be processed as required - data can be displayed, emailed, persisted in the database, etc.
Cache Statistics pattern defines a common interface that enable multiple cache and pool implementations to record and publish their details using a consistent structure.
The following figure illustrates the static structure for the Cache Statistics pattern :
The IStatistics interface represents a single set of cache or pool statistics. It defines accessor operations to retrieve specific statistics. ConcreteStatistics implements this interface and is directly coupled (i.e., contained) to a specific cache or pool instance. It is therefore, the cache or pool's responsibility to maintain the accuracy and currency of the statistics contained by ConcreteStatistics. Cache and pool implementations normally define specific ConcreteStatistics objects that directly reflect the relevant information. Clients interact with these object directly via the IStatistics interface.
As explained in the Strategy section, statistics can be either current or snapshot. Consider using the modified structure below if clients require both current and snapshot statistics:
SnapshotStatistics is the default IStatistics implementation whose attributes are assigned during initialization. The ICurrentStatistics interface extends the IStatistics interface adding the GetSnapshot method to return a SnapshotStatistics object on request.
Use this pattern when:
When you publish cache or pool statistics, you can designate them as current or snapshot:
The information that Cache Statistics maintain is helpful when debugging any of the previous cache patterns: Cache Collector, Cache Accessor, Demand Cache, and Primed Cache.