1package pages
2
3import (
4 "sync"
5)
6
7type TmplCache[K comparable, V any] struct {
8 data map[K]V
9 mutex sync.RWMutex
10}
11
12func NewTmplCache[K comparable, V any]() *TmplCache[K, V] {
13 return &TmplCache[K, V]{
14 data: make(map[K]V),
15 }
16}
17
18func (c *TmplCache[K, V]) Get(key K) (V, bool) {
19 c.mutex.RLock()
20 defer c.mutex.RUnlock()
21 val, exists := c.data[key]
22 return val, exists
23}
24
25func (c *TmplCache[K, V]) Set(key K, value V) {
26 c.mutex.Lock()
27 defer c.mutex.Unlock()
28 c.data[key] = value
29}
30
31func (c *TmplCache[K, V]) Size() int {
32 c.mutex.RLock()
33 defer c.mutex.RUnlock()
34 return len(c.data)
35}