ro

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2018 License: MIT Imports: 7 Imported by: 0

README

ro: Redis Objects to Go

Build Status codecov GoDoc Go Report Card Go project version license

Example

type Post struct {
	ID        uint64 `redis:"id"`
	UserID    int `redis:"user_id"`
	Title     string `redis:"title"`
	Body      string `redis:"body"`
	CreatedAt uint64 `redis:"created_at"`
}

func (p *Post) GetKeySuffix() string {
	return fmt.Sprint(p.ID)
}

func (p *Post) GetScoreMap() map[string]interface{} {
	return map[string]interface{}{
		"created_at":                     p.CreatedAt,
		fmt.Sprintf("user:%d", p.UserID): p.CreatedAt,
	}
}

var pool *redis.Pool

func main() {
	pool = &redis.Pool{
		Dial: func() (redis.Conn, error) {
			return redis.DialURL("redis://localhost:6379")
		},
	}

	store := ro.New(pool.Get, &Post{})
	now := time.Now()
	ctx := context.Background()

	// Posts will be stored as Hash, and user:{{userID}} and created_at are stored as OrderedSet
	store.Put(ctx, []*Post{
		{
			ID:        1,
			UserID:    1,
			Title:     "post 1",
			Body:      "This is a post 1",
			CreatedAt: now.UnixNano(),
		},
		{
			ID:        2,
			UserID:    2,
			Title:     "post 2",
			Body:      "This is a post 2",
			CreatedAt: now.Add(-24 * 60 * 60 * time.Second).UnixNano(),
		},
		{
			ID:        3,
			UserID:    1,
			Title:     "post 3",
			Body:      "This is a post 3",
			CreatedAt: now.Add(24 * 60 * 60 * time.Second).UnixNano(),
		},
	})

	post := &Post{ID: 1}
	_ := store.Get(ctx, post)
	fmt.Println("%v", post)
	// Output:
	// Post{ID: 1, Title: "post 1", Body: "This is a post 1"}

	posts := []*Post{}
	_ := store.List(ctx, &posts, rq.Key("created_at"), rq.GtEq(now.UnixNano()), rq.Reverse())
	fmt.Println("%v", posts[0])
	// Output:
	// Post{ID: 3, UserID: 1, Title: "post 3", Body: "This is a post 3"}
	fmt.Println("%v", posts[1])
	// Output:
	// Post{ID: 1, UserID: 1, Title: "post 1", Body: "This is a post 1"}

	cnt, _ := store.Count(ctx, rq.Key("user", 1), rq.Gt(now.UnixNano()), rq.Reverse())
	fmt.Println(cnt)
	// Output:
	// 1
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config added in v0.4.0

type Config struct {
	KeyPrefix             string
	ScoreSetKeysKeySuffix string
	KeyDelimiter          string
	ScoreKeyDelimiter     string
	HashStoreEnabled      bool
}

Config contains configurations of a store

type Model

type Model interface {
	GetKeySuffix() string
	GetScoreMap() map[string]interface{}
}

Model is an interface for redis objects

type Option added in v0.4.0

type Option func(c *Config)

Option configures a store

func WithHashStore added in v0.3.0

func WithHashStore(enabled bool) Option

WithHashStore returns a StoreOption that enables or disables to store models into redis hash (default: true).

func WithKeyDelimiter added in v0.2.0

func WithKeyDelimiter(d string) Option

WithKeyDelimiter returns a StoreOption that specifies a key delimiter (default: :).

func WithKeyPrefix

func WithKeyPrefix(prefix string) Option

WithKeyPrefix returns a StoreOption that specifies key prefix If you does not set this option or set an empty string, it will use a model type name as key prefix.

func WithScoreKeyDelimiter added in v0.2.0

func WithScoreKeyDelimiter(d string) Option

WithScoreKeyDelimiter returns a StoreOption that specifies a score key delimiter (default: /).

func WithScoreSetKeysKeySuffix added in v0.2.0

func WithScoreSetKeysKeySuffix(suffix string) Option

WithScoreSetKeysKeySuffix returns a StoreOption that specifies a score set suffix key prefix (default: scoreSetKeys).

type Pool added in v0.4.0

type Pool interface {
	GetContext(context.Context) (redis.Conn, error)
}

Pool is a pool of redis connections.

type Store

type Store interface {
	List(ctx context.Context, dest interface{}, mods ...rq.Modifier) error
	Get(ctx context.Context, dests ...Model) error
	Put(ctx context.Context, src interface{}) error
	Delete(ctx context.Context, src interface{}) error
	DeleteAll(ctx context.Context, mods ...rq.Modifier) error
	Count(ctx context.Context, mods ...rq.Modifier) (int, error)
}

Store is an interface for providing CRUD operations for objects

func New

func New(pool Pool, model Model, opts ...Option) Store

New creates a redisStore instance

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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