Code Monkey home page Code Monkey logo

go-redis-template's Introduction

Redis Template

Project status Go Report Card Coverage Status Open Source Helpers RedisDoc RedisDriver GoDoc License

The go-redis-template project came to facilitate the use of the redis driver in your go project, With simplicity in configuration and calls, without needing to implement repetitive conversion interfaces and worrying about pointers, this powerful lib will help you in your day-to-day life. See below some implemented features:

NOTE: first MVP version, if you want to help add features, create a PR and help the community.

  • Simplicity in function calls without the need for repetitive conversions and concerns with pointers.
  • Use any type of key without worrying about conversions to string.
  • Automatic conversions of any type of value.
  • Clearer error handling.
  • Don't worry about unnecessary errors when using the Get operation.

Installation

Use go get.

go get github.com/GabrielHCataldo/go-redis-template

Then import the go-mongo package into your own code.

import "github.com/GabrielHCataldo/go-redis-template/redis"

Usability and documentation

Below we will show some basic examples:

IMPORTANT: Always check the documentation in the structures and functions fields. For more details on the examples, visit All examples link.

Set

To save a value is very simple, you can pass the key and value of any type in the parameters and customize it with the opts parameter, see:

package main

import (
    "context"
    "github.com/GabrielHCataldo/go-helper/helper"
    "github.com/GabrielHCataldo/go-logger/logger"
    "github.com/GabrielHCataldo/go-redis-template/redis"
    "github.com/GabrielHCataldo/go-redis-template/redis/option"
    "os"
    "time"
)

type testStruct struct {
    Name      string
    BirthDate time.Time
}

func main() {
    key := "example-struct"
    value := testStruct{
        Name:      "Foo bar",
        BirthDate: time.Now(),
    }
    redisTemplate := redis.NewTemplate(option.Client{
        Addr:     os.Getenv("REDIS_URL"),
        Password: os.Getenv("REDIS_PASSWORD"),
        DB:       0,
    })
    defer redisTemplate.SimpleDisconnect()
    ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
    defer cancel()
    opt := option.NewSet()
    opt.SetMode(option.SetModeDefault)
    opt.SetTTL(5*time.Minute)
    opt.SetExpireAt(time.Time{})
    opt.SetKeepTTL(false)
    err := redisTemplate.Set(ctx, key, value, opt)
    if helper.IsNotNil(err) {
        logger.Error("error set redis value:", err)
        return
    }
    logger.Info("set", key, "value redis completed successfully!")
}

Output:

[INFO 2024/01/07 11:51:38] main.go:41: set example-struct value redis completed successfully!

See all Set examples by accessing the link.

Get

You can get the key value without worrying about string conversions, just pass the dest parameter, see:

package main

import (
    "context"
    "github.com/GabrielHCataldo/go-helper/helper"
    "github.com/GabrielHCataldo/go-logger/logger"
    "github.com/GabrielHCataldo/go-redis-template/redis"
    "github.com/GabrielHCataldo/go-redis-template/redis/option"
    "os"
    "time"
)

type testStruct struct {
    Name      string
    BirthDate time.Time
}

func main() {
    key := "example-struct"
    var dest testStruct
    redisTemplate := redis.NewTemplate(option.Client{
        Addr:     os.Getenv("REDIS_URL"),
        Password: os.Getenv("REDIS_PASSWORD"),
        DB:       0,
    })
    defer redisTemplate.SimpleDisconnect()
    ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
    defer cancel()
    err := redisTemplate.Get(ctx, key, &dest,)
    if helper.IsNotNil(err) {
        logger.Error("error get redis value:", err)
        return
    }
    logger.Info("get", key, "value redis completed successfully!", dest)
}

Output:

[INFO 2024/01/07 11:59:46] main.go:33: get example-struct value redis completed successfully! {"Name":"Foo bar","BirthDate":"2024-01-07T11:51:38-03:00"}

See all Get examples by accessing the link.

Exists

To check if a key already exists in redis, just do:

package main

import (
    "context"
    "github.com/GabrielHCataldo/go-helper/helper"
    "github.com/GabrielHCataldo/go-logger/logger"
    "github.com/GabrielHCataldo/go-redis-template/redis"
    "github.com/GabrielHCataldo/go-redis-template/redis/option"
    "os"
    "time"
)

type testStruct struct {
    Name      string
    BirthDate time.Time
}

func main() {
    key := "example-struct"
    redisTemplate := redis.NewTemplate(option.Client{
        Addr:     os.Getenv("REDIS_URL"),
        Password: os.Getenv("REDIS_PASSWORD"),
        DB:       0,
    })
    defer redisTemplate.SimpleDisconnect()
    ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
    defer cancel()
    alreadyExists, err := redisTemplate.Exists(ctx, key)
    if helper.IsNotNil(err) {
        logger.Error("error exists redis value:", err)
        return
    }
    logger.Info("already exists", key, "?", alreadyExists)
}

Output:

[INFO 2024/01/07 12:02:01] main.go:32: already exists example-struct ? true

Delete

package main

import (
    "context"
    "github.com/GabrielHCataldo/go-helper/helper"
    "github.com/GabrielHCataldo/go-logger/logger"
    "github.com/GabrielHCataldo/go-redis-template/redis"
    "github.com/GabrielHCataldo/go-redis-template/redis/option"
    "os"
    "time"
)

func main() {
    key1 := "example-struct"
    key2 := "example-struct-2"
    redisTemplate := redis.NewTemplate(option.Client{
        Addr:     os.Getenv("REDIS_URL"),
        Password: os.Getenv("REDIS_PASSWORD"),
        DB:       0,
    })
    defer redisTemplate.SimpleDisconnect()
    ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
    defer cancel()
    err := redisTemplate.Del(ctx, key1, key2)
    if helper.IsNotNil(err) {
        logger.Error("error delete redis keys:", err)
        return
    }
    logger.Info("delete redis keys (", key1, "-", key2, ") completed successfully!")
}

Output:

[INFO 2024/01/07 12:07:54] main.go:28: delete redis keys ( example-struct - example-struct-2 ) completed successfully!

How to contribute

Make a pull request, or if you find a bug, open it an Issues.

License

Distributed under MIT license, see the license file within the code for more details.

go-redis-template's People

Contributors

gabrielhcataldo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.