redis

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2021 License: AGPL-3.0 Imports: 7 Imported by: 0

README

Helper functions

To create a Redis connection :
CreateRedisClient(temp *RedisClient)

To get Redis client connection instance :
GetRedisClient(temp *RedisClientId)


Supported redis operations :
1. StoreKV
2. GetKV
3. StoreJSON
4. GetJSON 


### Example

`

package main

import (
    "context"
    "fmt"
    "sync"
    "time"

    "github.com/codefamily-org/go-sdk/integrations/database/redis"
    "github.com/codefamily-org/go-sdk/utils/logger"
)

var redis_clients = []*redis.RedisClient{
    {
        Id:       "test-conn-1",
        Hostname: "",
        Password: "",
        Once:     &sync.Once{},
        DB:       0,
    },
}

var (
    ctx          = context.Background()
    test_kv      = "test_kv_1"
    test_kv_json = "test_kv_json_1"
)

type SomeResponse struct {
    Name string
    Roll int
}

// init block
func init() {
    initialize_connections()
}

func initialize_connections() {
    for _, r := range redis_clients {
        redis.CreateRedisClient(r)
    }
}

func main() {
    logger.Log("====================\t 1 \t=========================")
    get_kv()
    logger.Log("====================\t 2 \t=========================")
    store_kv()
    logger.Log("====================\t 3 \t=========================")
    get_kv()
    logger.Log("====================\t 4 \t=========================")
    get_kv_json()
    logger.Log("====================\t 5 \t=========================")
    store_kv_json()
    logger.Log("====================\t 6 \t=========================")
    get_kv_json()
    logger.Log("====================\t 7 \t=========================")

}

func get_kv() {
    serv := &redis.Service{}
    for _, v := range redis_clients {
        k := &redis.GetKV{
            ClientId:       &redis.RedisClientId{Id: v.Id},
            Key:            test_kv,
            MyContext:      &ctx,
            ContextTimeout: time.Second * 5,
        }
        some_value, err := serv.GetKV(k)
        if err == nil {
            logger.Log(some_value)
        } else {
            logger.Log("error : " + err.Error())
        }
    }
}

func store_kv() {
    serv := &redis.Service{}
    for _, v := range redis_clients {
        s := &redis.StoreKV{
            ClientId:       &redis.RedisClientId{Id: v.Id},
            Key:            test_kv,
            Value:          "Welcome to Codefamily.org",
            TTL:            time.Minute * 1,
            MyContext:      &ctx,
            ContextTimeout: time.Second * 5,
        }
        err := serv.StoreKV(s)
        if err != nil {
            logger.Log("error : " + err.Error())
        }
    }
}

func get_kv_json() {
    serv := &redis.Service{}
    for _, v := range redis_clients {
        k := &redis.GetJSON{
            ClientId:       &redis.RedisClientId{Id: v.Id},
            Key:            test_kv_json,
            MyContext:      &ctx,
            ContextTimeout: time.Second * 5,
        }
        var all_items []*SomeResponse
        err := serv.GetJSON(k, &all_items)
        if err == nil {
            for _, zz := range all_items {
                logger.Log(fmt.Sprintf("%+v ", zz))
            }
        } else {
            logger.Log("error : " + err.Error())
        }
    }
}

func store_kv_json() {
    serv := &redis.Service{}
    for _, v := range redis_clients {
        var i = []*SomeResponse{{Name: "Codefamily.org", Roll: 1}, {Name: "Rahul Reddy", Roll: 2}}
        s := &redis.StoreJSON{
            ClientId:       &redis.RedisClientId{Id: v.Id},
            Key:            test_kv_json,
            Value:          i,
            TTL:            time.Minute * 1,
            MyContext:      &ctx,
            ContextTimeout: time.Second * 5,
        }
        if err := serv.StoreJSON(s); err != nil {
            logger.Log("error : " + err.Error())
        }
    }
}

`

Documentation

Index

Constants

View Source
const (
	ERROR_REDIS_INSTANCE = "redis_connection : failed to get the redis client"
)

Variables

This section is empty.

Functions

func CloseRedisClient

func CloseRedisClient(temp *RedisClientId)

ClosePubsubClient used to close the pubsub client instance

func CreateRedisClient

func CreateRedisClient(temp *RedisClient)

CreateRedisClient used to create a new RedisClient connection

func GetRedisClient

func GetRedisClient(temp *RedisClientId) (*redis.Client, error)

GetRedisClient used to get the redis client connection id

Types

type DeleteKey

type DeleteKey struct {
	Id             *RedisClientId
	Key            string
	MyContext      *context.Context
	ContextTimeout time.Duration
}

type GetJSON

type GetJSON struct {
	Id             *RedisClientId
	Key            string
	MyContext      *context.Context
	ContextTimeout time.Duration
}

type GetKV

type GetKV struct {
	Id             *RedisClientId
	Key            string
	MyContext      *context.Context
	ContextTimeout time.Duration
}

type RedisClient

type RedisClient struct {
	Id   *RedisClientId
	Once *sync.Once
	Opts *redis.Options

	Err *error
	// contains filtered or unexported fields
}

RedisClient Id -> Unique Id for Redis Client

example : "test-connection-1"

Hostname -> Redis Host name Password -> Redis Password DB -> Redis DB Once -> Singleton mutex

type RedisClientId

type RedisClientId struct {
	Id string
}

RedisClientId Id -> should have same Id as RedisClient instance Id

type RedisClients

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

RedisClients all_clients -> all redis connection details

type Service

type Service struct{}

func (*Service) DeleteKey

func (s *Service) DeleteKey(temp *DeleteKey) error

This method returns the value if key present in redis else returns error object.

func (*Service) GetJSON

func (s *Service) GetJSON(temp *GetJSON, v interface{}) error

This method returns the value if key present in redis else returns error object.

func (*Service) GetKV

func (s *Service) GetKV(temp *GetKV) (string, error)

This method returns the value if key present in redis else returns error object.

func (*Service) StoreJSON

func (s *Service) StoreJSON(temp *StoreJSON) error

This method only stores composite data structure in redis and returns nil if succesful else returns error object Value -> interface{} fields should be exported (eg: {TestField string})

func (*Service) StoreKV

func (s *Service) StoreKV(temp *StoreKV) error

This method only store data in redis and returns nil if succesful else returns error

type StoreJSON

type StoreJSON struct {
	Id             *RedisClientId
	Key            string
	Value          interface{}
	TTL            time.Duration
	MyContext      *context.Context
	ContextTimeout time.Duration
}

type StoreKV

type StoreKV struct {
	Id             *RedisClientId
	Key            string
	Value          string
	TTL            time.Duration
	MyContext      *context.Context
	ContextTimeout time.Duration
}

Jump to

Keyboard shortcuts

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