github.com/jackc/pgx/v5@v5.5.5/internal/stmtcache/unlimited_cache.go (about) 1 package stmtcache 2 3 import ( 4 "math" 5 6 "github.com/jackc/pgx/v5/pgconn" 7 ) 8 9 // UnlimitedCache implements Cache with no capacity limit. 10 type UnlimitedCache struct { 11 m map[string]*pgconn.StatementDescription 12 invalidStmts []*pgconn.StatementDescription 13 } 14 15 // NewUnlimitedCache creates a new UnlimitedCache. 16 func NewUnlimitedCache() *UnlimitedCache { 17 return &UnlimitedCache{ 18 m: make(map[string]*pgconn.StatementDescription), 19 } 20 } 21 22 // Get returns the statement description for sql. Returns nil if not found. 23 func (c *UnlimitedCache) Get(sql string) *pgconn.StatementDescription { 24 return c.m[sql] 25 } 26 27 // Put stores sd in the cache. Put panics if sd.SQL is "". Put does nothing if sd.SQL already exists in the cache. 28 func (c *UnlimitedCache) Put(sd *pgconn.StatementDescription) { 29 if sd.SQL == "" { 30 panic("cannot store statement description with empty SQL") 31 } 32 33 if _, present := c.m[sd.SQL]; present { 34 return 35 } 36 37 c.m[sd.SQL] = sd 38 } 39 40 // Invalidate invalidates statement description identified by sql. Does nothing if not found. 41 func (c *UnlimitedCache) Invalidate(sql string) { 42 if sd, ok := c.m[sql]; ok { 43 delete(c.m, sql) 44 c.invalidStmts = append(c.invalidStmts, sd) 45 } 46 } 47 48 // InvalidateAll invalidates all statement descriptions. 49 func (c *UnlimitedCache) InvalidateAll() { 50 for _, sd := range c.m { 51 c.invalidStmts = append(c.invalidStmts, sd) 52 } 53 54 c.m = make(map[string]*pgconn.StatementDescription) 55 } 56 57 // GetInvalidated returns a slice of all statement descriptions invalidated since the last call to RemoveInvalidated. 58 func (c *UnlimitedCache) GetInvalidated() []*pgconn.StatementDescription { 59 return c.invalidStmts 60 } 61 62 // RemoveInvalidated removes all invalidated statement descriptions. No other calls to Cache must be made between a 63 // call to GetInvalidated and RemoveInvalidated or RemoveInvalidated may remove statement descriptions that were 64 // never seen by the call to GetInvalidated. 65 func (c *UnlimitedCache) RemoveInvalidated() { 66 c.invalidStmts = nil 67 } 68 69 // Len returns the number of cached prepared statement descriptions. 70 func (c *UnlimitedCache) Len() int { 71 return len(c.m) 72 } 73 74 // Cap returns the maximum number of cached prepared statement descriptions. 75 func (c *UnlimitedCache) Cap() int { 76 return math.MaxInt 77 }