summaryrefslogtreecommitdiff
path: root/registry-scanner/pkg/cache/memcache.go
blob: 03b4080179fc98324195b47ef12210e4204600d3 (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
72
73
74
package cache

import (
	"fmt"

	"github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/tag"

	memcache "github.com/patrickmn/go-cache"
)

type MemCache struct {
	cache *memcache.Cache
}

// NewMemCache returns a new instance of MemCache
func NewMemCache() ImageTagCache {
	mc := MemCache{}
	c := memcache.New(0, 0)
	mc.cache = c
	return &mc
}

// HasTag returns true if cache has entry for given tag, false if not
func (mc *MemCache) HasTag(imageName string, tagName string) bool {
	tag, err := mc.GetTag(imageName, tagName)
	if err != nil || tag == nil {
		return false
	} else {
		return true
	}
}

// SetTag sets a tag entry into the cache
func (mc *MemCache) SetTag(imageName string, imgTag *tag.ImageTag) {
	mc.cache.Set(tagCacheKey(imageName, imgTag.TagName), *imgTag, -1)
}

// GetTag gets a tag entry from the cache
func (mc *MemCache) GetTag(imageName string, tagName string) (*tag.ImageTag, error) {
	var imgTag tag.ImageTag
	e, ok := mc.cache.Get(tagCacheKey(imageName, tagName))
	if !ok {
		return nil, nil
	}
	imgTag, ok = e.(tag.ImageTag)
	if !ok {
		return nil, fmt.Errorf("")
	}
	return &imgTag, nil
}

func (mc *MemCache) SetImage(imageName, application string) {
	mc.cache.Set(imageCacheKey(imageName), application, -1)
}

// ClearCache clears the cache
func (mc *MemCache) ClearCache() {
	for k := range mc.cache.Items() {
		mc.cache.Delete(k)
	}
}

// NumEntries returns the number of entries in the cache
func (mc *MemCache) NumEntries() int {
	return mc.cache.ItemCount()
}

func tagCacheKey(imageName, imageTag string) string {
	return fmt.Sprintf("tags:%s:%s", imageName, imageTag)
}

func imageCacheKey(imageName string) string {
	return fmt.Sprintf("image:%s", imageName)
}