forked from hanguofeng/gocaptcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcstore.go
124 lines (94 loc) · 2.35 KB
/
mcstore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2013 hanguofeng. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gocaptcha
import (
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
"log"
"time"
enc "encoding/gob"
"github.com/bradfitz/gomemcache/memcache"
)
const (
MC_KEY_PREFIX = "gocaptcha-"
)
//MCStore is the Captcha info store service
type MCStore struct {
engine string
mc *memcache.Client
}
//CreateCStore will create a new CStore
func CreateMCStore(expiresTime time.Duration, servers []string) *MCStore {
store := new(MCStore)
store.mc = memcache.New(servers...)
store.mc.Timeout = expiresTime
return store
}
//Get captcha info by key
func (store *MCStore) Get(key string) *CaptchaInfo {
item, err := store.mc.Get(MC_KEY_PREFIX + key)
if nil != err {
return nil
}
ret := store.decodeValue(item.Value)
return ret
}
//Add captcha info and get the auto generated key
func (store *MCStore) Add(captcha *CaptchaInfo) string {
key := fmt.Sprintf("%s%s%x", captcha.Text, randStr(20), time.Now().UnixNano())
key = hex.EncodeToString(md5.New().Sum([]byte(key)))
key = key[:32]
item := new(memcache.Item)
item.Key = MC_KEY_PREFIX + key
item.Value = store.encodeValue(captcha)
err := store.mc.Add(item)
if nil != err {
log.Printf("add key in memcache err:%s", err)
}
return key
}
//Update captcha info
func (store *MCStore) Update(key string, captcha *CaptchaInfo) bool {
item := new(memcache.Item)
item.Key = MC_KEY_PREFIX + key
item.Value = store.encodeValue(captcha)
err := store.mc.Set(item)
if nil != err {
log.Printf("update key in memcache err:%s", err)
return false
} else {
return true
}
}
//Del captcha info by key
func (store *MCStore) Del(key string) {
store.mc.Delete(MC_KEY_PREFIX + key)
}
//Destroy the whole store
func (store *MCStore) Destroy() {
}
//OnConstruct load data
func (store *MCStore) OnConstruct() {
}
//OnDestruct dump data
func (store *MCStore) OnDestruct() {
}
func (store *MCStore) encodeValue(val *CaptchaInfo) []byte {
buf := bytes.NewBufferString("")
encoder := enc.NewEncoder(buf)
err := encoder.Encode(val)
if nil != err {
return nil
}
return buf.Bytes()
}
func (store *MCStore) decodeValue(value []byte) *CaptchaInfo {
data := &CaptchaInfo{}
buf := bytes.NewBuffer(value)
decoder := enc.NewDecoder(buf)
decoder.Decode(data)
return data
}