forked from goffee/core
add documentation
This commit is contained in:
parent
1b23363f6f
commit
695f1f57ba
8 changed files with 116 additions and 14 deletions
8
cache.go
8
cache.go
|
|
@ -16,6 +16,9 @@ type Cache struct {
|
|||
redis *redis.Client
|
||||
}
|
||||
|
||||
// NewCache initializes a new Cache instance with the provided configuration and connects to a Redis database.
|
||||
// If caching is enabled in the provided configuration but the connection to Redis fails, it causes a panic.
|
||||
// Returns a pointer to the initialized Cache structure.
|
||||
func NewCache(cacheConfig CacheConfig) *Cache {
|
||||
ctx = context.Background()
|
||||
dbStr := os.Getenv("REDIS_DB")
|
||||
|
|
@ -40,6 +43,7 @@ func NewCache(cacheConfig CacheConfig) *Cache {
|
|||
}
|
||||
}
|
||||
|
||||
// Set stores a key-value pair in the Redis cache with no expiration. Returns an error if the operation fails.
|
||||
func (c *Cache) Set(key string, value string) error {
|
||||
err := c.redis.Set(ctx, key, value, 0).Err()
|
||||
if err != nil {
|
||||
|
|
@ -48,6 +52,8 @@ func (c *Cache) Set(key string, value string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// SetWithExpiration stores a key-value pair in the cache with a specified expiration duration.
|
||||
// Returns an error if the operation fails.
|
||||
func (c *Cache) SetWithExpiration(key string, value string, expiration time.Duration) error {
|
||||
err := c.redis.Set(ctx, key, value, expiration).Err()
|
||||
if err != nil {
|
||||
|
|
@ -56,6 +62,7 @@ func (c *Cache) SetWithExpiration(key string, value string, expiration time.Dura
|
|||
return nil
|
||||
}
|
||||
|
||||
// Get retrieves the value associated with the provided key from the cache. Returns an error if the operation fails.
|
||||
func (c *Cache) Get(key string) (string, error) {
|
||||
result, err := c.redis.Get(ctx, key).Result()
|
||||
if err != nil {
|
||||
|
|
@ -64,6 +71,7 @@ func (c *Cache) Get(key string) (string, error) {
|
|||
return result, nil
|
||||
}
|
||||
|
||||
// Delete removes the specified key from the cache and returns an error if the operation fails.
|
||||
func (c *Cache) Delete(key string) error {
|
||||
err := c.redis.Del(ctx, key).Err()
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue