-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (63 loc) · 1.87 KB
/
main.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
package memcached
import (
"errors"
"os"
"time"
"github.com/bradberger/gocache/drivers/lru"
"github.com/google/logger"
)
// Generic memcached errors
var (
ErrNotImplemented = errors.New("not implemented")
ErrInvalidCommand = errors.New("invalid command")
ErrUnknownCommand = errors.New("unknown command")
)
const (
cmdCas = "cas"
cmdSet = "set"
cmdAdd = "add"
cmdReplace = "replace"
cmdAppend = "append"
cmdPrepend = "prepend"
cmdGet = "get"
cmdGets = "gets"
cmdIncr = "incr"
cmdDecr = "decr"
cmdDel = "delete"
cmdTouch = "touch"
cmdGat = "gat"
cmdGats = "gats"
cmdFlushAll = "flush_all"
cmdCacheMemlimit = "cache_memlimit"
cmdVersion = "version"
cmdQuit = "quit"
cmdStat = "stat"
cmdStats = "stats"
)
// ListenAndServe starts a memcache server on the
func ListenAndServe(addr string) error {
s := &Server{
Cache: lru.NewBasic(lru.Gigabyte, 100000),
Logger: logger.Init("Memcached", true, false, os.Stdout),
Addr: ":11211",
}
return s.ListenAndServe()
}
// getExpiration caculates the actual time of the expiration based on
// the given int. Per the memcached spec the actual value sent may either be
// Unix time (number of seconds since January 1, 1970, as a 32-bit
// value), or a number of seconds starting from current time. In the
// latter case, this number of seconds may not exceed 60*60*24*30 (number
// of seconds in 30 days); if the number sent by a client is larger than
// that, the server will consider it to be real Unix time value rather
// than an offset from current time.
func getExpiration(dur int) time.Time {
switch {
case dur > 2592000:
return time.Unix(int64(dur), 0)
case dur > 0:
return time.Now().Add(time.Duration(dur) * time.Second)
default:
return time.Time{}
}
}