simpleredis

package module
Version: v0.0.0-...-3ef49f0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2020 License: MIT Imports: 6 Imported by: 51

README

Simple Redis

Build Status GoDoc Go Report Card

Easy way to use Redis from Go.

Packaging status

Online API Documentation

godoc.org

Features and limitations

  • Supports simple use of lists, hashmaps, sets and key/values
  • Deals mainly with strings
  • Uses the redigo package

Example usage

package main

import (
	"log"

	"github.com/xyproto/simpleredis"
)

func main() {
	// Check if the redis service is up
	if err := simpleredis.TestConnection(); err != nil {
		log.Fatalln("Could not connect to Redis. Is the service up and running?")
	}

	// Use instead for testing if a different host/port is up.
	// simpleredis.TestConnectionHost("localhost:6379")

	// Create a connection pool, connect to the given redis server
	pool := simpleredis.NewConnectionPool()

	// Use this for connecting to a different redis host/port
	// pool := simpleredis.NewConnectionPoolHost("localhost:6379")

	// For connecting to a different redis host/port, with a password
	// pool := simpleredis.NewConnectionPoolHost("password@redishost:6379")

	// Close the connection pool right after this function returns
	defer pool.Close()

	// Create a list named "greetings"
	list := simpleredis.NewList(pool, "greetings")

	// Add "hello" to the list, check if there are errors
	if list.Add("hello") != nil {
		log.Fatalln("Could not add an item to list!")
	}

	// Get the last item of the list
	if item, err := list.GetLast(); err != nil {
		log.Fatalln("Could not fetch the last item from the list!")
	} else {
		log.Println("The value of the stored item is:", item)
	}

	// Remove the list
	if list.Remove() != nil {
		log.Fatalln("Could not remove the list!")
	}
}

Testing

Redis must be up and running locally for the go test tests to work.

Timeout issues

If there are timeout issues when connecting to Redis, try consulting the Redis latency doctor on the server by running redis-cli and then latency doctor.

Version, license and author

  • Version: 2.6.0
  • License: MIT
  • Author: Alexander F. Rødseth <xyproto@archlinux.org>

Documentation

Overview

Package simpleredis provides an easy way to use Redis.

Index

Constants

View Source
const (
	// Version number. Stable API within major version numbers.
	Version = 2.6
)

Variables

This section is empty.

Functions

func ConnectTimeout

func ConnectTimeout() time.Duration

ConnectTimeout returns the current connect timeout for new connections

func IdleTimeout

func IdleTimeout() time.Duration

IdleTimeout returns the current idle timeout for new connections

func ReadTimeout

func ReadTimeout() time.Duration

ReadTimeout returns the current read timeout for new connections

func SetConnectTimeout

func SetConnectTimeout(t time.Duration)

SetConnectTimeout sets the connect timeout for new connections

func SetIdleTimeout

func SetIdleTimeout(t time.Duration)

SetIdleTimeout sets the idle timeout for new connections

func SetMaxIdleConnections

func SetMaxIdleConnections(maximum int)

Set the number of maximum *idle* connections standing ready when creating new connection pools. When an idle connection is used, a new idle connection is created. The default is 3 and should be fine for most cases.

func SetReadTimeout

func SetReadTimeout(t time.Duration)

SetReadTimeout sets the read timeout for new connections

func SetWriteTimeout

func SetWriteTimeout(t time.Duration)

SetWriteTimeout sets the write timeout for new connections

func TestConnection

func TestConnection() (err error)

Test if the local Redis server is up and running

func TestConnectionHost

func TestConnectionHost(hostColonPort string) (err error)

Test if a given Redis server at host:port is up and running. Does not try to PING or AUTH.

func WriteTimeout

func WriteTimeout() time.Duration

WriteTimeout returns the current write timeout for new connections

Types

type ConnectionPool

type ConnectionPool redis.Pool

A pool of readily available Redis connections

func NewConnectionPool

