Skip to content

Commit 7ef4515

Browse files
committed
added docs
1 parent bd87e05 commit 7ef4515

File tree

11 files changed

+259
-22
lines changed

11 files changed

+259
-22
lines changed

cache/cache.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ import (
77
"github.com/spf13/viper"
88
)
99

10+
// The CacheInterface defines methods for initializing, getting, setting, and extending the
11+
// time-to-live (TTL) of cached items.
12+
// @property Init - Init is a method that initializes the cache with a specified cache duration. It
13+
// takes a time.Duration parameter that represents the duration for which the cache items should be
14+
// stored.
15+
// @property Get - Get is a method of the CacheInterface that takes a cacheKey string as input and
16+
// returns an interface{} and a bool. The interface{} represents the cached item associated with the
17+
// cacheKey, and the bool indicates whether the item was found in the cache or not.
18+
// @property Set - Set is a method of the CacheInterface that allows you to store an item in the cache
19+
// with a given cacheKey. The item can be of any type that implements the empty interface {}.
20+
// @property GetItemTTL - GetItemTTL is a method of the CacheInterface that returns the remaining
21+
// time-to-live (TTL) of a cached item identified by its cacheKey. It returns the TTL as a
22+
// time.Duration value and a boolean indicating whether the item exists in the cache or not. The TTL
23+
// represents the time
24+
// @property GetTTL - GetTTL is a method of the CacheInterface that returns the default time-to-live
25+
// (TTL) duration for cached items. This duration specifies how long an item should remain in the cache
26+
// before it is considered stale and needs to be refreshed or removed.
27+
// @property ExtendTTL - ExtendTTL is a method in the CacheInterface that allows you to extend the
28+
// time-to-live (TTL) of a cached item. This means that you can update the expiration time of a cached
29+
// item to keep it in the cache for a longer period of time. This can be useful if you
1030
type CacheInterface interface {
1131
Init(cacheDuration time.Duration)
1232
Get(cacheKey string) (interface{}, bool)
@@ -16,8 +36,13 @@ type CacheInterface interface {
1636
ExtendTTL(cacheKey string, item interface{})
1737
}
1838

39+
// `var CacheInstance CacheInterface` is declaring a variable named `CacheInstance` of type
40+
// `CacheInterface`. This variable will be used to store an instance of a cache that implements the
41+
// `CacheInterface` methods.
1942
var CacheInstance CacheInterface
2043

44+
// The function initializes a cache instance based on the cache type specified in the configuration
45+
// file.
2146
func CacheInit() {
2247
cacheDuration, err := time.ParseDuration(viper.GetString("cache_expire"))
2348
if err != nil {

cache/goCache.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,62 @@ import (
66
gocache "github.com/patrickmn/go-cache"
77
)
88

9+
// The GoCache type represents a cache with a specified time-to-live duration.
10+
// @property Cache - Cache is a property of type `*gocache.Cache` which is a pointer to an instance of
11+
// the GoCache library's Cache struct. This property is used to store and manage cached data in memory.
12+
// @property TTL - TTL stands for Time To Live and it is a duration that specifies the amount of time
13+
// for which an item should be considered valid in the cache before it is evicted. After the TTL
14+
// expires, the item is considered stale and will be removed from the cache on the next access or
15+
// eviction.
916
type GoCache struct {
1017
Cache *gocache.Cache
1118
TTL time.Duration
1219
}
1320

21+
// `func (c *GoCache) Init(cacheDuration time.Duration)` is a method of the `GoCache` struct that
22+
// initializes the cache with a specified time-to-live duration. It sets the `TTL` property of the
23+
// `GoCache` instance to the `cacheDuration` parameter and creates a new instance of the
24+
// `gocache.Cache` struct with the same `cacheDuration` and `TTL` properties. This method is called
25+
// when creating a new `GoCache` instance to set up the cache for use.
1426
func (c *GoCache) Init(cacheDuration time.Duration) {
1527
c.TTL = cacheDuration
1628
c.Cache = gocache.New(cacheDuration, c.TTL)
1729
}
1830

31+
// `func (c *GoCache) GetTTL() time.Duration {` is a method of the `GoCache` struct that returns the
32+
// time-to-live duration (`TTL`) of the cache instance. It retrieves the `TTL` property of the
33+
// `GoCache` instance and returns it as a `time.Duration` value. This method can be used to check the
34+
// current `TTL` value of the cache instance.
1935
func (c *GoCache) GetTTL() time.Duration {
2036
return c.TTL
2137
}
2238

39+
// `func (c *GoCache) Get(cacheKey string) (interface{}, bool)` is a method of the `GoCache` struct
40+
// that retrieves an item from the cache based on the specified `cacheKey`. It returns two values: the
41+
// cached item (as an `interface{}`) and a boolean value indicating whether the item was found in the
42+
// cache or not. If the item is found in the cache, the boolean value will be `true`, otherwise it will
43+
// be `false`.
2344
func (c *GoCache) Get(cacheKey string) (interface{}, bool) {
2445
return c.Cache.Get(cacheKey)
2546
}
2647

48+
// `func (c *GoCache) Set(cacheKey string, item interface{})` is a method of the `GoCache` struct that
49+
// sets a value in the cache with the specified `cacheKey`. The `item` parameter is the value to be
50+
// cached and the `cacheKey` parameter is the key used to identify the cached item. The method sets the
51+
// value in the cache with the specified `cacheKey` and a time-to-live duration (`TTL`) equal to the
52+
// `TTL` property of the `GoCache` instance. This means that the cached item will be considered valid
53+
// for the duration of the `TTL` and will be automatically evicted from the cache after the `TTL`
54+
// expires.
2755
func (c *GoCache) Set(cacheKey string, item interface{}) {
2856
c.Cache.Set(cacheKey, item, c.TTL)
2957
}
3058

59+
// `func (c *GoCache) GetItemTTL(cacheKey string) (time.Duration, bool)` is a method of the `GoCache`
60+
// struct that retrieves the time-to-live duration (`TTL`) of a cached item identified by the specified
61+
// `cacheKey`. It returns two values: the time-to-live duration of the cached item (as a
62+
// `time.Duration` value) and a boolean value indicating whether the item was found in the cache or
63+
// not. If the item is found in the cache, the boolean value will be `true`, otherwise it will be
64+
// `false`. This method can be used to check the remaining time-to-live of a cached item.
3165
func (c *GoCache) GetItemTTL(cacheKey string) (time.Duration, bool) {
3266
_, expiration, found := c.Cache.GetWithExpiration(cacheKey)
3367

@@ -37,6 +71,14 @@ func (c *GoCache) GetItemTTL(cacheKey string) (time.Duration, bool) {
3771
return difference, found
3872
}
3973

74+
// `func (c *GoCache) ExtendTTL(cacheKey string, item interface{})` is a method of the `GoCache` struct
75+
// that extends the time-to-live duration (`TTL`) of a cached item identified by the specified
76+
// `cacheKey`. It does this by calling the `Set` method of the `gocache.Cache` struct with the same
77+
// `cacheKey` and `item` parameters, and with a time-to-live duration (`TTL`) equal to the `TTL`
78+
// property of the `GoCache` instance. This means that the cached item will be considered valid for an
79+
// additional duration of the `TTL` and will be automatically evicted from the cache after the extended
80+
// `TTL` expires. This method can be used to refresh the time-to-live of a cached item to prevent it
81+
// from being evicted from the cache prematurely.
4082
func (c *GoCache) ExtendTTL(cacheKey string, item interface{}) {
4183
c.Set(cacheKey, item)
4284
}

cache/redisCache.go

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,27 @@ import (
55
"time"
66

77
"github.com/go-redis/redis/v8"
8-
"github.com/wasilak/elastauth/logger"
98
"golang.org/x/exp/slog"
109
)
1110

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
1229
type RedisCache struct {
1330
Cache *redis.Client
1431
TTL time.Duration
@@ -17,6 +34,13 @@ type RedisCache struct {
1734
DB int
1835
}
1936

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.
2044
func (c *RedisCache) Init(cacheDuration time.Duration) {
2145
c.Cache = redis.NewClient(&redis.Options{
2246
Addr: c.Address,
@@ -28,36 +52,63 @@ func (c *RedisCache) Init(cacheDuration time.Duration) {
2852
c.TTL = cacheDuration
2953
}
3054

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.
3159
func (c *RedisCache) GetTTL() time.Duration {
3260
return c.TTL
3361
}
3462

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`.
3568
func (c *RedisCache) Get(cacheKey string) (interface{}, bool) {
3669
item, err := c.Cache.Get(c.CTX, cacheKey).Result()
3770

3871
if err != nil || len(item) == 0 {
39-
logger.LoggerInstance.Error("Error", slog.Any("message", err))
72+
slog.Error("Error", slog.Any("message", err))
4073
return item, false
4174
}
4275

4376
return item, true
4477
}
4578

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.
4686
func (c *RedisCache) Set(cacheKey string, item interface{}) {
4787
c.Cache.Set(c.CTX, cacheKey, item, c.TTL).Err()
4888
}
4989

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`.
5096
func (c *RedisCache) GetItemTTL(cacheKey string) (time.Duration, bool) {
5197
item, err := c.Cache.TTL(c.CTX, cacheKey).Result()
5298

5399
if err != nil {
54-
logger.LoggerInstance.Error("Error", slog.Any("message", err))
100+
slog.Error("Error", slog.Any("message", err))
55101
return item, false
56102
}
57103

58104
return item, true
59105
}
60106

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.
61112
func (c *RedisCache) ExtendTTL(cacheKey string, item interface{}) {
62113
c.Cache.Expire(c.CTX, cacheKey, c.TTL)
63114
}

libs/config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"github.com/wasilak/elastauth/logger"
1414
)
1515

16+
// This function initializes the configuration for an application using flags, environment variables,
17+
// and a YAML configuration file.
1618
func InitConfiguration() error {
1719
flag.Bool("debug", false, "Debug")
1820
flag.Bool("generateKey", false, "Generate valid encryption key for use in app")
@@ -60,6 +62,8 @@ func InitConfiguration() error {
6062
return nil
6163
}
6264

65+
// The function generates and sets a secret key if one is not provided or generates and prints a secret
66+
// key if the "generateKey" flag is set to true.
6367
func HandleSecretKey() error {
6468
if viper.GetBool("generateKey") {
6569
key, err := GenerateKey()
@@ -76,7 +80,7 @@ func HandleSecretKey() error {
7680
return err
7781
}
7882
viper.Set("secret_key", key)
79-
logger.LoggerInstance.Info("WARNING: No secret key provided. Setting randomly generated", slog.String("key", key))
83+
slog.Info("WARNING: No secret key provided. Setting randomly generated", slog.String("key", key))
8084
}
8185

8286
return nil

libs/crypto.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import (
1111
"io"
1212
)
1313

14+
// Encrypt encrypts a string using AES-GCM algorithm with a given key. The key
15+
// should be provided as a hexadecimal string. It returns the encrypted string
16+
// in hexadecimal format.
1417
func Encrypt(stringToEncrypt string, keyString string) (encryptedString string) {
1518

1619
//Since the key is in string, we need to convert decode it to bytes
@@ -42,6 +45,9 @@ func Encrypt(stringToEncrypt string, keyString string) (encryptedString string)
4245
return fmt.Sprintf("%x", ciphertext)
4346
}
4447

48+
// Decrypt decrypts a previously encrypted string using the same key used to
49+
// encrypt it. It takes in an encrypted string and a key string as parameters
50+
// and returns the decrypted string. The key must be in hexadecimal format.
4551
func Decrypt(encryptedString string, keyString string) (decryptedString string) {
4652

4753
key, _ := hex.DecodeString(keyString)

0 commit comments

Comments
 (0)