@@ -5,10 +5,27 @@ import (
5
5
"time"
6
6
7
7
"github.com/go-redis/redis/v8"
8
- "github.com/wasilak/elastauth/logger"
9
8
"golang.org/x/exp/slog"
10
9
)
11
10
11
+ // The RedisCache type represents a Redis cache with a specified time-to-live, context, address, and
12
+ // database.
13
+ // @property Cache - Cache is a pointer to a Redis client instance that is used to interact with the
14
+ // Redis cache.
15
+ // @property TTL - TTL stands for "Time To Live" and refers to the amount of time that a cached item
16
+ // will remain in the cache before it is considered expired and needs to be refreshed or removed. In
17
+ // the context of the RedisCache struct, it represents the duration of time that cached items will be
18
+ // stored in
19
+ // @property CTX - CTX is a context.Context object that is used to manage the lifecycle of a RedisCache
20
+ // instance. It is used to control the cancellation of operations and to pass values between functions.
21
+ // It is a part of the standard library in Go and is used extensively in network programming.
22
+ // @property {string} Address - Address is a string property that represents the network address of the
23
+ // Redis server. It typically includes the hostname or IP address of the server and the port number on
24
+ // which Redis is listening. For example, "localhost:6379" or "redis.example.com:6379".
25
+ // @property {int} DB - DB stands for "database" and is an integer value that represents the specific
26
+ // database within the Redis instance that the RedisCache struct will be interacting with. Redis allows
27
+ // for multiple databases to be created within a single instance, each with its own set of keys and
28
+ // values. The DB property allows the RedisCache
12
29
type RedisCache struct {
13
30
Cache * redis.Client
14
31
TTL time.Duration
@@ -17,6 +34,13 @@ type RedisCache struct {
17
34
DB int
18
35
}
19
36
37
+ // `func (c *RedisCache) Init(cacheDuration time.Duration)` is a method of the `RedisCache` struct that
38
+ // initializes a new Redis client instance and sets the cache duration (TTL) for the RedisCache
39
+ // instance. It takes a `time.Duration` parameter `cacheDuration` which represents the duration of time
40
+ // that cached items will be stored in the cache. The method creates a new Redis client instance using
41
+ // the `redis.NewClient` function and sets the `Cache` property of the `RedisCache` instance to the new
42
+ // client instance. It also sets the `CTX` property to a new `context.Background()` instance. Finally,
43
+ // it sets the `TTL` property of the `RedisCache` instance to the `cacheDuration` parameter.
20
44
func (c * RedisCache ) Init (cacheDuration time.Duration ) {
21
45
c .Cache = redis .NewClient (& redis.Options {
22
46
Addr : c .Address ,
@@ -28,36 +52,63 @@ func (c *RedisCache) Init(cacheDuration time.Duration) {
28
52
c .TTL = cacheDuration
29
53
}
30
54
55
+ // `func (c *RedisCache) GetTTL() time.Duration {` is a method of the `RedisCache` struct that returns
56
+ // the `TTL` property of the `RedisCache` instance, which represents the duration of time that cached
57
+ // items will be stored in the cache before they are considered expired and need to be refreshed or
58
+ // removed. The method returns a `time.Duration` value.
31
59
func (c * RedisCache ) GetTTL () time.Duration {
32
60
return c .TTL
33
61
}
34
62
63
+ // `func (c *RedisCache) Get(cacheKey string) (interface{}, bool)` is a method of the `RedisCache`
64
+ // struct that retrieves a cached item from the Redis cache using the specified `cacheKey`. It returns
65
+ // a tuple containing the cached item as an `interface{}` and a boolean value indicating whether the
66
+ // item was successfully retrieved from the cache or not. If the item is not found in the cache or an
67
+ // error occurs during retrieval, the method returns an empty `interface{}` and `false`.
35
68
func (c * RedisCache ) Get (cacheKey string ) (interface {}, bool ) {
36
69
item , err := c .Cache .Get (c .CTX , cacheKey ).Result ()
37
70
38
71
if err != nil || len (item ) == 0 {
39
- logger . LoggerInstance .Error ("Error" , slog .Any ("message" , err ))
72
+ slog .Error ("Error" , slog .Any ("message" , err ))
40
73
return item , false
41
74
}
42
75
43
76
return item , true
44
77
}
45
78
79
+ // `func (c *RedisCache) Set(cacheKey string, item interface{})` is a method of the `RedisCache` struct
80
+ // that sets a value in the Redis cache with the specified `cacheKey`. It takes two parameters:
81
+ // `cacheKey`, which is a string representing the key under which the value will be stored in the
82
+ // cache, and `item`, which is an interface{} representing the value to be stored. The method uses the
83
+ // `Set` function of the Redis client to set the value in the cache with the specified key and TTL
84
+ // (time-to-live) duration. If an error occurs during the set operation, it is logged using the
85
+ // `slog.Error` function.
46
86
func (c * RedisCache ) Set (cacheKey string , item interface {}) {
47
87
c .Cache .Set (c .CTX , cacheKey , item , c .TTL ).Err ()
48
88
}
49
89
90
+ // `func (c *RedisCache) GetItemTTL(cacheKey string) (time.Duration, bool)` is a method of the
91
+ // `RedisCache` struct that retrieves the time-to-live (TTL) duration of a cached item with the
92
+ // specified `cacheKey`. It returns a tuple containing the TTL duration as a `time.Duration` value and
93
+ // a boolean value indicating whether the TTL was successfully retrieved from the cache or not. If the
94
+ // TTL is not found in the cache or an error occurs during retrieval, the method returns a zero
95
+ // `time.Duration` value and `false`.
50
96
func (c * RedisCache ) GetItemTTL (cacheKey string ) (time.Duration , bool ) {
51
97
item , err := c .Cache .TTL (c .CTX , cacheKey ).Result ()
52
98
53
99
if err != nil {
54
- logger . LoggerInstance .Error ("Error" , slog .Any ("message" , err ))
100
+ slog .Error ("Error" , slog .Any ("message" , err ))
55
101
return item , false
56
102
}
57
103
58
104
return item , true
59
105
}
60
106
107
+ // `func (c *RedisCache) ExtendTTL(cacheKey string, item interface{})` is a method of the `RedisCache`
108
+ // struct that extends the time-to-live (TTL) duration of a cached item with the specified `cacheKey`.
109
+ // It uses the `Expire` function of the Redis client to set the TTL duration of the cached item to the
110
+ // value of the `TTL` property of the `RedisCache` instance. This method is useful for refreshing the
111
+ // TTL of a cached item to prevent it from expiring prematurely.
61
112
func (c * RedisCache ) ExtendTTL (cacheKey string , item interface {}) {
62
113
c .Cache .Expire (c .CTX , cacheKey , c .TTL )
63
114
}
0 commit comments