-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
102 lines (87 loc) · 3.4 KB
/
cache.go
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
package cachego
import (
"context"
"log"
"time"
"dario.cat/mergo"
"github.com/wasilak/cachego/config"
"github.com/wasilak/cachego/providers"
"go.opentelemetry.io/otel"
)
// The above code defines a CacheInterface type in Go that represents a cache with methods for
// initialization, getting and setting items, and managing item time-to-live (TTL).
// @property {error} Init - The Init method is used to initialize the cache. It can be used to set up
// any necessary data structures or connections required for caching.
// @property Get - The Get method retrieves an item from the cache based on the provided cache key. It
// returns the item, a boolean indicating whether the item was found in the cache, and an error if any
// occurred.
// @property {error} Set - The Set method is used to store an item in the cache. It takes a cacheKey
// string as the identifier for the item and the item itself as the value to be stored in the cache.
// @property GetItemTTL - GetItemTTL is a method that retrieves the time-to-live (TTL) value for a
// specific item in the cache. The TTL represents the amount of time that the item will remain in the
// cache before it expires and is automatically removed. The method returns the TTL value as a
// time.Duration,
// @property {error} ExtendTTL - ExtendTTL is a method that extends the time to live (TTL) of a cached
// item. It takes a cacheKey string and an item interface as parameters. The cacheKey is used to
// identify the cached item, and the item is the updated value that will be stored in the cache.
type CacheInterface interface {
Init() error
Get(cacheKey string) ([]byte, bool, error)
GetConfig() config.CacheGoConfig
Set(cacheKey string, item []byte) error
GetItemTTL(cacheKey string) (time.Duration, bool, error)
ExtendTTL(cacheKey string, item []byte) error
}
// The line `var CacheInstance CacheInterface` is declaring a variable named `CacheInstance` of type
// `CacheInterface`. This variable will be used to store an instance of a cache that implements the
// `CacheInterface` interface.
var CacheInstance CacheInterface
// The function `CacheInit` initializes and returns a cache instance based on the provided
// configuration.
func CacheInit(ctx context.Context, cacheConfig config.CacheGoConfig) (CacheInterface, error) {
tracer := otel.Tracer("Cache")
_, span := tracer.Start(ctx, "CacheInit")
defer span.End()
err := mergo.Merge(&config.DefaultConfig, cacheConfig, mergo.WithOverride)
if err != nil {
return nil, err
}
ttl, err := time.ParseDuration(config.DefaultConfig.Expiration)
if err != nil {
return nil, err
}
config.DefaultConfig.CTX = ctx
config.DefaultConfig.TTL = ttl
switch config.DefaultConfig.Type {
case "memory":
{
config.DefaultConfig.Tracer = otel.Tracer("GoCache")
CacheInstance = &providers.GoCache{
Config: config.DefaultConfig,
}
}
case "file", "badger":
{
config.DefaultConfig.Tracer = otel.Tracer("FileCache")
CacheInstance = &providers.BadgerCache{
Path: config.DefaultConfig.Path,
Config: config.DefaultConfig,
}
}
case "redis":
{
config.DefaultConfig.Tracer = otel.Tracer("RedisCache")
CacheInstance = &providers.RedisCache{
Address: config.DefaultConfig.RedisHost,
DB: config.DefaultConfig.RedisDB,
Config: config.DefaultConfig,
}
}
default:
{
log.Fatal("No cache type selected or cache type is invalid")
}
}
CacheInstance.Init()
return CacheInstance, nil
}