12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- package Account
- import (
- "Yunlot/conf"
- "Yunlot/logs"
- "fmt"
- "github.com/astaxie/beego/cache"
- _ "github.com/astaxie/beego/cache/redis"
- uuid "github.com/satori/go.uuid"
- "time"
- )
- var redis_Tokey cache.Cache
- func init() {
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_Tokey", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- var err error
- redis_Tokey, err = cache.NewCache("redis", config)
- if err != nil || redis_Tokey == nil {
- logs.Println(config)
- panic(any(err))
- }
- }
- // ---------------- Redis -------------------
- // Redis_Set(m.T_sn,m) // Redis 更新缓存
- func Redis_Tokey_Set(key string, r string) (err error) {
- err = redis_Tokey.Put(key, r, 24*time.Hour)
- if err != nil {
- logs.Println("set key:", key, ",value:", r, err)
- }
- return
- }
- // if r,is :=Redis_Get(T_sn);is{
- // return r,nil
- // }
- func Redis_Tokey_Get(key string) (r string, is bool) {
- if redis_Tokey.IsExist(key) {
- //println("找到key:",key)
- v := redis_Tokey.Get(key)
- value := string(v.([]byte))
- Redis_Tokey_Set(key, value)
- return value, true
- }
- //println("没有 找到key:",key)
- return "", false
- }
- func Redis_DelK(key string) (err error) {
- err = redis_Tokey.Delete(key)
- return
- }
- // ---------------- 方法 -------------------
- // 验证 TOKEY
- func Read_Tokey(User_tokey string) (string, bool) {
- return Redis_Tokey_Get(User_tokey)
- }
- // 添加 Tokey
- func Add_Tokey(User_uuid string) (Tokey string) {
- for true {
- Tokey = uuid.NewV4().String()
- _, is := Redis_Tokey_Get(Tokey)
- if !is {
- break
- }
- fmt.Print("申请 TOKEY 重复!重新生成。", Tokey)
- }
- Redis_Tokey_Set(Tokey, User_uuid)
- return Tokey
- }
|