redistructs

package module
v0.0.0-...-4b30a6a Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2022 License: MIT Imports: 8 Imported by: 0

README

redistructs

Boilerplate codes to store struct as hashset or serialized object into Redis

Features

  • RediStructs has an interface compatible with fatih/structs which is optimizely queried.

  • RediStore is an object for operations to Model and Redistructs inspired by izumin5210/ro

  • Set EXPIRE/EXPIREAT

  • Set a prefix string

  • Atomic read and write

  • Store recursive redistructs if the struct is nested or serialized object (by gob)

Examples

Store a struct
redisPool := &redis.Pool{
    Dial: func() (redis.Conn, error) {
        return redis.DialURL(fmt.Sprintf("redis://localhost:%s", p.dockerRes.GetPort("6379/tcp")))
    }
}
postStore := New(redisPool, types.CreateConfig(), &Post{})

err := postStore.Put(context.TODO(), []*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(),
    },
    {
        ID:        4,
        UserID:    1,
        Title:     "post 4",
        Body:      "This is a post 4",
        CreatedAt: now.Add(-24 * 60 * 60 * time.Second).UnixNano(),
    },
})
Get a struct
postStore := New(redisPool, types.CreateConfig(), &Post{})

p := &Post{ID: 4}
err := postStore.Get(context.TODO(), p)
if err != nil {
    panic(err)
}

if p.UserID != 1 {
    panic(fmt.Errorf("expect: %v, got: %v", 1, p.UserID))
}

if p.Title != "post 4" {
    panic(fmt.Errorf("expect: %v, got: %v", "post 4", p.Title))
}
Delete a struct
postStore := New(redisPool, types.CreateConfig(), &Post{})

p := &Post{ID: 4}
err := postStore.Delete(context.TODO(), p)
if err != nil {
    panic(err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

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

Pool is a pool of redis connections.

type RediStructs

type RediStructs interface {
	Get(ctx context.Context, dests ...types.RediStruct) error
	Put(ctx context.Context, src interface{}) error
	Delete(ctx context.Context, src interface{}) error

	Map(ctx context.Context) (map[string]types.RediStruct, error)
	Values(ctx context.Context) ([]types.RediStruct, error)
	Names(ctx context.Context) ([]string, error)
	// contains filtered or unexported methods
}

RediStructs is an interface for operations for objects

func New

func New(pool Pool, config types.Config, model types.RediStruct) RediStructs

New creates a RediStructs instance

func NewRedigoStructs

func NewRedigoStructs(pool Pool, config types.Config, model types.RediStruct) RediStructs

type RedigoStructs

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

func (*RedigoStructs) Delete

func (rs *RedigoStructs) Delete(ctx context.Context, src interface{}) error

func (*RedigoStructs) Get

func (rs *RedigoStructs) Get(ctx context.Context, dests ...types.RediStruct) error

func (*RedigoStructs) Map

func (*RedigoStructs) Names

func (rs *RedigoStructs) Names(ctx context.Context) ([]string, error)

func (*RedigoStructs) Put

func (rs *RedigoStructs) Put(ctx context.Context, src interface{}) error

func (*RedigoStructs) Values

func (rs *RedigoStructs) Values(ctx context.Context) ([]types.RediStruct, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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