lru_cache.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package stmtcache
  2. import (
  3. "container/list"
  4. "github.com/jackc/pgx/v5/pgconn"
  5. )
  6. // LRUCache implements Cache with a Least Recently Used (LRU) cache.
  7. type LRUCache struct {
  8. cap int
  9. m map[string]*list.Element
  10. l *list.List
  11. invalidStmts []*pgconn.StatementDescription
  12. invalidSet map[string]struct{}
  13. }
  14. // NewLRUCache creates a new LRUCache. cap is the maximum size of the cache.
  15. func NewLRUCache(cap int) *LRUCache {
  16. return &LRUCache{
  17. cap: cap,
  18. m: make(map[string]*list.Element),
  19. l: list.New(),
  20. invalidSet: make(map[string]struct{}),
  21. }
  22. }
  23. // Get returns the statement description for sql. Returns nil if not found.
  24. func (c *LRUCache) Get(key string) *pgconn.StatementDescription {
  25. if el, ok := c.m[key]; ok {
  26. c.l.MoveToFront(el)
  27. return el.Value.(*pgconn.StatementDescription)
  28. }
  29. return nil
  30. }
  31. // Put stores sd in the cache. Put panics if sd.SQL is "". Put does nothing if sd.SQL already exists in the cache or
  32. // sd.SQL has been invalidated and HandleInvalidated has not been called yet.
  33. func (c *LRUCache) Put(sd *pgconn.StatementDescription) {
  34. if sd.SQL == "" {
  35. panic("cannot store statement description with empty SQL")
  36. }
  37. if _, present := c.m[sd.SQL]; present {
  38. return
  39. }
  40. // The statement may have been invalidated but not yet handled. Do not readd it to the cache.
  41. if _, invalidated := c.invalidSet[sd.SQL]; invalidated {
  42. return
  43. }
  44. if c.l.Len() == c.cap {
  45. c.invalidateOldest()
  46. }
  47. el := c.l.PushFront(sd)
  48. c.m[sd.SQL] = el
  49. }
  50. // Invalidate invalidates statement description identified by sql. Does nothing if not found.
  51. func (c *LRUCache) Invalidate(sql string) {
  52. if el, ok := c.m[sql]; ok {
  53. delete(c.m, sql)
  54. sd := el.Value.(*pgconn.StatementDescription)
  55. c.invalidStmts = append(c.invalidStmts, sd)
  56. c.invalidSet[sql] = struct{}{}
  57. c.l.Remove(el)
  58. }
  59. }
  60. // InvalidateAll invalidates all statement descriptions.
  61. func (c *LRUCache) InvalidateAll() {
  62. el := c.l.Front()
  63. for el != nil {
  64. sd := el.Value.(*pgconn.StatementDescription)
  65. c.invalidStmts = append(c.invalidStmts, sd)
  66. c.invalidSet[sd.SQL] = struct{}{}
  67. el = el.Next()
  68. }
  69. c.m = make(map[string]*list.Element)
  70. c.l = list.New()
  71. }
  72. // GetInvalidated returns a slice of all statement descriptions invalidated since the last call to RemoveInvalidated.
  73. func (c *LRUCache) GetInvalidated() []*pgconn.StatementDescription {
  74. return c.invalidStmts
  75. }
  76. // RemoveInvalidated removes all invalidated statement descriptions. No other calls to Cache must be made between a
  77. // call to GetInvalidated and RemoveInvalidated or RemoveInvalidated may remove statement descriptions that were
  78. // never seen by the call to GetInvalidated.
  79. func (c *LRUCache) RemoveInvalidated() {
  80. c.invalidStmts = nil
  81. c.invalidSet = make(map[string]struct{})
  82. }
  83. // Len returns the number of cached prepared statement descriptions.
  84. func (c *LRUCache) Len() int {
  85. return c.l.Len()
  86. }
  87. // Cap returns the maximum number of cached prepared statement descriptions.
  88. func (c *LRUCache) Cap() int {
  89. return c.cap
  90. }
  91. func (c *LRUCache) invalidateOldest() {
  92. oldest := c.l.Back()
  93. sd := oldest.Value.(*pgconn.StatementDescription)
  94. c.invalidStmts = append(c.invalidStmts, sd)
  95. c.invalidSet[sd.SQL] = struct{}{}
  96. delete(c.m, sd.SQL)
  97. c.l.Remove(oldest)
  98. }