redis

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2015 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

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

Example
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)
	}
}
Output:

Index

Examples

Constants

View Source
const (
	// MaxItems specifies the max number of items to fetch form redis on each call
	MaxItems = 1024
)

Variables

View Source
var (
	// DefaultRedisURLEnv specifies the name of environment variable
	// that contains the Redis connection url. It expects the format
	// redis://:password@hostname:port/db_number
	DefaultRedisURLEnv = "REDIS_URL"

	// Default connection url to connect to redis
	DefaultRedisUrl = "redis://@127.0.0.1:6379"
)

Functions

func NewPool

func NewPool(config *Config) *driver.Pool

NewPool returns a default redis pool with default configuration.

func NewStore

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

Types

type Config

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

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 added in v0.0.3

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

redis implements represents the Store methods implemention for Redis.

func New added in v0.0.3

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

func (*Redis) Delete added in v0.0.3

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 added in v0.0.3

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 added in v0.0.3

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

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

func (*Redis) Pool added in v0.0.3

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

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

func (*Redis) Read added in v0.0.3

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 added in v0.0.3

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

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

func (*Redis) Write added in v0.0.3

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 added in v0.0.3

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

WriteMultiple writes multiple items i to the store.

Jump to

Keyboard shortcuts

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