forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cacheservice.go
98 lines (87 loc) · 3.27 KB
/
cacheservice.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
// Copyright 2015, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cacheservice
import (
"fmt"
"sync"
"time"
)
// NewConnFunc is a factory method that creates a CacheService instance
// using given CacheServiceConfig.
type NewConnFunc func(config Config) (CacheService, error)
// services stores all supported cache service.
var services = make(map[string]NewConnFunc)
var mu sync.Mutex
// DefaultCacheService decides the default cache service connection.
var DefaultCacheService string
// Config carries config data for CacheService.
type Config struct {
Address string
Timeout time.Duration
}
// Result gives the cached data.
type Result struct {
Key string
Value []byte
Flags uint16
Cas uint64
}
// CacheService defines functions to use a cache service.
type CacheService interface {
// Get returns cached data for given keys.
Get(keys ...string) (results []Result, err error)
// Gets returns cached data for given keys, it is an alternative Get api
// for using with CAS. Gets returns a CAS identifier with the item. If
// the item's CAS value has changed since you Gets'ed it, it will not be stored.
Gets(keys ...string) (results []Result, err error)
// Set set the value with specified cache key.
Set(key string, flags uint16, timeout uint64, value []byte) (stored bool, err error)
// Add store the value only if it does not already exist.
Add(key string, flags uint16, timeout uint64, value []byte) (stored bool, err error)
// Replace replaces the value, only if the value already exists,
// for the specified cache key.
Replace(key string, flags uint16, timeout uint64, value []byte) (stored bool, err error)
// Append appends the value after the last bytes in an existing item.
Append(key string, flags uint16, timeout uint64, value []byte) (stored bool, err error)
// Prepend prepends the value before existing value.
Prepend(key string, flags uint16, timeout uint64, value []byte) (stored bool, err error)
// Cas stores the value only if no one else has updated the data since you read it last.
Cas(key string, flags uint16, timeout uint64, value []byte, cas uint64) (stored bool, err error)
// Delete delete the value for the specified cache key.
Delete(key string) (deleted bool, err error)
// FlushAll purges the entire cache.
FlushAll() (err error)
// Stats returns a list of basic stats.
Stats(argument string) (result []byte, err error)
// Close closes the CacheService
Close()
}
// Register a db connection.
func Register(name string, fn NewConnFunc) {
mu.Lock()
defer mu.Unlock()
if _, ok := services[name]; ok {
panic(fmt.Sprintf("register a registered key: %s", name))
}
services[name] = fn
}
// Connect returns a CacheService using the given config.
func Connect(config Config) (CacheService, error) {
mu.Lock()
defer mu.Unlock()
if DefaultCacheService == "" {
if len(services) == 1 {
for _, fn := range services {
return fn(config)
}
}
panic("there are more than one service connect func " +
"registered but no default cache service has been specified.")
}
fn, ok := services[DefaultCacheService]
if !ok {
panic(fmt.Sprintf("service connect function for given default cache service: %s is not found.", DefaultCacheService))
}
return fn(config)
}