-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
100 lines (80 loc) · 2.14 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
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
package main
import (
"flag"
"fmt"
"log"
"net"
"github.com/xiaost/blobcached/cache"
"github.com/xiaost/blobcached/server"
)
const VERSION = "0.1.1-dev"
func main() {
var (
bindAddr string
cachePath string
cacheSize int64
cacheShards int64
cacheTTL int64
bufsize int
printVersion bool
)
flag.StringVar(&bindAddr,
"addr", ":11211",
"the addr that blobcached listen on.")
flag.StringVar(&cachePath,
"path", "cachedata",
"the cache path used by blobcached to store items.")
flag.Int64Var(&cacheSize,
"size", int64(cache.DefualtCacheOptions.Size),
"cache file size used by blobcached to store items. ")
flag.Int64Var(&cacheShards,
"shards", int64(cache.DefualtCacheOptions.ShardNum),
"cache shards for performance purpose. max shards is 128.")
flag.Int64Var(&cacheTTL,
"ttl", 0,
"the global ttl of cache items.")
flag.IntVar(&bufsize,
"buf", 4096,
"default buffer size used by get/set.")
flag.BoolVar(&printVersion, "v", false,
"print the version and exit")
flag.Parse()
if printVersion {
fmt.Println("Blobcached", VERSION)
return
}
l, err := net.Listen("tcp", bindAddr)
if err != nil {
log.Fatal(err)
}
// cacheSize: limit to [2*MaxValueSize, +)
if cacheSize <= 2*cache.MaxValueSize {
cacheSize = 2 * cache.MaxValueSize
log.Printf("warn: cache size invaild. set to %d", cacheSize)
}
// cacheShards: limit to (0, cache.MaxShards] and per cache shard size >= cache.MaxValueSize
if (cacheShards <= 0 || cacheShards > cache.MaxShards) ||
(cacheSize/cacheShards < cache.MaxValueSize) {
cacheShards = int64(cache.DefualtCacheOptions.ShardNum)
if cacheSize/cacheShards < cache.MaxValueSize {
cacheShards = cacheSize / cache.MaxValueSize
}
log.Printf("warn: cache shards invaild. set to %d", cacheShards)
}
if cacheTTL < 0 {
cacheTTL = 0
}
allocator := cache.NewAllocatorPool(bufsize)
options := &cache.CacheOptions{
ShardNum: int(cacheShards),
Size: cacheSize,
TTL: cacheTTL,
Allocator: allocator,
}
c, err := cache.NewCache(cachePath, options)
if err != nil {
log.Fatal(err)
}
s := server.NewMemcacheServer(l, c, allocator)
log.Fatal(s.Serv())
}