func NewConnectionPool() *ConnectionPool

Create a new connection pool

func NewConnectionPoolHost

func NewConnectionPoolHost(hostColonPort string) *ConnectionPool

Create a new connection pool given a host:port string. A password may be supplied as well, on the form "password@host:port".

func (*ConnectionPool) Close

func (pool *ConnectionPool) Close()

Close down the connection pool

func (*ConnectionPool) Get

func (pool *ConnectionPool) Get(dbindex int) redis.Conn

Get one of the available connections from the connection pool, given a database index

func (*ConnectionPool) Ping

func (pool *ConnectionPool) Ping() error

Ping the server by sending a PING command

type HashMap

type HashMap redisDatastructure

func NewHashMap

func NewHashMap(pool *ConnectionPool, id string) *HashMap

Create a new hashmap

func (*HashMap) All

func (rh *HashMap) All() ([]string, error)

Get all elementid's for all hash elements

func (*HashMap) Clear

func (rh *HashMap) Clear() error

Clear the contents

func (*HashMap) Del

func (rh *HashMap) Del(elementid string) error

Remove an element (for instance a user)

func (*HashMap) DelKey

func (rh *HashMap) DelKey(elementid, key string) error

Remove a key for an entry in a hashmap (for instance the email field for a user)

func (*HashMap) Exists

func (rh *HashMap) Exists(elementid string) (bool, error)

Check if a given elementid exists as a hash map at all

func (*HashMap) Get

func (rh *HashMap) Get(elementid, key string) (string, error)

Get a value from a hashmap given the element id (for instance a user id) and the key (for instance "password")

func (*HashMap) GetAll

func (rh *HashMap) GetAll() ([]string, error)

Deprecated

func (*HashMap) Has

func (rh *HashMap) Has(elementid, key string) (bool, error)

Check if a given elementid + key is in the hash map

func (*HashMap) Keys

func (rh *HashMap) Keys(elementid string) ([]string, error)

Keys returns the keys of the given elementid.

func (*HashMap) Remove

func (rh *HashMap) Remove() error

Remove this hashmap (all keys that starts with this hashmap id and a colon)

func (*HashMap) SelectDatabase

func (rh *HashMap) SelectDatabase(dbindex int)

Select a different database

func (*HashMap) Set

func (rh *HashMap) Set(elementid, key, value string) error

Set a value in a hashmap given the element id (for instance a user id) and the key (for instance "password")

func (*HashMap) SetExpire

func (rh *HashMap) SetExpire(elementid, key, value string, expire time.Duration) error

Given an element id, set a key and a value together with an expiration time

type KeyValue

type KeyValue redisDatastructure

func NewKeyValue

func NewKeyValue(pool *ConnectionPool, id string) *KeyValue

Create a new key/value

func (*KeyValue) Clear

func (rkv *KeyValue) Clear() error

Clear the contents

func (*KeyValue) Del

func (rkv *KeyValue) Del(key string) error

Remove a key

func (*KeyValue) Get

func (rkv *KeyValue) Get(key string) (string, error)

Get a value given a key

func (*KeyValue) Inc

func (rkv *KeyValue) Inc(key string) (string, error)

Increase the value of a key, returns the new value Returns an empty string if there were errors, or "0" if the key does not already exist.

func (*KeyValue) Remove

func (rkv *KeyValue) Remove() error

Remove this key/value

func (*KeyValue) SelectDatabase

func (rkv *KeyValue) SelectDatabase(dbindex int)

Select a different database

func (*KeyValue) Set

func (rkv *KeyValue) Set(key, value string) error

Set a key and value

func (*KeyValue) SetExpire

func (rkv *KeyValue) SetExpire(key, value string, expire time.Duration) error

Set a key and value, with expiry

func (*KeyValue) TimeToLive

func (rkv *KeyValue) TimeToLive(key string) (time.Duration, error)

