cassandra

package module
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 3, 2026 License: MIT Imports: 8 Imported by: 0

README

Cassandra

A Cassandra storage driver using gocql/gocql

Release Discord Test

Table of Contents

Signatures
func New(config ...Config) (*Storage, error)
func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) GetWithContext(ctx context.Context, key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
func (s *Storage) SetWithContext(ctx context.Context, key string, val []byte, exp time.Duration) error
func (s *Storage) Delete(key string) error
func (s *Storage) DeleteWithContext(ctx context.Context, key string) error
func (s *Storage) Reset() error
func (s *Storage) ResetWithContext(ctx context.Context) error
func (s *Storage) Close() error
func (s *Storage) Conn() *gocql.Session
Installation

Cassandra is supported on the latest two versions of Go:

Install the cassandra implementation:

go get github.com/gofiber/storage/cassandra
Running the tests

This module uses Testcontainers for Go to run integration tests, which will start a local instance of Cassandra as a Docker container under the hood. To run the tests, you must have Docker (or another container runtime 100% compatible with the Docker APIs) installed on your machine.

Local development

Before running this implementation, you must ensure a Cassandra cluster is available. For local development, we recommend using the Cassandra Docker image; it contains everything necessary for the client to operate correctly.

To start Cassandra using Docker, issue the following:

docker run --name cassandra -p 9042:9042 -d cassandra:latest

After running this command, you're ready to start using the storage and connecting to the database.

Examples

You can use the following options to create a cassandra storage driver:

import "github.com/gofiber/storage/cassandra"

// Initialize default config, to connect to localhost:9042 using the memory engine and with a clean table.
store := New(Config{
    Hosts:     []string{"localhost:9042"},
    Keyspace: "test_keyspace_creation",
    Table:    "test_kv",
    Expiration : 10 * time.Minute,
})
Config
// Config defines the configuration options for the Cassandra storage
type Config struct {
    // Optional. Default is localhost
    // Hosts is a list of Cassandra nodes to connect to.
    Hosts []string
    // Optional. Default is gofiber
    // Keyspace is the name of the Cassandra keyspace to use.
    Keyspace string
    // Optional. Default is kv_store
    // Table is the name of the Cassandra table to use.
    Table string
    // Optional. Default is Quorum
    // Consistency is the Cassandra consistency level.
    Consistency gocql.Consistency
    // Optional. PoolConfig.HostSelectionPolicy = gocql.TokenAwareHostPolicy(gocql.RoundRobinHostPolicy())
    // PoolConfig is the Cassandra connection pool configuration.
    PoolConfig *gocql.PoolConfig
    // Optional. Default is false
    // SslOpts is the SSL options for the Cassandra cluster.
    SslOpts *gocql.SslOptions
    // Optional. Default is 10 minutes
    // Expiration is the time after which an entry is considered expired.
    Expiration time.Duration
    // Optional. Default is false
    // Reset is a flag to reset the database.
    Reset bool
    // Optional. Default is 3
    // MaxRetries is the maximum number of retries for a query.
    MaxRetries int
    // Optional. Default is 5 seconds
    // ConnectTimeout is the timeout for connecting to the Cassandra cluster.
    ConnectTimeout time.Duration
}
Default Config
var ConfigDefault = Config{
    Hosts:          []string{"localhost:9042"},
    Keyspace:       "gofiber",
    Table:          "kv_store",
    Consistency:    gocql.Quorum,
    Reset:          false,
    Expiration:     10 * time.Minute,
    MaxRetries:     3,
    ConnectTimeout: 5 * time.Second,
    SslOpts:        nil,
    PoolConfig: &gocql.PoolConfig{
        HostSelectionPolicy: gocql.TokenAwareHostPolicy(gocql.RoundRobinHostPolicy()),
    },
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is returned when the key does not exist
	ErrNotFound = fmt.Errorf("key not found")
	// ErrKeyExpired is returned when the key has expired
	ErrKeyExpired = fmt.Errorf("key expired")
)
View Source
var ConfigDefault = Config{
	Hosts:          []string{"localhost:9042"},
	Keyspace:       "gofiber",
	Table:          "kv_store",
	Consistency:    gocql.Quorum,
	Reset:          false,
	Expiration:     10 * time.Minute,
	MaxRetries:     3,
	ConnectTimeout: 5 * time.Second,
	SslOpts:        nil,
	PoolConfig: &gocql.PoolConfig{
		HostSelectionPolicy: gocql.TokenAwareHostPolicy(gocql.RoundRobinHostPolicy()),
	},
}

ConfigDefault is the default config

Functions

This section is empty.

Types

type Config

type Config struct {
	// Optional. Default is localhost
	// Hosts is a list of Cassandra nodes to connect to.
	Hosts []string
	// Optional. Default is gofiber
	// Keyspace is the name of the Cassandra keyspace to use.
	Keyspace string
	// Optional. Default is kv_store
	// Table is the name of the Cassandra table to use.
	Table string
	// Optional. Default is Quorum
	// Consistency is the Cassandra consistency level.
	Consistency gocql.Consistency
	// Optional. PoolConfig.HostSelectionPolicy = gocql.TokenAwareHostPolicy(gocql.RoundRobinHostPolicy())
	// PoolConfig is the Cassandra connection pool configuration.
	PoolConfig *gocql.PoolConfig
	// Optional. Default is false
	// SslOpts is the SSL options for the Cassandra cluster.
	SslOpts *gocql.SslOptions
	// Optional. Default is 10 minutes
	// Expiration is the time after which an entry is considered expired.
	Expiration time.Duration
	// Optional. Default is false
	// Reset is a flag to reset the database.
	Reset bool
	// Optional. Default is 3
	// MaxRetries is the maximum number of retries for a query.
	MaxRetries int
	// Optional. Default is 5 seconds
	// ConnectTimeout is the timeout for connecting to the Cassandra cluster.
	ConnectTimeout time.Duration
}

Config defines the configuration options for the Cassandra storage

type Storage

type Storage struct {
	// contains filtered or unexported fields
}

Storage represents a Cassandra storage implementation

func New

func New(cnfg Config) (*Storage, error)

New creates a new Cassandra storage instance

func (*Storage) Close

func (s *Storage) Close()

Close closes the storage connection. This method is not thread-safe and should not be called concurrently with other methods.

func (*Storage) Conn

func (s *Storage) Conn() *gocql.Session

Conn returns the underlying gocql session.

func (*Storage) Delete

func (s *Storage) Delete(key string) error

Delete removes a key from storage.

func (*Storage) DeleteWithContext added in v0.2.0

func (s *Storage) DeleteWithContext(ctx context.Context, key string) error

DeleteWithContext removes a key from storage with context support.

func (*Storage) Get

func (s *Storage) Get(key string) ([]byte, error)

Get retrieves a value by key.

func (*Storage) GetWithContext added in v0.2.0

func (s *Storage) GetWithContext(ctx context.Context, key string) ([]byte, error)

GetWithContext retrieves a value by key with context support.

func (*Storage) Reset

func (s *Storage) Reset() error

Reset clears all keys from storage.

func (*Storage) ResetWithContext added in v0.2.0

func (s *Storage) ResetWithContext(ctx context.Context) error

ResetWithContext clears all keys from storage with context support.

func (*Storage) Set

func (s *Storage) Set(key string, value []byte, exp time.Duration) error

Set stores a key-value pair with optional expiration

func (*Storage) SetWithContext added in v0.2.0

func (s *Storage) SetWithContext(ctx context.Context, key string, value []byte, exp time.Duration) error

SetWithContext stores a key-value pair with optional expiration with context support

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL