summaryrefslogtreecommitdiffstats
path: root/modules/cache/string_cache.go
blob: 4f659616f501e87b20e480fdc2d5d3c93b016922 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package cache

import (
	"errors"
	"strings"

	"code.gitea.io/gitea/modules/json"
	"code.gitea.io/gitea/modules/setting"
	"code.gitea.io/gitea/modules/util"

	chi_cache "gitea.com/go-chi/cache" //nolint:depguard
)

type GetJSONError struct {
	err         error
	cachedError string // Golang error can't be stored in cache, only the string message could be stored
}

func (e *GetJSONError) ToError() error {
	if e.err != nil {
		return e.err
	}
	return errors.New("cached error: " + e.cachedError)
}

type StringCache interface {
	Ping() error

	Get(key string) (string, bool)
	Put(key, value string, ttl int64) error
	Delete(key string) error
	IsExist(key string) bool

	PutJSON(key string, v any, ttl int64) error
	GetJSON(key string, ptr any) (exist bool, err *GetJSONError)

	ChiCache() chi_cache.Cache
}

type stringCache struct {
	chiCache chi_cache.Cache
}

func NewStringCache(cacheConfig setting.Cache) (StringCache, error) {
	adapter := util.IfZero(cacheConfig.Adapter, "memory")
	interval := util.IfZero(cacheConfig.Interval, 60)
	cc, err := chi_cache.NewCacher(chi_cache.Options{
		Adapter:       adapter,
		AdapterConfig: cacheConfig.Conn,
		Interval:      interval,
	})
	if err != nil {
		return nil, err
	}
	return &stringCache{chiCache: cc}, nil
}

func (sc *stringCache) Ping() error {
	return sc.chiCache.Ping()
}

func (sc *stringCache) Get(key string) (string, bool) {
	v := sc.chiCache.Get(key)
	if v == nil {
		return "", false
	}
	s, ok := v.(string)
	return s, ok
}

func (sc *stringCache) Put(key, value string, ttl int64) error {
	return sc.chiCache.Put(key, value, ttl)
}

func (sc *stringCache) Delete(key string) error {
	return sc.chiCache.Delete(key)
}

func (sc *stringCache) IsExist(key string) bool {
	return sc.chiCache.IsExist(key)
}

const cachedErrorPrefix = "<CACHED-ERROR>:"

func (sc *stringCache) PutJSON(key string, v any, ttl int64) error {
	var s string
	switch v := v.(type) {
	case error:
		s = cachedErrorPrefix + v.Error()
	default:
		b, err := json.Marshal(v)
		if err != nil {
			return err
		}
		s = util.UnsafeBytesToString(b)
	}
	return sc.chiCache.Put(key, s, ttl)
}

func (sc *stringCache) GetJSON(key string, ptr any) (exist bool, getErr *GetJSONError) {
	s, ok := sc.Get(key)
	if !ok || s == "" {
		return false, nil
	}
	s, isCachedError := strings.CutPrefix(s, cachedErrorPrefix)
	if isCachedError {
		return true, &GetJSONError{cachedError: s}
	}
	if err := json.Unmarshal(util.UnsafeStringToBytes(s), ptr); err != nil {
		return false, &GetJSONError{err: err}
	}
	return true, nil
}

func (sc *stringCache) ChiCache() chi_cache.Cache {
	return sc.chiCache
}