Skip to content

Commit

Permalink
增加redis
Browse files Browse the repository at this point in the history
  • Loading branch information
LawyZheng committed Mar 23, 2021
1 parent 9b3a94e commit 6730f94
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 37 deletions.
70 changes: 35 additions & 35 deletions commands/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
beegoCache "github.com/astaxie/beego/cache"
_ "github.com/astaxie/beego/cache/memcache"

// "github.com/astaxie/beego/cache/redis"
// _ "github.com/astaxie/beego/cache/redis"
"github.com/astaxie/beego/cache/redis"
_ "github.com/astaxie/beego/cache/redis"
"github.com/astaxie/beego/logs"
"github.com/astaxie/beego/orm"
"github.com/howeyc/fsnotify"
Expand Down Expand Up @@ -326,7 +326,7 @@ func ResolveCommand(args []string) {
beego.BConfig.WebConfig.StaticDir["/static"] = filepath.Join(conf.WorkingDirectory, "static")
beego.BConfig.WebConfig.StaticDir["/uploads"] = uploads
beego.BConfig.WebConfig.ViewsPath = conf.WorkingDir("views")
beego.BConfig.WebConfig.Session.SessionCookieSameSite = http.SameSiteLaxMode
beego.BConfig.WebConfig.Session.SessionCookieSameSite = http.SameSiteNoneMode

fonts := conf.WorkingDir("static", "fonts")

Expand Down Expand Up @@ -384,38 +384,38 @@ func RegisterCache() {
memory := beegoCache.NewMemoryCache()
beegoCache.DefaultEvery = cacheInterval
cache.Init(memory)
// } else if cacheProvider == "redis" {
// //设置Redis前缀
// if key := beego.AppConfig.DefaultString("cache_redis_prefix", ""); key != "" {
// redis.DefaultKey = key
// }
// var redisConfig struct {
// Conn string `json:"conn"`
// Password string `json:"password"`
// DbNum int `json:"dbNum"`
// }
// redisConfig.DbNum = 0
// redisConfig.Conn = beego.AppConfig.DefaultString("cache_redis_host", "")
// if pwd := beego.AppConfig.DefaultString("cache_redis_password", ""); pwd != "" {
// redisConfig.Password = pwd
// }
// if dbNum := beego.AppConfig.DefaultInt("cache_redis_db", 0); dbNum > 0 {
// redisConfig.DbNum = dbNum
// }

// bc, err := json.Marshal(&redisConfig)
// if err != nil {
// beego.Error("初始化Redis缓存失败:", err)
// os.Exit(1)
// }
// redisCache, err := beegoCache.NewCache("redis", string(bc))

// if err != nil {
// beego.Error("初始化Redis缓存失败:", err)
// os.Exit(1)
// }

// cache.Init(redisCache)
} else if cacheProvider == "redis" {
//设置Redis前缀
if key := beego.AppConfig.DefaultString("cache_redis_prefix", ""); key != "" {
redis.DefaultKey = key
}
var redisConfig struct {
Conn string `json:"conn"`
Password string `json:"password"`
DbNum int `json:"dbNum"`
}
redisConfig.DbNum = 0
redisConfig.Conn = beego.AppConfig.DefaultString("cache_redis_host", "")
if pwd := beego.AppConfig.DefaultString("cache_redis_password", ""); pwd != "" {
redisConfig.Password = pwd
}
if dbNum := beego.AppConfig.DefaultInt("cache_redis_db", 0); dbNum > 0 {
redisConfig.DbNum = dbNum
}

bc, err := json.Marshal(&redisConfig)
if err != nil {
beego.Error("初始化Redis缓存失败:", err)
os.Exit(1)
}
redisCache, err := beegoCache.NewCache("redis", string(bc))

if err != nil {
beego.Error("初始化Redis缓存失败:", err)
os.Exit(1)
}

cache.Init(redisCache)
} else if cacheProvider == "memcache" {

var memcacheConfig struct {
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/astaxie/beego v1.12.3
github.com/boombuler/barcode v1.0.0
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b // indirect
github.com/gomodule/redigo/redis v0.0.0-20200429221454-e14091dffc1b // indirect
github.com/howeyc/fsnotify v0.9.0
github.com/kardianos/service v1.1.0
github.com/lib/pq v1.7.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/snappy v0.0.0-20170215233205-553a64147049/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0=
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/gomodule/redigo/redis v0.0.0-20200429221454-e14091dffc1b h1:XAV1CHPRk+MUwbuzYHrCgq7mVq0Z0kcKNilDF4A8yys=
github.com/gomodule/redigo/redis v0.0.0-20200429221454-e14091dffc1b/go.mod h1:0zioC1ElIFjAFjnYHA9Y8oeU9SHff86XhZgs2tpx9SU=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
_ "github.com/astaxie/beego/session/memcache"
_ "github.com/astaxie/beego/session/mysql"

// _ "github.com/astaxie/beego/session/redis"
_ "github.com/astaxie/beego/session/redis"
"github.com/kardianos/service"
"github.com/lifei6671/mindoc/commands"
"github.com/lifei6671/mindoc/commands/daemon"
Expand Down

0 comments on commit 6730f94

Please sign in to comment.