| 1 |
package sturdyc |
| 2 |
|
| 3 |
type MetricsRecorder interface { |
| 4 |
// CacheHit is called for every key that results in a cache hit. |
| 5 |
CacheHit() |
| 6 |
// CacheMiss is called for every key that results in a cache miss. |
| 7 |
CacheMiss() |
| 8 |
// AsynchronousRefresh is called when a get operation results in an asynchronous refresh. |
| 9 |
AsynchronousRefresh() |
| 10 |
// SynchronousRefresh is called when a get operation results in a synchronous refresh. |
| 11 |
SynchronousRefresh() |
| 12 |
// MissingRecord is called every time the cache is asked to |
| 13 |
// look up a key which has been marked as missing. |
| 14 |
MissingRecord() |
| 15 |
// ForcedEviction is called when the cache reaches its capacity, and has to |
| 16 |
// evict keys in order to write a new one. |
| 17 |
ForcedEviction() |
| 18 |
// EntriesEvicted is called when the cache evicts keys from a shard. |
| 19 |
EntriesEvicted(int) |
| 20 |
// ShardIndex is called to report which shard it was that performed an operation. |
| 21 |
ShardIndex(int) |
| 22 |
// CacheBatchRefreshSize is called to report the size of the batch refresh. |
| 23 |
CacheBatchRefreshSize(size int) |
| 24 |
// ObserveCacheSize is called to report the size of the cache. |
| 25 |
ObserveCacheSize(callback func() int) |
| 26 |
} |
| 27 |
|
| 28 |
type DistributedMetricsRecorder interface { |
| 29 |
MetricsRecorder |
| 30 |
// DistributedCacheHit is called for every key that results in a cache hit. |
| 31 |
DistributedCacheHit() |
| 32 |
// DistributedCacheMiss is called for every key that results in a cache miss. |
| 33 |
DistributedCacheMiss() |
| 34 |
// DistributedRefresh is called when we retrieve a record from |
| 35 |
// the distributed storage that should be refreshed. |
| 36 |
DistributedRefresh() |
| 37 |
// DistributedMissingRecord is called when we retrieve a record from the |
| 38 |
// distributed storage that has been marked as a missing record. |
| 39 |
DistributedMissingRecord() |
| 40 |
// DistributedFallback is called when you are using a distributed storage |
| 41 |
// with early refreshes, and the call for a value was supposed to refresh it, |
| 42 |
// but the call failed. When that happens, the cache fallbacks to the latest |
| 43 |
// value from the distributed storage. |
| 44 |
DistributedFallback() |
| 45 |
} |
| 46 |
|
| 47 |
type distributedMetricsRecorder struct { |
| 48 |
MetricsRecorder |
| 49 |
} |
| 50 |
|
| 51 |
func (d *distributedMetricsRecorder) DistributedCacheHit() {} |
| 52 |
|
| 53 |
func (d *distributedMetricsRecorder) DistributedCacheMiss() {} |
| 54 |
|
| 55 |
func (d *distributedMetricsRecorder) DistributedRefresh() {} |
| 56 |
|
| 57 |
func (d *distributedMetricsRecorder) DistributedMissingRecord() {} |
| 58 |
|
| 59 |
func (d *distributedMetricsRecorder) DistributedFallback() {} |
| 60 |
|
| 61 |
func (s *shard[T]) reportForcedEviction() { |
| 62 |
if s.metricsRecorder == nil { |
| 63 |
return |
| 64 |
} |
| 65 |
s.metricsRecorder.ForcedEviction() |
| 66 |
} |
| 67 |
|
| 68 |
func (s *shard[T]) reportEntriesEvicted(n int) { |
| 69 |
if s.metricsRecorder == nil { |
| 70 |
return |
| 71 |
} |
| 72 |
s.metricsRecorder.EntriesEvicted(n) |
| 73 |
} |
| 74 |
|
| 75 |
// reportCacheHits is used to report cache hits and misses to the metrics recorder. |
| 76 |
func (c *Client[T]) reportCacheHits(cacheHit, missingRecord, asyncRefresh, syncRefresh bool) { |
| 77 |
if c.metricsRecorder == nil { |
| 78 |
return |
| 79 |
} |
| 80 |
|
| 81 |
if missingRecord { |
| 82 |
c.metricsRecorder.MissingRecord() |
| 83 |
} |
| 84 |
|
| 85 |
if asyncRefresh { |
| 86 |
c.metricsRecorder.AsynchronousRefresh() |
| 87 |
} |
| 88 |
|
| 89 |
if syncRefresh { |
| 90 |
c.metricsRecorder.SynchronousRefresh() |
| 91 |
} |
| 92 |
|
| 93 |
if !cacheHit { |
| 94 |
c.metricsRecorder.CacheMiss() |
| 95 |
return |
| 96 |
} |
| 97 |
c.metricsRecorder.CacheHit() |
| 98 |
} |
| 99 |
|
| 100 |
func (c *Client[T]) reportShardIndex(index int) { |
| 101 |
if c.metricsRecorder == nil { |
| 102 |
return |
| 103 |
} |
| 104 |
c.metricsRecorder.ShardIndex(index) |
| 105 |
} |
| 106 |
|
| 107 |
func (c *Client[T]) reportBatchRefreshSize(n int) { |
| 108 |
if c.metricsRecorder == nil { |
| 109 |
return |
| 110 |
} |
| 111 |
c.metricsRecorder.CacheBatchRefreshSize(n) |
| 112 |
} |
| 113 |
|
| 114 |
func (c *Client[T]) reportDistributedCacheHit(cacheHit bool) { |
| 115 |
if c.metricsRecorder == nil { |
| 116 |
return |
| 117 |
} |
| 118 |
if !cacheHit { |
| 119 |
c.metricsRecorder.DistributedCacheMiss() |
| 120 |
return |
| 121 |
} |
| 122 |
c.metricsRecorder.DistributedCacheHit() |
| 123 |
} |
| 124 |
|
| 125 |
func (c *Client[T]) reportDistributedRefresh() { |
| 126 |
if c.metricsRecorder == nil { |
| 127 |
return |
| 128 |
} |
| 129 |
c.metricsRecorder.DistributedRefresh() |
| 130 |
} |
| 131 |
|
| 132 |
func (c *Client[T]) reportDistributedMissingRecord() { |
| 133 |
if c.metricsRecorder == nil { |
| 134 |
return |
| 135 |
} |
| 136 |
c.metricsRecorder.DistributedMissingRecord() |
| 137 |
} |
| 138 |
|
| 139 |
func (c *Client[T]) reportDistributedStaleFallback() { |
| 140 |
if c.metricsRecorder == nil { |
| 141 |
return |
| 142 |
} |
| 143 |
c.metricsRecorder.DistributedFallback() |
| 144 |
} |