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 ¶
- Constants
- Variables
- func NewPool(config *Config) *driver.Pool
- func NewStore(connUrl, namespace string) (store.Store, error)
- type Config
- type Redis
- func (s *Redis) Delete(i store.Item) error
- func (s *Redis) DeleteMultiple(items []store.Item) (int, error)
- func (s *Redis) List(i interface{}) error
- func (r *Redis) Pool() *driver.Pool
- func (s *Redis) Read(i store.Item) error
- func (s *Redis) ReadMultiple(i interface{}) error
- func (s *Redis) Write(i store.Item) error
- func (s *Redis) WriteMultiple(i []store.Item) error
Examples ¶
Constants ¶
const (
// MaxItems specifies the max number of items to fetch form redis on each call
MaxItems = 1024
)
Variables ¶
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 ¶
Types ¶
type Config ¶
Config stores the configuration values used for establishing a connection with Redis server.
func NewConfig ¶
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 (*Redis) Delete ¶ added in v0.0.3
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
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) Pool ¶ added in v0.0.3
Pool returns a redis pool in use with the store. It returns a new pool otherwise
func (*Redis) Read ¶ added in v0.0.3
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
ReadMultiple gets the values from redis in a single call by pipelining