File: cache.go

package info (click to toggle)
gitlab-agent 16.11.5-1
  • links: PTS, VCS
  • area: contrib
  • in suites: experimental
  • size: 7,072 kB
  • sloc: makefile: 193; sh: 55; ruby: 3
file content (85 lines) | stat: -rw-r--r-- 1,781 bytes parent folder | download | duplicates (2)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package cache

import (
	"sync"
	"time"

	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/syncz"
)

type Entry[V any] struct {
	// protects state in this object.
	syncz.Mutex
	// Expires holds the time when this entry should be removed from the cache.
	Expires time.Time
	// Item is the cached item.
	Item    V
	HasItem bool
}

func (e *Entry[V]) IsNeedRefreshLocked() bool {
	return !e.HasItem || e.IsExpiredLocked(time.Now())
}

func (e *Entry[V]) IsExpiredLocked(t time.Time) bool {
	return e.Expires.Before(t)
}

type Cache[K comparable, V any] struct {
	mu                    sync.Mutex
	data                  map[K]*Entry[V]
	expirationCheckPeriod time.Duration
	nextExpirationCheck   time.Time
}

func New[K comparable, V any](expirationCheckPeriod time.Duration) *Cache[K, V] {
	return &Cache[K, V]{
		data:                  make(map[K]*Entry[V]),
		expirationCheckPeriod: expirationCheckPeriod,
	}
}

func (c *Cache[K, V]) EvictExpiredEntries() {
	c.mu.Lock()
	defer c.mu.Unlock()
	now := time.Now()
	if now.Before(c.nextExpirationCheck) {
		return
	}
	c.nextExpirationCheck = now.Add(c.expirationCheckPeriod)
	for key, entry := range c.data {
		func() {
			if !entry.TryLock() {
				// entry is busy, skip
				return
			}
			defer entry.Unlock()
			if entry.IsExpiredLocked(now) {
				delete(c.data, key)
			}
		}()
	}
}

func (c *Cache[K, V]) GetOrCreateCacheEntry(key K) *Entry[V] {
	c.mu.Lock()
	defer c.mu.Unlock()
	entry := c.data[key]
	if entry != nil {
		return entry
	}
	entry = &Entry[V]{
		Mutex: syncz.NewMutex(),
	}
	c.data[key] = entry
	return entry
}

func (c *Cache[K, V]) EvictEntry(key K, entry *Entry[V]) {
	c.mu.Lock()
	defer c.mu.Unlock()
	existingEntry := c.data[key]
	if existingEntry == entry {
		delete(c.data, key)
	}
}