|
| 1 | +--- |
| 2 | +id: scylladb |
| 3 | +title: ScyllaDb |
| 4 | +--- |
| 5 | + |
| 6 | + |
| 7 | +[](https://gofiber.io/discord) |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +# ScyllaDb |
| 13 | + |
| 14 | +A ScyllaDb storage engine for [Fiber](github.com/gofiber/fiber) using [gocql](github.com/scylladb/gocql). |
| 15 | + |
| 16 | +### Table of Contents |
| 17 | +- [Signatures](#signatures) |
| 18 | +- [Installation](#installation) |
| 19 | +- [Examples](#examples) |
| 20 | +- [Config](#config) |
| 21 | +- [Default Config](#default-config) |
| 22 | + |
| 23 | +### Signatures |
| 24 | +```go |
| 25 | +func New(config ...Config) Storage |
| 26 | +func (s *Storage) Get(key string) ([]byte, error) |
| 27 | +func (s *Storage) Set(key string, value []byte, expire 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() *gocql.Session |
| 32 | +``` |
| 33 | + |
| 34 | +### Installation |
| 35 | +ScyllaDb is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet: |
| 36 | +```bash |
| 37 | +go mod init github.com/<user>/<repo> |
| 38 | +``` |
| 39 | +And then install the scylladb implementation: |
| 40 | +```bash |
| 41 | +go get github.com/gofiber/storage/scylladb |
| 42 | +``` |
| 43 | + |
| 44 | +### Examples |
| 45 | +Import the storage package. |
| 46 | +```go |
| 47 | +import "github.com/gofiber/storage/scylladb" |
| 48 | +``` |
| 49 | + |
| 50 | +You can use the following possibilities to create a storage: |
| 51 | +```go |
| 52 | +// Initialize default config |
| 53 | +store := scylladb.New() |
| 54 | + |
| 55 | +// Initialize custom config |
| 56 | +store := scylladb.New(scylladb.Config{ |
| 57 | + Keyspace: "fiber", |
| 58 | + Hosts: []string{"127.0.0.1"}, |
| 59 | + Port: 9042, |
| 60 | + Table: "fiber_storage", |
| 61 | + Consistency: "ONE", |
| 62 | + Reset: false, |
| 63 | +}) |
| 64 | + |
| 65 | +// Initialize with support for TLS (SslOptions configures TLS use) |
| 66 | +// |
| 67 | +// InsecureSkipVerify and EnableHostVerification interact as follows: |
| 68 | +// |
| 69 | +// |Config.InsecureSkipVerify | EnableHostVerification | Result | |
| 70 | +// |--------------------------|------------------------|--------------------| |
| 71 | +// |Config is nil | false | do not verify host | |
| 72 | +// |Config is nil | true | verify host | |
| 73 | +// |false | false | verify host | |
| 74 | +// |true | false | do not verify host | |
| 75 | +// |false | true | verify host | |
| 76 | +// |true | true | verify host | |
| 77 | +store := New( |
| 78 | + Config{ |
| 79 | + Keyspace: "fiber", |
| 80 | + Hosts: []string{"127.0.0.1"}, |
| 81 | + Port: 9042, |
| 82 | + Table: "fiber_storage", |
| 83 | + Consistency: "ONE", |
| 84 | + SslOpts: &gocql.SslOptions{ |
| 85 | + Config: &tls.Config{ |
| 86 | + InsecureSkipVerify: false, // Set this too false to enable certificate verification |
| 87 | + }, |
| 88 | + CertPath: "/path/to/client_cert.pem", // Path to the client certificate |
| 89 | + KeyPath: "/path/to/client_key.pem", // Path to the client certificate's private key |
| 90 | + CaPath: "/path/to/ca_cert.pem", // Path to the CA certificate |
| 91 | + EnableHostVerification: true, // Enable hostname verification |
| 92 | + }, |
| 93 | + Reset: false, |
| 94 | + }, |
| 95 | +) |
| 96 | + |
| 97 | +// Initialize custom config using scylladb connection |
| 98 | +cluster, _ := gocql.NewCluster("127.0.0.1") |
| 99 | +cluster.Keyspace = "fiber" |
| 100 | +cluster.Port = 9042 |
| 101 | + |
| 102 | +session, _ := cluster.CreateSession() |
| 103 | +store := scylladb.New(scylladb.Config{ |
| 104 | + Session: session, |
| 105 | + Keyspace: "fiber", |
| 106 | + Table: "fiber_storage", |
| 107 | + Reset: false, |
| 108 | +}) |
| 109 | +``` |
| 110 | + |
| 111 | +### Config |
| 112 | +```go |
| 113 | +type Config struct { |
| 114 | + // Session is provided by the user to use an existing ScyllaDb session |
| 115 | + // Session Will override Keyspace and all other authentication values if used |
| 116 | + // |
| 117 | + // Optional. Default is nil |
| 118 | + Session *gocql.Session |
| 119 | + |
| 120 | + // Keyspace name |
| 121 | + // |
| 122 | + // Optional. Default is "fiber" |
| 123 | + Keyspace string |
| 124 | + |
| 125 | + // Hosts are an array of network addresses for establishing initial connections |
| 126 | + // You have the flexibility to specify one or multiple addresses as needed |
| 127 | + // |
| 128 | + // Optional. Default is "127.0.0.1" |
| 129 | + Hosts []string |
| 130 | + |
| 131 | + // Port where the ScyllaDb cluster is listening on |
| 132 | + // |
| 133 | + // Optional. Default is 9042 |
| 134 | + Port int |
| 135 | + |
| 136 | + // Username for ScyllaDb cluster |
| 137 | + // |
| 138 | + // Optional. Default is "" |
| 139 | + Username string |
| 140 | + |
| 141 | + // Password for ScyllaDb cluster |
| 142 | + // |
| 143 | + // Optional. Default is "" |
| 144 | + Password string |
| 145 | + |
| 146 | + // Table name |
| 147 | + // |
| 148 | + // Optional. Default is "fiber_storage" |
| 149 | + Table string |
| 150 | + |
| 151 | + // Level of the consistency |
| 152 | + // |
| 153 | + // Optional. Default is "LOCAL_ONE" |
| 154 | + Consistency string |
| 155 | + |
| 156 | + // SslOpts configures TLS use. |
| 157 | + // |
| 158 | + // Optional. Default is nil |
| 159 | + SslOpts *gocql.SslOptions |
| 160 | + |
| 161 | + // Reset clears any existing keys in existing Table |
| 162 | + // |
| 163 | + // Optional. Default is false |
| 164 | + Reset bool |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +### Default Config |
| 169 | +```go |
| 170 | +// ConfigDefault is the default config |
| 171 | +var ConfigDefault = Config{ |
| 172 | + Session: nil, |
| 173 | + Keyspace: "fiber", |
| 174 | + Hosts: []string{"127.0.0.1"}, |
| 175 | + Username: "", |
| 176 | + Password: "", |
| 177 | + Port: 9042, |
| 178 | + Table: "fiber_storage", |
| 179 | + Consistency: "LOCAL_ONE", |
| 180 | + SslOpts: nil, |
| 181 | + Reset: false, |
| 182 | +} |
| 183 | +``` |
0 commit comments