phone_check.go 1.0 KB
package cache

import (
	"encoding/json"
	"time"
)

const phoneCheckExpire = 60 * 5

//短信验证码验证缓存
type PhoneCheckCache struct {
}

func (ca PhoneCheckCache) phoneCheckKey(smsCodeIdentity string) string {
	str := KEY_PREFIX + "phone-check:" + smsCodeIdentity
	return str
}

func (ca PhoneCheckCache) Add(smsCodeIdentity string, value interface{}) error {
	key := ca.phoneCheckKey(smsCodeIdentity)
	data, err := json.Marshal(value)
	if err != nil {
		return err
	}
	result := clientRedis.Set(key, string(data), time.Duration(phoneCheckExpire*time.Second))
	return result.Err()
}

func (ca PhoneCheckCache) Get(smsCodeIdentity string, value interface{}) error {
	key := ca.phoneCheckKey(smsCodeIdentity)
	result := clientRedis.Get(key)
	if result.Err() != nil {
		return result.Err()
	}
	err := json.Unmarshal([]byte(result.Val()), value)
	return err
}

type PhoneCheckItem struct {
	Phone           string `json:"phone"`
	SmsCodeIdentity string `json:"smsCodeIdentity"`
	Action          int    `json:"action"`
}