-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverificationdriver.go
71 lines (55 loc) · 1.15 KB
/
verificationdriver.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
package gocache
import "time"
type verification struct {
name string
ttl time.Duration
cache Cache
}
func (driver verification) Set(code string) error {
exists, err := driver.cache.Set(driver.name, code)
if err != nil {
return err
}
if !exists {
return driver.cache.Put(driver.name, code, &driver.ttl)
}
return nil
}
func (driver verification) Generate(count uint) (string, error) {
code, err := randomString(count, "0123456789")
if err != nil {
return "", err
}
err = driver.Set(code)
if err != nil {
return "", err
}
return code, nil
}
func (driver verification) Clear() error {
return driver.cache.Forget(driver.name)
}
func (driver verification) Get() (string, error) {
caster, err := driver.cache.Cast(driver.name)
if err != nil {
return "", err
}
if caster.IsNil() {
return "", nil
}
code, err := caster.String()
if err != nil {
return "", err
}
return code, nil
}
func (driver verification) Exists() (bool, error) {
return driver.cache.Exists(driver.name)
}
func (driver verification) TTL() (time.Duration, error) {
ttl, err := driver.cache.TTL(driver.name)
if err != nil {
return 0, err
}
return ttl, nil
}