Skip to content

Commit

Permalink
feat: black_cache
Browse files Browse the repository at this point in the history
  • Loading branch information
songzhibin97 committed Nov 18, 2023
1 parent 2523467 commit 8addc42
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 20 deletions.
1 change: 1 addition & 0 deletions server/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ jwt:
expires-time: 7d
buffer-time: 1d
issuer: qmPlus
cache-type: "local"
# zap logger configuration
zap:
level: info
Expand Down
1 change: 1 addition & 0 deletions server/config/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ type JWT struct {
ExpiresTime string `mapstructure:"expires-time" json:"expires-time" yaml:"expires-time"` // 过期时间
BufferTime string `mapstructure:"buffer-time" json:"buffer-time" yaml:"buffer-time"` // 缓冲时间
Issuer string `mapstructure:"issuer" json:"issuer" yaml:"issuer"` // 签发者
CacheType string `mapstructure:"cache-type" json:"cache-type" yaml:"cache-type"` // 缓存类型
}
11 changes: 11 additions & 0 deletions server/global/black_cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package global

import "time"

type IBlackCache interface {
Set(k string, v interface{}, d time.Duration)
SetDefault(k string, v interface{})
Get(k string) (interface{}, bool)

Increment(k string, n int64) error
}
7 changes: 3 additions & 4 deletions server/global/global.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package global

import (
"github.com/qiniu/qmgo"
"sync"

"github.com/flipped-aurora/gin-vue-admin/server/utils/timer"
"github.com/songzhibin97/gkit/cache/local_cache"
"github.com/qiniu/qmgo"

"github.com/flipped-aurora/gin-vue-admin/server/utils/timer"
"golang.org/x/sync/singleflight"

"go.uber.org/zap"
Expand All @@ -30,7 +29,7 @@ var (
GVA_Timer timer.Timer = timer.NewTimerTask()
GVA_Concurrency_Control = &singleflight.Group{}

BlackCache local_cache.Cache
BlackCache IBlackCache
lock sync.RWMutex
)

Expand Down
1 change: 0 additions & 1 deletion server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ require (
golang.org/x/crypto v0.10.0
golang.org/x/sync v0.3.0
golang.org/x/text v0.11.0
gorm.io/datatypes v1.2.0
gorm.io/driver/mysql v1.5.1
gorm.io/driver/postgres v1.5.2
gorm.io/driver/sqlserver v1.5.1
Expand Down
4 changes: 0 additions & 4 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,6 @@ github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJ
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/microsoft/go-mssqldb v1.1.0 h1:jsV+tpvcPTbNNKW0o3kiCD69kOHICsfjZ2VcVu2lKYc=
github.com/microsoft/go-mssqldb v1.1.0/go.mod h1:LzkFdl4z2Ck+Hi+ycGOTbL56VEfgoyA2DvYejrNGbRk=
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
Expand Down Expand Up @@ -832,14 +831,11 @@ gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/datatypes v1.2.0 h1:5YT+eokWdIxhJgWHdrb2zYUimyk0+TaFth+7a0ybzco=
gorm.io/datatypes v1.2.0/go.mod h1:o1dh0ZvjIjhH/bngTpypG6lVRJ5chTBxE09FH/71k04=
gorm.io/driver/mysql v1.4.3/go.mod h1:sSIebwZAVPiT+27jK9HIwvsqOGKx3YMPmrA3mBJR10c=
gorm.io/driver/mysql v1.5.1 h1:WUEH5VF9obL/lTtzjmML/5e6VfFR/788coz2uaVCAZw=
gorm.io/driver/mysql v1.5.1/go.mod h1:Jo3Xu7mMhCyj8dlrb3WoCaRd1FhsVh+yMXb1jUInf5o=
gorm.io/driver/postgres v1.5.2 h1:ytTDxxEv+MplXOfFe3Lzm7SjG09fcdb3Z/c056DTBx0=
gorm.io/driver/postgres v1.5.2/go.mod h1:fmpX0m2I1PKuR7mKZiEluwrP3hbs+ps7JIGMUBpCgl8=
gorm.io/driver/sqlite v1.4.3 h1:HBBcZSDnWi5BW3B3rwvVTc510KGkBkexlOg0QrmLUuU=
gorm.io/driver/sqlserver v1.5.1 h1:wpyW/pR26U94uaujltiFGXY7fd2Jw5hC9PB1ZF/Y5s4=
gorm.io/driver/sqlserver v1.5.1/go.mod h1:AYHzzte2msKTmYBYsSIq8ZUsznLJwBdkB2wpI+kt0nM=
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
Expand Down
27 changes: 16 additions & 11 deletions server/initialize/other.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@ import (
)

func OtherInit() {
dr, err := utils.ParseDuration(global.GVA_CONFIG.JWT.ExpiresTime)
if err != nil {
panic(err)
}
_, err = utils.ParseDuration(global.GVA_CONFIG.JWT.BufferTime)
if err != nil {
panic(err)
}
switch global.GVA_CONFIG.JWT.CacheType {
case "", "local":
dr, err := utils.ParseDuration(global.GVA_CONFIG.JWT.ExpiresTime)
if err != nil {
panic(err)
}
_, err = utils.ParseDuration(global.GVA_CONFIG.JWT.BufferTime)
if err != nil {
panic(err)
}

global.BlackCache = local_cache.NewCache(
local_cache.SetDefaultExpire(dr),
)
global.BlackCache = local_cache.NewCache(
local_cache.SetDefaultExpire(dr),
)
default:
panic("jwt cache type is not support")
}
}

0 comments on commit 8addc42

Please sign in to comment.