-
Notifications
You must be signed in to change notification settings - Fork 6
/
cache.go
42 lines (37 loc) · 1.13 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
package tilenol
import (
"errors"
)
var (
// ErrNoValue occurs when trying to access a value that doesn't exist in the cache
ErrNoValue = errors.New("No value exists in cache")
)
// CacheConfig is a generic YAML cache configuration object
type CacheConfig struct {
// Redis is an optional YAML key for configuring a RedisCache
Redis *RedisConfig `yaml:"redis"`
}
// Cache is a generic interface for a tile server cache
type Cache interface {
// Exists determines whether or not there is a cache value for the given key
Exists(key string) bool
// Get retrieves the cached data given a key
Get(key string) ([]byte, error)
// Put stores a new value in the cache at a given key
Put(key string, val []byte) error
}
// CreateCache creates a new generic Cache from a CacheConfig
func CreateCache(config *CacheConfig) (Cache, error) {
if config != nil {
if config.Redis != nil {
Logger.Debug("Using RedisCache configuration")
cache, err := NewRedisCache(config.Redis)
if err != nil {
return nil, err
}
return cache, nil
}
}
Logger.Debug("No cache configured, falling back to NilCache implementation")
return &NilCache{}, nil
}