1 // Package stmtcache is a cache for statement descriptions. 2 package stmtcache 3 4 import ( 5 "crypto/sha256" 6 "encoding/hex" 7 8 "github.com/jackc/pgx/v5/pgconn" 9 ) 10 11 // StatementName returns a statement name that will be stable for sql across multiple connections and program 12 // executions. 13 func StatementName(sql string) string { 14 digest := sha256.Sum256([]byte(sql)) 15 return "stmtcache_" + hex.EncodeToString(digest[0:24]) 16 } 17 18 // Cache caches statement descriptions. 19 type Cache interface { 20 // Get returns the statement description for sql. Returns nil if not found. 21 Get(sql string) *pgconn.StatementDescription 22 23 // Put stores sd in the cache. Put panics if sd.SQL is "". Put does nothing if sd.SQL already exists in the cache. 24 Put(sd *pgconn.StatementDescription) 25 26 // Invalidate invalidates statement description identified by sql. Does nothing if not found. 27 Invalidate(sql string) 28 29 // InvalidateAll invalidates all statement descriptions. 30 InvalidateAll() 31 32 // GetInvalidated returns a slice of all statement descriptions invalidated since the last call to RemoveInvalidated. 33 GetInvalidated() []*pgconn.StatementDescription 34 35 // RemoveInvalidated removes all invalidated statement descriptions. No other calls to Cache must be made between a 36 // call to GetInvalidated and RemoveInvalidated or RemoveInvalidated may remove statement descriptions that were 37 // never seen by the call to GetInvalidated. 38 RemoveInvalidated() 39 40 // Len returns the number of cached prepared statement descriptions. 41 Len() int 42 43 // Cap returns the maximum number of cached prepared statement descriptions. 44 Cap() int 45 } 46