database

package module
v0.5.6 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2025 License: MIT Imports: 14 Imported by: 0

README

Go Redis Facade

Validate

Coop Redis Facade wraps simple interaction with Redis clients for CRUD operations by preventing race conditions between multiple client instances against singular instances of Redis.

If you are interested in how Sync between clients works, take a look at this post.

Module Documentation

https://pkg.go.dev/github.com/coopnorge/go-redis-facade

Mocks

To generate or update mocks use gomockhandler. gomockhandler is provided by golang-devtools.

Check mocks
docker compose run --rm golang-devtools gomockhandler -config ./gomockhandler.json check
Generate / Update mocks
docker compose run --rm golang-devtools gomockhandler -config ./gomockhandler.json mockgen

Documentation

Overview

Example
package main

import (
	"context"

	database "github.com/coopnorge/go-redis-facade"
)

func main() {
	encryptionConfig := database.EncryptionConfig{
		RedisKeyURI: "",
		Aad:         []byte{},
	}
	encryptor, err := database.NewEncryptionClient(encryptionConfig)
	if err != nil {
		panic(err)
	}

	redisConfig := database.Config{
		Address:           "",
		Password:          "",
		Database:          0,
		DialTimeout:       0,
		WriteTimeout:      0,
		ReadTimeout:       0,
		MaxRetries:        0,
		PoolSize:          0,
		PoolTimeout:       0,
		EncryptionEnabled: false,
	}
	redFacade, err := database.NewRedisFacade(redisConfig, encryptor)
	if err != nil {
		panic(err)
	}

	value, err := redFacade.Find(context.Background(), "key")
	if err != nil {
		panic(err)
	}

	println(value)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Address           string
	Password          string
	Database          int
	DialTimeout       time.Duration
	WriteTimeout      time.Duration
	ReadTimeout       time.Duration
	MaxRetries        int
	PoolSize          int
	PoolTimeout       time.Duration
	EncryptionEnabled bool
}

Config has all data to connect to redis

type Encryption

type Encryption interface {
	Encrypt(value []byte) ([]byte, error)
	Decrypt(ciphertext []byte) ([]byte, error)
}

Encryption defines interface

type EncryptionClient

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

EncryptionClient handles encryption and decryption tasks

func NewEncryptionClient

func NewEncryptionClient(c EncryptionConfig) (*EncryptionClient, error)

NewEncryptionClient creates a new client for encrypting and decrypting data

func (*EncryptionClient) Decrypt

func (e *EncryptionClient) Decrypt(ciphertext []byte) ([]byte, error)

Decrypt decrypts data

func (*EncryptionClient) Encrypt

func (e *EncryptionClient) Encrypt(value []byte) ([]byte, error)

Encrypt encrypts data

type EncryptionConfig

type EncryptionConfig struct {
	RedisKeyURI string
	Aad         []byte
}

EncryptionConfig is configuration needed to set up the encryption client

type ErrorType

type ErrorType int

ErrorType is the type of error that is returned

const (
	// ErrorTypeInvalid invalid error type
	ErrorTypeInvalid ErrorType = iota
	// KeyMissing error type
	KeyMissing
	// KeyExist error type
	KeyExist
	// SyncError will occur with resource locking
	SyncError
	// CommandError error type
	CommandError
	// OperationError error type
	OperationError
	// BytesConversionError error type
	BytesConversionError
)

type KeyValueStorage

type KeyValueStorage interface {
	// Save value in storage by key with expiration
	Save(ctx context.Context, key string, value string, expiration time.Duration) error
	// Update value in storage by key and update expiration
	Update(ctx context.Context, key string, value string, expiration time.Duration) error
	// Delete value in storage by key
	Delete(ctx context.Context, key string) (count int64, err error)
	// Find in storage by key
	Find(ctx context.Context, key string) (string, error)
	// FindKeys in storage by given pattern
	FindKeys(ctx context.Context, pattern string) ([]string, error)
	// IsAvailable will return error if there is trouble to access storage
	IsAvailable(ctx context.Context) error
	// Close connection
	Close() error
}

KeyValueStorage defines interface

type RedisFacade

type RedisFacade struct {
	sync.Mutex
	// contains filtered or unexported fields
}

RedisFacade storage

func NewRedisFacade

func NewRedisFacade(config Config, encryption Encryption) (*RedisFacade, error)

NewRedisFacade makes new connection to key value storage

func (*RedisFacade) Close

func (rf *RedisFacade) Close() error

Close connection

func (*RedisFacade) Delete

func (rf *RedisFacade) Delete(ctx context.Context, key string) (count int64, err error)

Delete value in storage by key

func (*RedisFacade) Find

func (rf *RedisFacade) Find(ctx context.Context, key string) (v string, err error)

Find in storage by key

func (*RedisFacade) FindKeys

func (rf *RedisFacade) FindKeys(ctx context.Context, pattern string) (redisKeysVal []string, err error)

FindKeys in storage by given pattern

func (*RedisFacade) IsAvailable

func (rf *RedisFacade) IsAvailable(ctx context.Context) error

IsAvailable will return error if there is trouble to access storage

func (*RedisFacade) Save

func (rf *RedisFacade) Save(ctx context.Context, key string, value string, expiration time.Duration) (err error)

Save value in storage by key with expiration

func (*RedisFacade) Update

func (rf *RedisFacade) Update(ctx context.Context, key string, value string, expiration time.Duration) (err error)

Update value in storage by key and update expiration

type StorageFacadeError

type StorageFacadeError struct {
	Type    ErrorType
	Details string
}

StorageFacadeError is a custom error type used for error handling

func (*StorageFacadeError) Error

func (e *StorageFacadeError) Error() string

Directories

Path Synopsis
internal
generated/mocks
Package mock_database is a generated GoMock package.
Package mock_database is a generated GoMock package.

Jump to

Keyboard shortcuts

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