redisclient

package module
v1.0.0-1a Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

README

Sure, here's a README.md template you can use for your redisclient package:

Redis Client Library

This library provides a Go client for Redis with convenient methods for common operations.

Installation

To use this library in your Go module, run:

go get "github.com/dexterdmonkey/go-redisclient"

Usage

Initializing the Client
package main

import (
    "fmt"
    "time"

    "github.com/dexterdmonkey/go-redisclient"
)

func main() {
    // Initialize Redis client options
    options := redisclient.ClientOptions{
        Host:     "localhost",
        Port:     6379,
        Password: "",
        DB:       0,
        PoolSize: 10,
    }

    // Create a new Redis client
    client, err := redisclient.New(options)
    if err != nil {
        fmt.Printf("Error creating Redis client: %v\n", err)
        return
    }
    
    defer client.Close()

    // Example usage of Redis operations
    err = client.Set("mykey", "myvalue", 0)
    if err != nil {
        fmt.Printf("Error setting value: %v\n", err)
        return
    }

    val, err := client.Get("mykey")
    if err != nil {
        fmt.Printf("Error getting value: %v\n", err)
        return
    }
    fmt.Printf("Value for key 'mykey': %s\n", val)
}
Available Methods
  • LPush(key string, values ...interface{}) error: Pushes one or multiple values to the left end of a list.
  • RPush(key string, values ...interface{}) error: Pushes one or multiple values to the right end of a list.
  • BRPop(timeout time.Duration, keys ...string) ([]string, error): Blocks and pops an element from the right end of one of the lists, with a timeout.
  • BLPop(timeout time.Duration, keys ...string) ([]string, error): Blocks and pops an element from the left end of one of the lists, with a timeout.
  • Set(key string, value interface{}, expirations ...time.Duration) error: Sets a key-value pair in Redis, with an optional expiration time.
  • Get(key string) (string, error): Retrieves the value associated with a key from Redis.
  • Del(keys ...string) (int64, error): Deletes one or multiple keys from Redis.
  • Clear() error: Clears all keys and data from the current Redis database.
Mock Implementation

The package also includes a mock implementation (NewMock()) of the Redis client interface (Interface) for testing purposes. The mock stores data in-memory and supports basic Redis operations.

Contributing

Contributions are welcome! Feel free to open issues or pull requests for any improvements or fixes.

License

This project is licensed under the Apache License, Version 2.0 - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client represents a Redis client with context.

func New

func New(options ClientOptions) (*Client, error)

New creates a new Redis client with the given options.

func (*Client) BLPop

func (r *Client) BLPop(timeout time.Duration, keys ...string) ([]string, error)

BLPop removes and returns the first element of the list stored at keys, blocking until one is available.

func (*Client) BRPop

func (r *Client) BRPop(timeout time.Duration, keys ...string) ([]string, error)

BRPop removes and returns the last element of the list stored at keys, blocking until one is available.

func (*Client) Clear

func (r *Client) Clear() error

Clear flushes the database.

func (*Client) Del

func (r *Client) Del(keys ...string) (int64, error)

Del deletes the specified keys.

func (*Client) Get

func (r *Client) Get(key string) (string, error)

Get gets the value of key. If the key does not exist, it returns an error.

func (*Client) LPopCount

func (r *Client) LPopCount(key string, count int) ([]string, error)

LPopCount pops 'count' values from the left of the list stored at key.

func (*Client) LPush

func (r *Client) LPush(key string, values ...interface{}) error

LPush pushes values onto the left of the list stored at key.

func (*Client) RPopCount

func (r *Client) RPopCount(key string, count int) ([]string, error)

RPopCount pops 'count' values from the right of the list stored at key.

func (*Client) RPush

func (r *Client) RPush(key string, values ...interface{}) error

RPush pushes values onto the right of the list stored at key.

func (*Client) Set

func (r *Client) Set(key string, value interface{}, expirations ...time.Duration) error

Set sets key to hold the string value. If expiration is provided, it sets an expiration time on the key.

func (*Client) SetContext

func (r *Client) SetContext(ctx context.Context) *Client

SetContext sets a new context for the Redis client.

type ClientOptions

type ClientOptions struct {
	Host     string // Host of the Redis server.
	Port     int    // Port of the Redis server.
	Password string // Password for authentication with the Redis server.
	DB       int    // Redis database number to select.
	PoolSize int    // Maximum number of connections in the pool.
}

ClientOptions holds the options for creating a new Redis client.

type Interface

type Interface interface {
	LPush(key string, values ...interface{}) error
	RPush(key string, values ...interface{}) error
	BRPop(timeout time.Duration, keys ...string) ([]string, error)
	BLPop(timeout time.Duration, keys ...string) ([]string, error)
	Set(key string, value interface{}, expirations ...time.Duration) error
	Get(key string) (string, error)
	Del(keys ...string) (int64, error)
	Clear() error
	LPopCount(key string, count int) ([]string, error)
	RPopCount(key string, count int) ([]string, error)
	SetContext(ctx context.Context) *Client
}

Interface defines the methods that a Redis client should implement.

type Mock

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

Mock represents an in-memory mock implementation of the redisclient.Interface

func NewMock

func NewMock() *Mock

NewMock creates a new instance of Mock

func (*Mock) BLPop

func (m *Mock) BLPop(timeout time.Duration, keys ...string) ([]string, error)

func (*Mock) BRPop

func (m *Mock) BRPop(timeout time.Duration, keys ...string) ([]string, error)

func (*Mock) Clear

func (m *Mock) Clear() error

func (*Mock) Del

func (m *Mock) Del(keys ...string) (int64, error)

func (*Mock) Get

func (m *Mock) Get(key string) (string, error)

func (*Mock) LPopCount

func (m *Mock) LPopCount(key string, count int) ([]string, error)

func (*Mock) LPush

func (m *Mock) LPush(key string, values ...interface{}) error

func (*Mock) RPopCount

func (m *Mock) RPopCount(key string, count int) ([]string, error)

func (*Mock) RPush

func (m *Mock) RPush(key string, values ...interface{}) error

func (*Mock) Set

func (m *Mock) Set(key string, value interface{}, expirations ...time.Duration) error

func (*Mock) SetContext

func (m *Mock) SetContext(ctx context.Context) *Client

Jump to

Keyboard shortcuts

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