summaryrefslogtreecommitdiff
path: root/vendor/github.com/jackc/pgx/v5/internal/stmtcache/unlimited_cache.go
blob: f5f59396e4f1831ddc07a648bcbe48e827a79fc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package stmtcache

import (
	"math"

	"github.com/jackc/pgx/v5/pgconn"
)

// UnlimitedCache implements Cache with no capacity limit.
type UnlimitedCache struct {
	m            map[string]*pgconn.StatementDescription
	invalidStmts []*pgconn.StatementDescription
}

// NewUnlimitedCache creates a new UnlimitedCache.
func NewUnlimitedCache() *UnlimitedCache {
	return &UnlimitedCache{
		m: make(map[string]*pgconn.StatementDescription),
	}
}

// Get returns the statement description for sql. Returns nil if not found.
func (c *UnlimitedCache) Get(sql string) *pgconn.StatementDescription {
	return c.m[sql]
}

// Put stores sd in the cache. Put panics if sd.SQL is "". Put does nothing if sd.SQL already exists in the cache.
func (c *UnlimitedCache) Put(sd *pgconn.StatementDescription) {
	if sd.SQL == "" {
		panic("cannot store statement description with empty SQL")
	}

	if _, present := c.m[sd.SQL]; present {
		return
	}

	c.m[sd.SQL] = sd
}

// Invalidate invalidates statement description identified by sql. Does nothing if not found.
func (c *UnlimitedCache) Invalidate(sql string) {
	if sd, ok := c.m[sql]; ok {
		delete(c.m, sql)
		c.invalidStmts = append(c.invalidStmts, sd)
	}
}

// InvalidateAll invalidates all statement descriptions.
func (c *UnlimitedCache) InvalidateAll() {
	for _, sd := range c.m {
		c.invalidStmts = append(c.invalidStmts, sd)
	}

	c.m = make(map[string]*pgconn.StatementDescription)
}

func (c *UnlimitedCache) HandleInvalidated() []*pgconn.StatementDescription {
	invalidStmts := c.invalidStmts
	c.invalidStmts = nil
	return invalidStmts
}

// Len returns the number of cached prepared statement descriptions.
func (c *UnlimitedCache) Len() int {
	return len(c.m)
}

// Cap returns the maximum number of cached prepared statement descriptions.
func (c *UnlimitedCache) Cap() int {
	return math.MaxInt
}