mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
39 lines
720 B
Go
39 lines
720 B
Go
package cache
|
|
|
|
import (
|
|
"errors"
|
|
"sync"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
var (
|
|
redisClient *redis.Client
|
|
mu sync.RWMutex
|
|
)
|
|
|
|
// SetRedis assigns the global redis client used across the application.
|
|
func SetRedis(client *redis.Client) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
redisClient = client
|
|
}
|
|
|
|
// Redis returns the configured redis client. It may be nil if not yet initialised.
|
|
func Redis() *redis.Client {
|
|
mu.RLock()
|
|
defer mu.RUnlock()
|
|
return redisClient
|
|
}
|
|
|
|
// MustRedis returns the redis client or panics if it has not been set.
|
|
func MustRedis() *redis.Client {
|
|
mu.RLock()
|
|
client := redisClient
|
|
mu.RUnlock()
|
|
if client == nil {
|
|
panic(errors.New("redis client not initialised"))
|
|
}
|
|
return client
|
|
}
|