12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- package cache
-
- import (
- "sync"
- "time"
-
- "git.links123.net/Slate/CorpusAI/config"
- "git.links123.net/links123.com/pkg/middleware/auth"
- "github.com/go-redis/redis"
- "github.com/sirupsen/logrus"
- )
-
- var (
- client *redis.Client
- session auth.Session
- once = &sync.Once{}
- )
-
-
- func Init() {
- once.Do(func() {
- redisConfig := config.C.Redis
-
- client = redis.NewClient(&redis.Options{
- Addr: redisConfig.Address,
- Password: redisConfig.Password,
- PoolSize: redisConfig.PoolSize,
- })
-
- for {
- if err := client.Ping().Err(); err != nil {
- logrus.Warn("waiting for redis server start...")
- time.Sleep(3 * time.Second)
- continue
- }
-
- logrus.Info("connect to redis successful")
-
- break
- }
-
- if config.C.App.Debug {
- session = auth.NewDebugSessionStore()
- } else {
- session = auth.NewRedisSessionStore(redisConfig.Address, redisConfig.Password)
- }
- })
- }
-
-
- func Close() {
- if client != nil {
- err := client.Close()
- if err != nil {
- logrus.Errorf("close redis connection error: %s", err.Error())
- return
- }
- logrus.Info("redis connection closed")
- }
- }
|