README
¶
Simple Redis
Easy way to use Redis from Go.
Online API Documentation
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.
Version, license and author
- Version: 1.0
- License: MIT
- Author: Alexander F Rødseth
Documentation
¶
Overview ¶
Easy way to use Redis from Go.
Index ¶
- Constants
- func SetMaxIdleConnections(maximum int)
- func TestConnection() (err error)
- func TestConnectionHost(hostColonPort string) (err error)
- type ConnectionPool
- type HashMap
- func (rh *HashMap) Del(elementid string) error
- func (rh *HashMap) DelKey(elementid, key string) error
- func (rh *HashMap) Exists(elementid string) (bool, error)
- func (rh *HashMap) Get(elementid, key string) (string, error)
- func (rh *HashMap) GetAll() ([]string, error)
- func (rh *HashMap) Has(elementid, key string) (bool, error)
- func (rh *HashMap) Remove() error
- func (rh *HashMap) SelectDatabase(dbindex int)
- func (rh *HashMap) Set(elementid, key, value string) error
- type KeyValue
- type List
- type Set
Constants ¶
const (
// Version number. Stable API within major version numbers.
Version = 1.0
)
Variables ¶
This section is empty.
Functions ¶
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 TestConnection ¶
func TestConnection() (err error)
Test if the local Redis server is up and running
func TestConnectionHost ¶
Test if a given Redis server at host:port is up and running. Does not try to PING or AUTH.
Types ¶
type ConnectionPool ¶
A pool of readily available Redis connections
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) 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() (pong bool)
Ping the server by sending a PING command
type HashMap ¶
type HashMap redisDatastructure
func (*HashMap) DelKey ¶
Remove a key for an entry in a hashmap (for instance the email field for a user)
func (*HashMap) Get ¶
Get a value from a hashmap given the element id (for instance a user id) and the key (for instance "password")
func (*HashMap) SelectDatabase ¶
Select a different database
type KeyValue ¶
type KeyValue redisDatastructure
func NewKeyValue ¶
func NewKeyValue(pool *ConnectionPool, id string) *KeyValue
Create a new key/value
func (*KeyValue) SelectDatabase ¶
Select a different database
type List ¶
type List redisDatastructure
func (*List) SelectDatabase ¶
Select a different database