...
1 package stmtcache
2
3 import (
4 "container/list"
5
6 "github.com/jackc/pgx/v5/pgconn"
7 )
8
9
10 type LRUCache struct {
11 cap int
12 m map[string]*list.Element
13 l *list.List
14 invalidStmts []*pgconn.StatementDescription
15 }
16
17
18 func NewLRUCache(cap int) *LRUCache {
19 return &LRUCache{
20 cap: cap,
21 m: make(map[string]*list.Element),
22 l: list.New(),
23 }
24 }
25
26
27 func (c *LRUCache) Get(key string) *pgconn.StatementDescription {
28 if el, ok := c.m[key]; ok {
29 c.l.MoveToFront(el)
30 return el.Value.(*pgconn.StatementDescription)
31 }
32
33 return nil
34
35 }
36
37
38
39 func (c *LRUCache) Put(sd *pgconn.StatementDescription) {
40 if sd.SQL == "" {
41 panic("cannot store statement description with empty SQL")
42 }
43
44 if _, present := c.m[sd.SQL]; present {
45 return
46 }
47
48
49 for _, invalidSD := range c.invalidStmts {
50 if invalidSD.SQL == sd.SQL {
51 return
52 }
53 }
54
55 if c.l.Len() == c.cap {
56 c.invalidateOldest()
57 }
58
59 el := c.l.PushFront(sd)
60 c.m[sd.SQL] = el
61 }
62
63
64 func (c *LRUCache) Invalidate(sql string) {
65 if el, ok := c.m[sql]; ok {
66 delete(c.m, sql)
67 c.invalidStmts = append(c.invalidStmts, el.Value.(*pgconn.StatementDescription))
68 c.l.Remove(el)
69 }
70 }
71
72
73 func (c *LRUCache) InvalidateAll() {
74 el := c.l.Front()
75 for el != nil {
76 c.invalidStmts = append(c.invalidStmts, el.Value.(*pgconn.StatementDescription))
77 el = el.Next()
78 }
79
80 c.m = make(map[string]*list.Element)
81 c.l = list.New()
82 }
83
84
85 func (c *LRUCache) GetInvalidated() []*pgconn.StatementDescription {
86 return c.invalidStmts
87 }
88
89
90
91
92 func (c *LRUCache) RemoveInvalidated() {
93 c.invalidStmts = nil
94 }
95
96
97 func (c *LRUCache) Len() int {
98 return c.l.Len()
99 }
100
101
102 func (c *LRUCache) Cap() int {
103 return c.cap
104 }
105
106 func (c *LRUCache) invalidateOldest() {
107 oldest := c.l.Back()
108 sd := oldest.Value.(*pgconn.StatementDescription)
109 c.invalidStmts = append(c.invalidStmts, sd)
110 delete(c.m, sd.SQL)
111 c.l.Remove(oldest)
112 }
113
View as plain text