1package i18n
2
3import "sync"
4
5// Cache provides thread-safe caching for translated strings.
6type Cache struct {
7 items map[string]string
8 mu sync.RWMutex
9}
10
11// NewCache creates a new Cache.
12func NewCache() *Cache {
13 return &Cache{
14 items: make(map[string]string),
15 }
16}
17
18// Get retrieves a cached value.
19func (c *Cache) Get(key string) (string, bool) {
20 c.mu.RLock()
21 defer c.mu.RUnlock()
22
23 val, ok := c.items[key]
24 return val, ok
25}
26
27// Set stores a value in the cache.
28func (c *Cache) Set(key, value string) {
29 c.mu.Lock()
30 defer c.mu.Unlock()
31
32 c.items[key] = value
33}
34
35// Clear removes all cached values.
36func (c *Cache) Clear() {
37 c.mu.Lock()
38 defer c.mu.Unlock()
39
40 c.items = make(map[string]string)
41}
42
43// Size returns the number of cached items.
44func (c *Cache) Size() int {
45 c.mu.RLock()
46 defer c.mu.RUnlock()
47
48 return len(c.items)
49}
50
51// Delete removes a specific key from the cache.
52func (c *Cache) Delete(key string) {
53 c.mu.Lock()
54 defer c.mu.Unlock()
55
56 delete(c.items, key)
57}
58
59// Has checks if a key exists in the cache.
60func (c *Cache) Has(key string) bool {
61 c.mu.RLock()
62 defer c.mu.RUnlock()
63
64 _, ok := c.items[key]
65 return ok
66}