forked from cafebazaar/mnemosyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
56 lines (49 loc) · 1.28 KB
/
cache.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
package mnemosyne
import (
"context"
"time"
"github.com/sirupsen/logrus"
)
type ICache interface {
Get(context.Context, string, interface{}) (*cachable, error)
Set(context.Context, string, *cachable) error
Delete(context.Context, string) error
Clear() error
TTL(context.Context, string) time.Duration
Name() string
}
type MemoryOpts struct {
maxMem int
}
type CacheOpts struct {
layerName string
layerType string
redisOpts RedisOpts
memOpts MemoryOpts
amnesiaChance int
compressionEnabled bool
cacheTTL time.Duration
cleanupInterval time.Duration
}
type baseCache struct {
layerName string
amnesiaChance int
compressionEnabled bool
}
func NewCacheLayer(opts *CacheOpts, watcher ITimer) ICache {
layerType := opts.layerType
if layerType == "memory" {
return NewInMemoryCache(opts)
} else if layerType == "tiny" {
return NewTinyCache(opts)
} else if layerType == "redis" {
return NewShardedClusterRedisCache(opts, watcher)
} else if layerType == "gaurdian" {
// to preserve backward-compatibility
return NewShardedClusterRedisCache(opts, watcher)
} else if layerType == "fastmemory" {
return NewFastMemoryCache(opts)
}
logrus.Errorf("Malformed: Unknown cache type %s", layerType)
return nil
}