-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
99 lines (88 loc) · 3.13 KB
/
option.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
package cache
import (
"encoding/json"
"fmt"
"github.com/jkratz55/redis-cache/compression/brotli"
"github.com/jkratz55/redis-cache/compression/flate"
"github.com/jkratz55/redis-cache/compression/gzip"
"github.com/jkratz55/redis-cache/compression/lz4"
)
// Option allows for the Cache behavior/configuration to be customized.
type Option func(c *Cache)
// Serialization allows for the marshalling and unmarshalling behavior to be
// customized for the Cache.
//
// A valid Marshaller and Unmarshaller must be provided. Providing nil for either
// will immediately panic.
func Serialization(mar Marshaller, unmar Unmarshaller) Option {
if mar == nil || unmar == nil {
panic(fmt.Errorf("nil Marshaller and/or Unmarshaller not permitted, illegal use of api"))
}
return func(c *Cache) {
c.marshaller = mar
c.unmarshaller = unmar
}
}
// JSON is a convenient Option for configuring Cache to use JSON for serializing
// data stored in the cache.
//
// JSON is the equivalent of using Serialization passing it a Marshaller and
// Unmarshaller using json.
func JSON() Option {
mar := func(v any) ([]byte, error) {
return json.Marshal(v)
}
unmar := func(data []byte, v any) error {
return json.Unmarshal(data, v)
}
return Serialization(mar, unmar)
}
// Compression allows for the values to be flated and deflated to conserve bandwidth
// and memory at the cost of higher CPU time. Compression accepts a Codec to handle
// compressing and decompressing the data to/from Redis.
func Compression(codec Codec) Option {
if codec == nil {
panic(fmt.Errorf("nil Codec not permitted, illegal use of API"))
}
return func(c *Cache) {
c.codec = codec
}
}
// Flate configures the Cache to use Flate Codec for compressing and decompressing
// values stored in Redis. Flate uses a default configuration favoring compression
// over speed.
func Flate() Option {
codec := &flate.Codec{Level: 9} // Best Compression
return Compression(codec)
}
// GZip configures the Cache to use gzip for compressing and decompressing values
// stored in Redis. GZip uses a default configuration favoring compression size
// over speed
func GZip() Option {
codec := gzip.NewCodec(9) // Best Compression
return Compression(codec)
}
// LZ4 configures the Cache to use lz4 for compressing and decompressing values
// stored in Redis.
func LZ4() Option {
codec := lz4.NewCodec()
return Compression(codec)
}
// Brotli configures the Cache to use Brotli for compressing and decompressing
// values stored in Redis. The default Brotli configuration uses a balanced
// approach between speed and compression level.
func Brotli() Option {
// While the other options favor the best compression with Brotli the default
// we use if balanced because benchmarks showed Brotli best compression was
// very slow in comparison to Gzip.
codec := brotli.NewCodec(6)
return Compression(codec)
}
// BatchMultiGets configures the Cache to use pipelining and split keys up into
// multiple MGET commands for increased throughput and lower latency when dealing
// with MGet operations with very large sets of keys.
func BatchMultiGets(batchSize int) Option {
return func(c *Cache) {
c.mgetBatch = batchSize
}
}