|
| 1 | +--- |
| 2 | +id: valkey |
| 3 | +title: Valkey |
| 4 | +--- |
| 5 | + |
| 6 | + |
| 7 | +[](https://gofiber.io/discord) |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +A fast Valkey Storage that does auto pipelining and supports client side caching. Implementation is based on [valkey-io/valkey](https://github.com/valkey-io/valkey-go). |
| 13 | + |
| 14 | +### Table of Contents |
| 15 | + |
| 16 | +- [Signatures](#signatures) |
| 17 | +- [Installation](#installation) |
| 18 | +- [Examples](#examples) |
| 19 | +- [Config](#config) |
| 20 | +- [Default Config](#default-config) |
| 21 | + |
| 22 | +### Signatures |
| 23 | + |
| 24 | +```go |
| 25 | +func New(config ...Config) Storage |
| 26 | +func (s *Storage) Get(key string) ([]byte, error) |
| 27 | +func (s *Storage) Set(key string, val []byte, exp time.Duration) error |
| 28 | +func (s *Storage) Delete(key string) error |
| 29 | +func (s *Storage) Reset() error |
| 30 | +func (s *Storage) Close() error |
| 31 | +func (s *Storage) Conn() valkey.Client |
| 32 | +``` |
| 33 | + |
| 34 | +### Installation |
| 35 | + |
| 36 | +The valkey driver is tested on the latest two [Go version](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet: |
| 37 | + |
| 38 | +```bash |
| 39 | +go mod init github.com/<user>/<repo> |
| 40 | +``` |
| 41 | + |
| 42 | +And then install the valkey implementation: |
| 43 | + |
| 44 | +```bash |
| 45 | +go get github.com/gofiber/storage/valkey |
| 46 | +``` |
| 47 | + |
| 48 | +### Examples |
| 49 | + |
| 50 | +Import the storage package. |
| 51 | + |
| 52 | +```go |
| 53 | +import "github.com/gofiber/storage/valkey" |
| 54 | +``` |
| 55 | + |
| 56 | +You can use the one of the following options to create a Valkey Storage: |
| 57 | + |
| 58 | +```go |
| 59 | +// Initialize default config (localhost:6379) |
| 60 | +store := valkey.New() |
| 61 | + |
| 62 | +// Initialize custom config |
| 63 | +store := valkey.New(valkey.Config{ |
| 64 | + InitAddress: []string{"localhost:6380"}, |
| 65 | + Username: "", |
| 66 | + Password: "", |
| 67 | + Database: 0, |
| 68 | + Reset: false, |
| 69 | + TLSConfig: nil, |
| 70 | +}) |
| 71 | + |
| 72 | +// Initialize using Redis-style URL |
| 73 | +store := valkey.New(valkey.Config{ |
| 74 | + URL: "redis://localhost:6379", |
| 75 | +}) |
| 76 | + |
| 77 | +// Initialize Valkey Cluster Client |
| 78 | +store := valkey.New(valkey.Config{ |
| 79 | + InitAddress: []string{":6379", ":6380"}, |
| 80 | +}) |
| 81 | + |
| 82 | +// Create a client with support for TLS |
| 83 | +cer, err := tls.LoadX509KeyPair("./client.crt", "./client.key") |
| 84 | +if err != nil { |
| 85 | + log.Println(err) |
| 86 | + return |
| 87 | +} |
| 88 | +tlsCfg := &tls.Config{ |
| 89 | + MinVersion: tls.VersionTLS12, |
| 90 | + InsecureSkipVerify: true, |
| 91 | + Certificates: []tls.Certificate{cer}, |
| 92 | +} |
| 93 | +store = valkey.New(valkey.Config{ |
| 94 | + InitAddress: []string{"localhost:6380"}, |
| 95 | + Username: "<user>", |
| 96 | + Password: "<password>", |
| 97 | + SelectDB: 0, |
| 98 | + TLSConfig: tlsCfg, |
| 99 | +}) |
| 100 | + |
| 101 | +``` |
| 102 | + |
| 103 | +### Config |
| 104 | + |
| 105 | +```go |
| 106 | +type Config struct { |
| 107 | + // Server username |
| 108 | + // |
| 109 | + // Optional. Default is "" |
| 110 | + Username string |
| 111 | + |
| 112 | + // Server password |
| 113 | + // |
| 114 | + // Optional. Default is "" |
| 115 | + Password string |
| 116 | + |
| 117 | + // ClientName will execute the `CLIENT SETNAME ClientName` command for each conn. |
| 118 | + // |
| 119 | + // Optional. Default is "" |
| 120 | + ClientName string |
| 121 | + |
| 122 | + // URL standard format Redis-style URL. If this is set all other config options, InitAddress, Username, Password, ClientName, and SelectDB have no effect. |
| 123 | + // |
| 124 | + // Example: redis://<user>:<pass>@localhost:6379/<db> |
| 125 | + // Optional. Default is "" |
| 126 | + URL string |
| 127 | + |
| 128 | + // SelectDB to be selected after connecting to the server. |
| 129 | + // |
| 130 | + // Optional. Default is 0 |
| 131 | + SelectDB int |
| 132 | + |
| 133 | + // Either a single address or a seed list of host:port addresses, this enables FailoverClient and ClusterClient |
| 134 | + // |
| 135 | + // Optional. Default is []string{"127.0.0.1:6379"} |
| 136 | + InitAddress []string |
| 137 | + |
| 138 | + // TLS Config to use. When set TLS will be negotiated. |
| 139 | + // |
| 140 | + // Optional. Default is nil |
| 141 | + TLSConfig *tls.Config |
| 142 | + |
| 143 | + // CacheSizeEachConn is valkey client side cache size that bind to each TCP connection to a single valkey instance. |
| 144 | + // |
| 145 | + // Optional. The default is DefaultCacheBytes: 128 * (1 << 20) |
| 146 | + CacheSizeEachConn int |
| 147 | + |
| 148 | + // RingScaleEachConn sets the size of the ring buffer in each connection to (2 ^ RingScaleEachConn). |
| 149 | + // |
| 150 | + // Optional. The default is RingScaleEachConn, which results into having a ring of size 2^10 for each connection. |
| 151 | + RingScaleEachConn int |
| 152 | + |
| 153 | + // ReadBufferEachConn is the size of the bufio.NewReaderSize for each connection, default to DefaultReadBuffer (0.5 MiB). |
| 154 | + // |
| 155 | + // Optional. The default is DefaultReadBuffer: 1 << 19 |
| 156 | + ReadBufferEachConn int |
| 157 | + |
| 158 | + // WriteBufferEachConn is the size of the bufio.NewWriterSize for each connection, default to DefaultWriteBuffer (0.5 MiB). |
| 159 | + // |
| 160 | + // Optional. The default is DefaultWriteBuffer: 1 << 19 |
| 161 | + WriteBufferEachConn int |
| 162 | + |
| 163 | + // BlockingPoolSize is the size of the connection pool shared by blocking commands (ex BLPOP, XREAD with BLOCK). |
| 164 | + // |
| 165 | + // Optional. The default is DefaultPoolSize: 1000 |
| 166 | + BlockingPoolSize int |
| 167 | + |
| 168 | + // PipelineMultiplex determines how many tcp connections used to pipeline commands to one valkey instance. |
| 169 | + // |
| 170 | + // Optional. The default for single and sentinel clients is 2, which means 4 connections (2^2). |
| 171 | + PipelineMultiplex int |
| 172 | + |
| 173 | + // DisableRetry disables retrying read-only commands under network errors |
| 174 | + // |
| 175 | + // Optional. The default is False |
| 176 | + DisableRetry bool |
| 177 | + |
| 178 | + // DisableCache falls back Client.DoCache/Client.DoMultiCache to Client.Do/Client.DoMulti |
| 179 | + // |
| 180 | + // Optional. The default is false |
| 181 | + DisableCache bool |
| 182 | + |
| 183 | + // AlwaysPipelining makes valkey.Client always pipeline valkey commands even if they are not issued concurrently. |
| 184 | + // |
| 185 | + // Optional. The default is true |
| 186 | + AlwaysPipelining bool |
| 187 | + |
| 188 | + // Reset clears any existing keys in existing Collection |
| 189 | + // |
| 190 | + // Optional. Default is false |
| 191 | + Reset bool |
| 192 | + |
| 193 | + // CacheTTL TTL |
| 194 | + // |
| 195 | + // Optional. Default is time.Minute |
| 196 | + CacheTTL time.Duration |
| 197 | +} |
| 198 | +``` |
| 199 | + |
| 200 | +### Default Config |
| 201 | + |
| 202 | +```go |
| 203 | +var ConfigDefault = Config{ |
| 204 | + Username: "", |
| 205 | + Password: "", |
| 206 | + ClientName: "", |
| 207 | + SelectDB: 0, |
| 208 | + InitAddress: []string{"127.0.0.1:6379"}, |
| 209 | + TLSConfig: nil, |
| 210 | + CacheSizeEachConn: valkey.DefaultCacheBytes, |
| 211 | + RingScaleEachConn: valkey.DefaultRingScale, |
| 212 | + ReadBufferEachConn: valkey.DefaultReadBuffer, |
| 213 | + WriteBufferEachConn: valkey.DefaultWriteBuffer, |
| 214 | + BlockingPoolSize: valkey.DefaultPoolSize, |
| 215 | + PipelineMultiplex: 2, |
| 216 | + DisableRetry: false, |
| 217 | + DisableCache: false, |
| 218 | + AlwaysPipelining: true, |
| 219 | + Reset: false, |
| 220 | + CacheTTL: time.Minute, |
| 221 | +} |
| 222 | +``` |
0 commit comments