The rediscache
package provides a convenient way to implement caching using Redis. It combines Redis and hashing algorithms in one place, making it faster to implement caching in your applications.
To install the rediscache
package, use the following command:
go get github.com/golanguzb70/redis-cache
Import the rediscache
package in your Go code:
import "github.com/golanguzb70/redis-cache"
Create a new Redis cache instance by calling the New
function and passing a Config
object:
cfg := &rediscache.Config{
RedisHost: "localhost",
RedisPort: 6379,
RedisUsername: "your-redis-username",
RedisPassword: "your-redis-password",
}
cache, err := rediscache.New(cfg)
if err != nil {
// handle error
}
Store a key-value pair in the cache:
err := cache.Set(ctx, key, value, expiration)
if err != nil {
// handle error
}
Retrieve a value from the cache using a key:
value, err := cache.Get(ctx, key)
if err != nil {
// handle error
}
Delete a key from the cache:
err := cache.Del(ctx, key)
if err != nil {
// handle error
}
Delete all keys that match a wildcard pattern:
err := cache.DelWildCard(ctx, wildcard)
if err != nil {
// handle error
}
Check if the cache is available:
err := cache.Ping(ctx)
if err != nil {
// handle error
}
Generate a hash for a given object:
hash := cache.HashOject(obj)
Generate a hash for a given key:
hash := cache.Hash(key)
For more information, refer to the Redis Go client documentation.