enhance(cache):增加两个用于生成缓存键的工具函数。
This commit is contained in:
parent
e09bf9e728
commit
b8c44c9b1e
12
cache/abstract.go
vendored
12
cache/abstract.go
vendored
|
@ -2,6 +2,8 @@ package cache
|
|||
|
||||
import (
|
||||
"electricity_bill_calc/global"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
|
@ -56,3 +58,13 @@ func Delete(key string) (bool, error) {
|
|||
}
|
||||
return result > 0, nil
|
||||
}
|
||||
|
||||
// 生成用于Redis存储的键
|
||||
func CacheKey(category string, ids ...string) string {
|
||||
var b strings.Builder
|
||||
b.WriteString(category)
|
||||
for _, s := range ids {
|
||||
fmt.Fprintf(&b, ":%s", s)
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
|
18
cache/session.go
vendored
18
cache/session.go
vendored
|
@ -3,25 +3,35 @@ package cache
|
|||
import (
|
||||
"electricity_bill_calc/model"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func SessionKey(keys ...string) string {
|
||||
var b strings.Builder
|
||||
b.WriteString("session")
|
||||
for _, s := range keys {
|
||||
fmt.Fprintf(&b, ":%s", s)
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func CacheSession(session *model.Session) error {
|
||||
key := fmt.Sprintf("session:%s", session.Token)
|
||||
key := SessionKey(session.Token)
|
||||
return Cache(key, session, 2*time.Hour)
|
||||
}
|
||||
|
||||
func RetreiveSession(token string) (*model.Session, error) {
|
||||
key := fmt.Sprintf("session:%s", token)
|
||||
key := SessionKey(token)
|
||||
return Retreive[model.Session](key)
|
||||
}
|
||||
|
||||
func HasSession(token string) (bool, error) {
|
||||
key := fmt.Sprintf("session:%s", token)
|
||||
key := SessionKey(token)
|
||||
return Exists(key)
|
||||
}
|
||||
|
||||
func ClearSession(token string) (bool, error) {
|
||||
key := fmt.Sprintf("session:%s", token)
|
||||
key := SessionKey(token)
|
||||
return Delete(key)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user