summaryrefslogtreecommitdiffstats
path: root/modules/cache/cache.go
blob: 2148e028d56b5d103f879f5869794304a91b40f7 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2017 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package cache

import (
	"fmt"
	"strconv"
	"time"

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

	mc "code.forgejo.org/go-chi/cache"

	_ "code.forgejo.org/go-chi/cache/memcache" // memcache plugin for cache
)

var conn mc.Cache

func newCache(cacheConfig setting.Cache) (mc.Cache, error) {
	return mc.NewCacher(mc.Options{
		Adapter:       cacheConfig.Adapter,
		AdapterConfig: cacheConfig.Conn,
		Interval:      cacheConfig.Interval,
	})
}

// Init start cache service
func Init() error {
	var err error

	if conn == nil {
		if conn, err = newCache(setting.CacheService.Cache); err != nil {
			return err
		}
		if err = conn.Ping(); err != nil {
			return err
		}
	}

	return err
}

const (
	testCacheKey       = "DefaultCache.TestKey"
	SlowCacheThreshold = 100 * time.Microsecond
)

func Test() (time.Duration, error) {
	if conn == nil {
		return 0, fmt.Errorf("default cache not initialized")
	}

	testData := fmt.Sprintf("%x", make([]byte, 500))

	start := time.Now()

	if err := conn.Delete(testCacheKey); err != nil {
		return 0, fmt.Errorf("expect cache to delete data based on key if exist but got: %w", err)
	}
	if err := conn.Put(testCacheKey, testData, 10); err != nil {
		return 0, fmt.Errorf("expect cache to store data but got: %w", err)
	}
	testVal := conn.Get(testCacheKey)
	if testVal == nil {
		return 0, fmt.Errorf("expect cache hit but got none")
	}
	if testVal != testData {
		return 0, fmt.Errorf("expect cache to return same value as stored but got other")
	}

	return time.Since(start), nil
}

// GetCache returns the currently configured cache
func GetCache() mc.Cache {
	return conn
}

// GetString returns the key value from cache with callback when no key exists in cache
func GetString(key string, getFunc func() (string, error)) (string, error) {
	if conn == nil || setting.CacheService.TTL == 0 {
		return getFunc()
	}

	cached := conn.Get(key)

	if cached == nil {
		value, err := getFunc()
		if err != nil {
			return value, err
		}
		return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
	}

	if value, ok := cached.(string); ok {
		return value, nil
	}

	if stringer, ok := cached.(fmt.Stringer); ok {
		return stringer.String(), nil
	}

	return fmt.Sprintf("%s", cached), nil
}

// GetInt returns key value from cache with callback when no key exists in cache
func GetInt(key string, getFunc func() (int, error)) (int, error) {
	if conn == nil || setting.CacheService.TTL == 0 {
		return getFunc()
	}

	cached := conn.Get(key)

	if cached == nil {
		value, err := getFunc()
		if err != nil {
			return value, err
		}

		return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
	}

	switch v := cached.(type) {
	case int:
		return v, nil
	case string:
		value, err := strconv.Atoi(v)
		if err != nil {
			return 0, err
		}
		return value, nil
	default:
		value, err := getFunc()
		if err != nil {
			return value, err
		}
		return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
	}
}

// GetInt64 returns key value from cache with callback when no key exists in cache
func GetInt64(key string, getFunc func() (int64, error)) (int64, error) {
	if conn == nil || setting.CacheService.TTL == 0 {
		return getFunc()
	}

	cached := conn.Get(key)

	if cached == nil {
		value, err := getFunc()
		if err != nil {
			return value, err
		}

		return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
	}

	switch v := conn.Get(key).(type) {
	case int64:
		return v, nil
	case string:
		value, err := strconv.ParseInt(v, 10, 64)
		if err != nil {
			return 0, err
		}
		return value, nil
	default:
		value, err := getFunc()
		if err != nil {
			return value, err
		}

		return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
	}
}

// Remove key from cache
func Remove(key string) {
	if conn == nil {
		return
	}
	_ = conn.Delete(key)
}