-
Notifications
You must be signed in to change notification settings - Fork 8
/
types.go
52 lines (44 loc) · 1.46 KB
/
types.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
package onecache
import (
"errors"
"time"
)
const (
EXPIRES_DEFAULT = time.Duration(0)
EXPIRES_FOREVER = time.Duration(-1)
)
var (
ErrCacheMiss = errors.New("Key not found")
ErrCacheNotStored = errors.New("Data not stored")
ErrCacheNotSupported = errors.New("Operation not supported")
ErrCacheDataCannotBeIncreasedOrDecreased = errors.New(`
Data isn't an integer/string type. Hence, it cannot be increased or decreased`)
)
// DefaultKeyFunc is the default implementation of cache keys
// All it does is to preprend "onecache:" to the key sent in by client code
func DefaultKeyFunc(s string) string {
return "onecache:" + s
}
//Item identifes a cached piece of data
type Item struct {
ExpiresAt time.Time
Data []byte
}
//Interface for all onecache store implementations
type Store interface {
Set(key string, data []byte, expires time.Duration) error
Get(key string) ([]byte, error)
Delete(key string) error
Flush() error
Has(key string) bool
}
//Some stores like redis and memcache automatically clear out the cache
//But for the filesystem and in memory, this cannot be said.
//Stores that have to manually clear out the cached data should implement this method.
//It's implementation should re run this function everytime the interval is reached
//Say every 5 minutes.
type GarbageCollector interface {
GC()
}
// KeyFunc defines a transformer for cache keys
type KeyFunc func(s string) string