go-store: github.com/gosuri/go-store/redis Index | Examples | Files

package redis

import "github.com/gosuri/go-store/redis"

Package redis is the implementation for store and provides simple data persitance and retrieval functionality for redis.

Code:

package main

import (
    "fmt"

    "github.com/gosuri/go-store/redis"
)

type Hacker struct {
    ID        string
    Name      string
    Birthyear int
}

func (h *Hacker) Key() string {
    return h.ID
}

func (h *Hacker) SetKey(k string) {
    h.ID = k
}

func main() {
    store, err := redis.NewStore("", "")
    if err != nil {
        panic(err) // handle error
    }

    // Save a hacker in the store with a auto-generated uuid
    if err := store.Write(&Hacker{Name: "Alan Turing", Birthyear: 1912}); err != nil {
        panic(err) // handle error
    }

    var hackers []Hacker
    // Populate hackers slice with ids of all hackers in the store
    store.List(&hackers)

    alan := hackers[0]
    store.Read(&alan)
    fmt.Println("Hello,", alan.Name)

    fmt.Println("Listing all", len(hackers), "hackers")
    // Fetches all hackers with names from the store
    store.ReadMultiple(hackers)
    for _, h := range hackers {
        fmt.Printf("%s (%d) (%s)\n", h.Name, h.Birthyear, h.ID)
    }
}

Index

Examples

Package Files

doc.go redis.go

Constants

const MaxItems = 1024

MaxItems specifies the max number of items to fetch form redis on each call

Variables

var DefaultRedisURL = "redis://@127.0.0.1:6379"

DefaultRedisURL connection url to connect to redis

var DefaultRedisURLEnv = "REDIS_URL"

DefaultRedisURLEnv specifies the name of environment variable that contains the Redis connection url. It expects the format redis://:password@hostname:port/db_number

func NewPool Uses

func NewPool(config *Config) *driver.Pool

NewPool returns a default redis pool with default configuration.

func NewStore Uses

func NewStore(connURL, namespace string) (store.Store, error)

NewStore returns an instance of Store. It parses the connection information from the connUrl provided and expects the format redis://:password@hostname:port/db_number. If connUrl is empty it reads from the environment variable DefaultRedisURLEnv or defaults to redis://127.0.0.0:6837

type Config Uses

type Config struct {
    Host, Port, Pass string
    Db               int
    // Namespace for redis
    Namespace string
}

Config stores the configuration values used for establishing a connection with Redis server.

func NewConfig Uses

func NewConfig(connURL string) (*Config, error)

NewConfig returns a default redis config. It parses the connection information from the connUrl provided and expects the format redis://:password@hostname:port/db_number. If connUrl is empty it reads from the environment variable DefaultRedisURLEnv or defaults to redis://127.0.0.0:6837

type Redis Uses

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

Redis implements represents the Store methods implemention for Redis.

func New Uses

func New(config *Config) (r *Redis, err error)

New returns a new Redis with defaults

func (*Redis) Delete Uses

func (s *Redis) Delete(i store.Item) error

Delete deletes the item from the store. It constructs the key using i.Key(). When the key is empty, it returns a store.ErrEmptyKey error. When the key does not exist, it returns a store.ErrKeyNotFound error.

func (*Redis) DeleteMultiple Uses

func (s *Redis) DeleteMultiple(items []store.Item) (int, error)

DeleteMultiple deletes multiple items i from the store. It returns the count of items successfully deleted. It returns an error if any of the items do not exist or can't be deleted. It will delete the other items, in that case.

func (*Redis) List Uses

func (s *Redis) List(i interface{}) error

List populates the slice with ids of the slice element type.

func (*Redis) Pool Uses

func (s *Redis) Pool() *driver.Pool

Pool returns a redis pool in use with the store. It returns a new pool otherwise

func (*Redis) Read Uses

func (s *Redis) Read(i store.Item) error

Read reads the item from redis store and copies the values to item It Returns store.ErrKeyNotFound when no values are found for the key provided and store.ErrKeyMissing when key is not provided. Unmarshalling id done using driver provided redis.ScanStruct

func (*Redis) ReadMultiple Uses

func (s *Redis) ReadMultiple(i interface{}) error

ReadMultiple gets the values from redis in a single call by pipelining

func (*Redis) Write Uses

func (s *Redis) Write(i store.Item) error

Write writes the item to the store. It constructs the key using the i.Key() and prefixes it with the type of struct. When the key is empty, it assigns a unique universal id(UUID) using the SetKey method of the Item

func (*Redis) WriteMultiple Uses

func (s *Redis) WriteMultiple(i []store.Item) error

WriteMultiple writes multiple items i to the store.

Package redis imports 11 packages (graph) and is imported by 1 packages. Updated 2017-02-24. Refresh now. Tools for package owners.