31 Star 127 Fork 43

ShirDon-廖显东 / go验证码合集包

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
cstore.go 3.21 KB
一键复制 编辑 原始数据 按行查看 历史
shirdon 提交于 2019-07-07 16:16 . commit
//++++++++++++++++++++++++++++++++++++++++
//Fighting for great,share generate value!
//Build the best soft by golang,let's go!
//++++++++++++++++++++++++++++++++++++++++
//Author:ShirDon <http://www.shirdon.com>
//Email:hcbsts@163.com; 823923263@qq.com
//++++++++++++++++++++++++++++++++++++++++
package captchas_with_go
import (
"crypto/md5"
enc "encoding/gob"
"encoding/hex"
"fmt"
"os"
"sync"
"time"
)
//CStore is the Captcha info store service
type CStore struct {
engine string
mu sync.RWMutex
data map[string]*CaptchaInfo
expiresTime time.Duration
gcProbability int
gcDivisor int
}
//CreateCStore will create a new CStore
func CreateCStore(expiresTime time.Duration, gcProbability int, gcDivisor int) *CStore {
store := new(CStore)
store.engine = STORE_ENGINE_BUILDIN
store.data = make(map[string]*CaptchaInfo)
store.expiresTime = expiresTime
store.gcProbability = gcProbability
store.gcDivisor = gcDivisor
return store
}
//Get captcha info by key
func (store *CStore) Get(key string) *CaptchaInfo {
defer store.gcWrapper() //run gc after get
store.mu.Lock()
defer store.mu.Unlock()
ret := store.data[key]
return ret
}
//Add captcha info and get the auto generated key
func (store *CStore) Add(captcha *CaptchaInfo) string {
store.mu.Lock()
defer store.mu.Unlock()
key := fmt.Sprintf("%s%s%x", captcha.Text, randStr(20), time.Now().UnixNano())
key = hex.EncodeToString(md5.New().Sum([]byte(key)))
key = key[:32]
store.data[key] = captcha
return key
}
//Update captcha info
func (store *CStore) Update(key string, captcha *CaptchaInfo) bool {
store.mu.Lock()
defer store.mu.Unlock()
store.data[key] = captcha
return true
}
//Del captcha info by key
func (store *CStore) Del(key string) {
store.mu.Lock()
defer store.mu.Unlock()
delete(store.data, key)
}
//Destroy the whole store
func (store *CStore) Destroy() {
for key := range store.data {
store.Del(key)
}
}
//OnConstruct load data
func (store *CStore) OnConstruct() {
}
//OnDestruct dump data
func (store *CStore) OnDestruct() {
}
//Dump the whole store
func (store *CStore) Dump(file string) error {
store.mu.Lock()
defer store.mu.Unlock()
pwd, err := os.Getwd()
if (nil == err) && ("" != pwd) {
f, err := os.Create(pwd + "/" + file)
if nil == err {
encoder := enc.NewEncoder(f)
err := encoder.Encode(store.data)
f.Close()
if nil == err {
return err
} else {
return nil
}
} else {
return err
}
} else {
return err
}
return nil
}
//LoadDumped file to store
func (store *CStore) LoadDumped(file string) error {
data := &map[string]*CaptchaInfo{}
pwd, err := os.Getwd()
if (nil == err) && ("" != pwd) {
f, err := os.Open(pwd + "/" + file)
if nil == err {
decoder := enc.NewDecoder(f)
err := decoder.Decode(data)
f.Close()
if nil == err {
store.data = *data
return nil
} else {
return err
}
} else {
return err
}
} else {
return err
}
return err
}
func (store *CStore) gcWrapper() {
//run PROBABILITY
if rnd(0, store.gcDivisor) == store.gcProbability {
go store.gc()
}
}
func (store *CStore) gc() {
for key, val := range store.data {
if val.CreateTime.Add(store.expiresTime).Before(time.Now()) {
store.Del(key)
}
}
}
Go
1
https://gitee.com/shirdonl/captchas_with_go.git
git@gitee.com:shirdonl/captchas_with_go.git
shirdonl
captchas_with_go
go验证码合集包
master

搜索帮助