TimeToLive returns how long a key has to live until it expires Returns a duration of 0 when the time has passed

type List

type List redisDatastructure

func NewList

func NewList(pool *ConnectionPool, id string) *List

Create a new list

func (*List) Add

func (rl *List) Add(value string) error

Default Add, aliased to List.AddStart

func (*List) AddEnd

func (rl *List) AddEnd(value string) error

Add an element to the end of the list list

func (*List) AddStart

func (rl *List) AddStart(value string) error

Add an element to the start of the list

func (*List) All

func (rl *List) All() ([]string, error)

Get all elements of a list

func (*List) Clear

func (rl *List) Clear() error

Clear the contents

func (*List) Get

func (rl *List) Get(index int64) (string, error)

Returns the element at index index in the list

func (*List) GetAll

func (rl *List) GetAll() ([]string, error)

Deprecated

func (*List) GetLast

func (rl *List) GetLast() (string, error)

Deprecated

func (*List) GetLastN

func (rl *List) GetLastN(n int) ([]string, error)

Deprecated

func (*List) Last

func (rl *List) Last() (string, error)

Get the last element of a list

func (*List) LastN

func (rl *List) LastN(n int) ([]string, error)

Get the last N elements of a list

func (*List) PopFirst

func (rl *List) PopFirst() (string, error)

Removes and returns the first element of the list

func (*List) PopLast

func (rl *List) PopLast() (string, error)

Removes and returns the last element of the list

func (*List) Remove

func (rl *List) Remove() error

Remove this list

func (*List) RemoveElement

func (rl *List) RemoveElement(value string) error

Remove the first occurrence of an element from the list

func (*List) SelectDatabase

func (rl *List) SelectDatabase(dbindex int)

Select a different database

func (*List) Set

func (rl *List) Set(index int64, value string) error

Set element of list at index n to value

func (*List) Size

func (rl *List) Size() (int64, error)

Get the size of the list

func (*List) Trim

func (rl *List) Trim(start, stop int64) error

Trim an existing list so that it will contain only the specified range of elements specified.

type RedisCreator

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

func NewCreator

func NewCreator(pool *ConnectionPool, dbindex int) *RedisCreator

func (*RedisCreator) NewHashMap

func (c *RedisCreator) NewHashMap(id string) (pinterface.IHashMap, error)

func (*RedisCreator) NewKeyValue

func (c *RedisCreator) NewKeyValue(id string) (pinterface.IKeyValue, error)

func (*RedisCreator) NewList

func (c *RedisCreator) NewList(id string) (pinterface.IList, error)

func (*RedisCreator) NewSet

func (c *RedisCreator) NewSet(id string) (pinterface.ISet, error)

func (*RedisCreator) SelectDatabase

func (c *RedisCreator) SelectDatabase(dbindex int)

type Set

type Set redisDatastructure

func NewSet

func NewSet(pool *ConnectionPool, id string) *Set

Create a new set

func (*Set) Add

func (rs *Set) Add(value string) error

Add an element to the set

func (*Set) All

func (rs *Set) All() ([]string, error)

Get all elements of the set

func (*Set) Clear

func (rs *Set) Clear() error

Clear the contents

func (*Set) Del

func (rs *Set) Del(value string) error

Remove an element from the set

func (*Set) GetAll

func (rs *Set) GetAll() ([]string, error)

Deprecated

func (*Set) Has

func (rs *Set) Has(value string) (bool, error)

Check if a given value is in the set

func (*Set) Pop

func (rs *Set) Pop() (string, error)

Remove a random member from the set

func (*Set) Random

func (rs *Set) Random() (string, error)

Get a random member of the set

func (*Set) Remove

func (rs *Set) Remove() error

Remove this set

func (*Set) SelectDatabase

func (rs *Set) SelectDatabase(dbindex int)

Select a different database

func (*Set) Size

func (rs *Set) Size() (int64, error)

Returns the set cardinality (number of elements) of the set

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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