Code Monkey home page Code Monkey logo

redisclient's Introduction

实现方式

实现目的

  • 在底层redis client之上增加一层接口封装,该接口供上层调用,有利于代码的扩展性
  • 在底层redis client发生替换或变化时,无需对Client接口协议协议进行变更,只需改变接口方法内的具体实现,增强了代码的可维护性

快速上手

package main

import (
	"context"
	"fmt"

	"github.com/fireyun/redisclient"
	"github.com/go-redis/redis/v7"
)

func MyClient() {
	client := redisclient.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "",
		DB:       0,
	})

	DBOps(client)
}

func MySentinelClient() {
	sentinelClient := redisclient.NewFailoverClient(&redis.FailoverOptions{
		MasterName:       "mymaster",
		SentinelAddrs:    []string{"localhost:26379", "localhost:26380", "localhost:26381"},
		SentinelPassword: "",
		Password:         "",
	})
	DBOps(sentinelClient)
}

func DBOps(cli redisclient.Client) {
	ctx := context.Background()
	key := "mykey"

	pipe := cli.Pipeline()
	pipe.Set("aaa", 99, 0)
	pipe.Get("aaa")
	vals, err := pipe.Exec()
	checkErr(err)
	fmt.Println("Pipeline", vals)

	err = cli.Set(ctx, key, "Hello,man!", 0).Err()
	checkErr(err)

	intVal, err := cli.Exists(ctx, key).Result()
	checkErr(err)
	fmt.Println("Exists", intVal)

	strVal, err := cli.Get(ctx, key).Result()
	checkErr(err)
	fmt.Println("Get", key, strVal)
}

func checkErr(err error) {
	if err != nil {
		if err == redis.Nil {
			return
		}
		panic(err)
	}
}

样例

更多例子见 example.go

redisclient's People

Contributors

fireyun avatar

Stargazers

 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.