stats.go 1003 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package gcache
  2. import (
  3. "sync/atomic"
  4. )
  5. type statsAccessor interface {
  6. HitCount() uint64
  7. MissCount() uint64
  8. LookupCount() uint64
  9. HitRate() float64
  10. }
  11. // statistics
  12. type stats struct {
  13. hitCount uint64
  14. missCount uint64
  15. }
  16. // increment hit count
  17. func (st *stats) IncrHitCount() uint64 {
  18. return atomic.AddUint64(&st.hitCount, 1)
  19. }
  20. // increment miss count
  21. func (st *stats) IncrMissCount() uint64 {
  22. return atomic.AddUint64(&st.missCount, 1)
  23. }
  24. // HitCount returns hit count
  25. func (st *stats) HitCount() uint64 {
  26. return atomic.LoadUint64(&st.hitCount)
  27. }
  28. // MissCount returns miss count
  29. func (st *stats) MissCount() uint64 {
  30. return atomic.LoadUint64(&st.missCount)
  31. }
  32. // LookupCount returns lookup count
  33. func (st *stats) LookupCount() uint64 {
  34. return st.HitCount() + st.MissCount()
  35. }
  36. // HitRate returns rate for cache hitting
  37. func (st *stats) HitRate() float64 {
  38. hc, mc := st.HitCount(), st.MissCount()
  39. total := hc + mc
  40. if total == 0 {
  41. return 0.0
  42. }
  43. return float64(hc) / float64(total)
  44. }