Documentation ¶
Index ¶
- func CacheInit(ctx context.Context)
- type CacheInterface
- type GoCache
- func (c *GoCache) ExtendTTL(ctx context.Context, cacheKey string, item interface{})
- func (c *GoCache) Get(ctx context.Context, cacheKey string) (interface{}, bool)
- func (c *GoCache) GetItemTTL(ctx context.Context, cacheKey string) (time.Duration, bool)
- func (c *GoCache) GetTTL(ctx context.Context) time.Duration
- func (c *GoCache) Init(ctx context.Context, cacheDuration time.Duration)
- func (c *GoCache) Set(ctx context.Context, cacheKey string, item interface{})
- type RedisCache
- func (c *RedisCache) ExtendTTL(ctx context.Context, cacheKey string, item interface{})
- func (c *RedisCache) Get(ctx context.Context, cacheKey string) (interface{}, bool)
- func (c *RedisCache) GetItemTTL(ctx context.Context, cacheKey string) (time.Duration, bool)
- func (c *RedisCache) GetTTL(ctx context.Context) time.Duration
- func (c *RedisCache) Init(ctx context.Context, cacheDuration time.Duration)
- func (c *RedisCache) Set(ctx context.Context, cacheKey string, item interface{})
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type CacheInterface ¶
type CacheInterface interface { Init(ctx context.Context, cacheDuration time.Duration) Get(ctx context.Context, cacheKey string) (interface{}, bool) Set(ctx context.Context, cacheKey string, item interface{}) GetItemTTL(ctx context.Context, cacheKey string) (time.Duration, bool) GetTTL(ctx context.Context) time.Duration ExtendTTL(ctx context.Context, cacheKey string, item interface{}) }
The CacheInterface defines methods for initializing, getting, setting, and extending the time-to-live (TTL) of cached items. @property Init - Init is a method that initializes the cache with a specified cache duration. It takes a time.Duration parameter that represents the duration for which the cache items should be stored. @property Get - Get is a method of the CacheInterface that takes a cacheKey string as input and returns an interface{} and a bool. The interface{} represents the cached item associated with the cacheKey, and the bool indicates whether the item was found in the cache or not. @property Set - Set is a method of the CacheInterface that allows you to store an item in the cache with a given cacheKey. The item can be of any type that implements the empty interface {}. @property GetItemTTL - GetItemTTL is a method of the CacheInterface that returns the remaining time-to-live (TTL) of a cached item identified by its cacheKey. It returns the TTL as a time.Duration value and a boolean indicating whether the item exists in the cache or not. The TTL represents the time @property GetTTL - GetTTL is a method of the CacheInterface that returns the default time-to-live (TTL) duration for cached items. This duration specifies how long an item should remain in the cache before it is considered stale and needs to be refreshed or removed. @property ExtendTTL - ExtendTTL is a method in the CacheInterface that allows you to extend the time-to-live (TTL) of a cached item. This means that you can update the expiration time of a cached item to keep it in the cache for a longer period of time. This can be useful if you
var CacheInstance CacheInterface
`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` methods.
type GoCache ¶
The GoCache type represents a cache with a specified time-to-live duration. @property Cache - Cache is a property of type `*gocache.Cache` which is a pointer to an instance of the GoCache library's Cache struct. This property is used to store and manage cached data in memory. @property TTL - TTL stands for Time To Live and it is a duration that specifies the amount of time for which an item should be considered valid in the cache before it is evicted. After the TTL expires, the item is considered stale and will be removed from the cache on the next access or eviction.
func (*GoCache) ExtendTTL ¶
`func (c *GoCache) ExtendTTL(cacheKey string, item interface{})` is a method of the `GoCache` struct that extends the time-to-live duration (`TTL`) of a cached item identified by the specified `cacheKey`. It does this by calling the `Set` method of the `gocache.Cache` struct with the same `cacheKey` and `item` parameters, and with a time-to-live duration (`TTL`) equal to the `TTL` property of the `GoCache` instance. This means that the cached item will be considered valid for an additional duration of the `TTL` and will be automatically evicted from the cache after the extended `TTL` expires. This method can be used to refresh the time-to-live of a cached item to prevent it from being evicted from the cache prematurely.
func (*GoCache) Get ¶
`func (c *GoCache) Get(cacheKey string) (interface{}, bool)` is a method of the `GoCache` struct that retrieves an item from the cache based on the specified `cacheKey`. It returns two values: the cached item (as an `interface{}`) and a boolean value indicating whether the item was found in the cache or not. If the item is found in the cache, the boolean value will be `true`, otherwise it will be `false`.
func (*GoCache) GetItemTTL ¶
`func (c *GoCache) GetItemTTL(cacheKey string) (time.Duration, bool)` is a method of the `GoCache` struct that retrieves the time-to-live duration (`TTL`) of a cached item identified by the specified `cacheKey`. It returns two values: the time-to-live duration of the cached item (as a `time.Duration` value) and a boolean value indicating whether the item was found in the cache or not. If the item is found in the cache, the boolean value will be `true`, otherwise it will be `false`. This method can be used to check the remaining time-to-live of a cached item.
func (*GoCache) GetTTL ¶
`func (c *GoCache) GetTTL() time.Duration {` is a method of the `GoCache` struct that returns the time-to-live duration (`TTL`) of the cache instance. It retrieves the `TTL` property of the `GoCache` instance and returns it as a `time.Duration` value. This method can be used to check the current `TTL` value of the cache instance.
func (*GoCache) Init ¶
`func (c *GoCache) Init(cacheDuration time.Duration)` is a method of the `GoCache` struct that initializes the cache with a specified time-to-live duration. It sets the `TTL` property of the `GoCache` instance to the `cacheDuration` parameter and creates a new instance of the `gocache.Cache` struct with the same `cacheDuration` and `TTL` properties. This method is called when creating a new `GoCache` instance to set up the cache for use.
func (*GoCache) Set ¶
`func (c *GoCache) Set(cacheKey string, item interface{})` is a method of the `GoCache` struct that sets a value in the cache with the specified `cacheKey`. The `item` parameter is the value to be cached and the `cacheKey` parameter is the key used to identify the cached item. The method sets the value in the cache with the specified `cacheKey` and a time-to-live duration (`TTL`) equal to the `TTL` property of the `GoCache` instance. This means that the cached item will be considered valid for the duration of the `TTL` and will be automatically evicted from the cache after the `TTL` expires.
type RedisCache ¶
type RedisCache struct { Cache *redis.Client TTL time.Duration Address string DB int Tracer trace.Tracer }
The RedisCache type represents a Redis cache with a specified time-to-live, context, address, and database. @property Cache - Cache is a pointer to a Redis client instance that is used to interact with the Redis cache. @property TTL - TTL stands for "Time To Live" and refers to the amount of time that a cached item will remain in the cache before it is considered expired and needs to be refreshed or removed. In the context of the RedisCache struct, it represents the duration of time that cached items will be stored in @property CTX - CTX is a context.Context object that is used to manage the lifecycle of a RedisCache instance. It is used to control the cancellation of operations and to pass values between functions. It is a part of the standard library in Go and is used extensively in network programming. @property {string} Address - Address is a string property that represents the network address of the Redis server. It typically includes the hostname or IP address of the server and the port number on which Redis is listening. For example, "localhost:6379" or "redis.example.com:6379". @property {int} DB - DB stands for "database" and is an integer value that represents the specific database within the Redis instance that the RedisCache struct will be interacting with. Redis allows for multiple databases to be created within a single instance, each with its own set of keys and values. The DB property allows the RedisCache
func (*RedisCache) ExtendTTL ¶
func (c *RedisCache) ExtendTTL(ctx context.Context, cacheKey string, item interface{})
`func (c *RedisCache) ExtendTTL(cacheKey string, item interface{})` is a method of the `RedisCache` struct that extends the time-to-live (TTL) duration of a cached item with the specified `cacheKey`. It uses the `Expire` function of the Redis client to set the TTL duration of the cached item to the value of the `TTL` property of the `RedisCache` instance. This method is useful for refreshing the TTL of a cached item to prevent it from expiring prematurely.
func (*RedisCache) Get ¶
func (c *RedisCache) Get(ctx context.Context, cacheKey string) (interface{}, bool)
`func (c *RedisCache) Get(cacheKey string) (interface{}, bool)` is a method of the `RedisCache` struct that retrieves a cached item from the Redis cache using the specified `cacheKey`. It returns a tuple containing the cached item as an `interface{}` and a boolean value indicating whether the item was successfully retrieved from the cache or not. If the item is not found in the cache or an error occurs during retrieval, the method returns an empty `interface{}` and `false`.
func (*RedisCache) GetItemTTL ¶
`func (c *RedisCache) GetItemTTL(cacheKey string) (time.Duration, bool)` is a method of the `RedisCache` struct that retrieves the time-to-live (TTL) duration of a cached item with the specified `cacheKey`. It returns a tuple containing the TTL duration as a `time.Duration` value and a boolean value indicating whether the TTL was successfully retrieved from the cache or not. If the TTL is not found in the cache or an error occurs during retrieval, the method returns a zero `time.Duration` value and `false`.
func (*RedisCache) GetTTL ¶
func (c *RedisCache) GetTTL(ctx context.Context) time.Duration
`func (c *RedisCache) GetTTL() time.Duration {` is a method of the `RedisCache` struct that returns the `TTL` property of the `RedisCache` instance, which represents the duration of time that cached items will be stored in the cache before they are considered expired and need to be refreshed or removed. The method returns a `time.Duration` value.
func (*RedisCache) Init ¶
func (c *RedisCache) Init(ctx context.Context, cacheDuration time.Duration)
`func (c *RedisCache) Init(cacheDuration time.Duration)` is a method of the `RedisCache` struct that initializes a new Redis client instance and sets the cache duration (TTL) for the RedisCache instance. It takes a `time.Duration` parameter `cacheDuration` which represents the duration of time that cached items will be stored in the cache. The method creates a new Redis client instance using the `redis.NewClient` function and sets the `Cache` property of the `RedisCache` instance to the new client instance. It also sets the `CTX` property to a new `context.Background()` instance. Finally, it sets the `TTL` property of the `RedisCache` instance to the `cacheDuration` parameter.
func (*RedisCache) Set ¶
func (c *RedisCache) Set(ctx context.Context, cacheKey string, item interface{})
`func (c *RedisCache) Set(cacheKey string, item interface{})` is a method of the `RedisCache` struct that sets a value in the Redis cache with the specified `cacheKey`. It takes two parameters: `cacheKey`, which is a string representing the key under which the value will be stored in the cache, and `item`, which is an interface{} representing the value to be stored. The method uses the `Set` function of the Redis client to set the value in the cache with the specified key and TTL (time-to-live) duration. If an error occurs during the set operation, it is logged using the `slog.Error` function.