optredis

package module
v0.1.2-0...-273168d Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2020 License: MIT, Apache-2.0 Imports: 8 Imported by: 0

README

TravisBuildStatus GoDoc GoReportCard codecov

for what

  • redis client loader and support bloom filter

depends

in go mod project

# warning use privte git host must set
# global set for once
# add private git host like github.com to evn GOPRIVATE
$ go env -w GOPRIVATE='github.com'
# use ssh proxy
# set ssh-key to use ssh as http
$ git config --global url."git@github.com:".insteadOf "http://github.com/"
# or use PRIVATE-TOKEN
# set PRIVATE-TOKEN as gitlab or gitea
$ git config --global http.extraheader "PRIVATE-TOKEN: {PRIVATE-TOKEN}"
# set this rep to download ssh as https use PRIVATE-TOKEN
$ git config --global url."ssh://github.com/".insteadOf "https://github.com/"

# before above global settings
# test version info
$ git ls-remote -q http://github.com/sinlovgo/optredis.git

# test depends see full version
$ go list -v -m -versions github.com/sinlovgo/optredis
# or use last version add go.mod by script
$ echo "go mod edit -require=$(go list -m -versions github.com/sinlovgo/optredis | awk '{print $1 "@" $NF}')"
$ echo "go mod vendor"

evn

  • golang sdk 1.13+
  • github.com/go-redis/redis v6.15.7
  • github.com/spf13/viper v1.6.3
  • github.com/willf/bitset v1.1.10
  • github.com/willf/bloom v2.0.3

use

  • viper config struct as
package cfg

import "github.com/sinlovgo/optredis/optredisconfig"

var global *Conf

func Global() Conf {
	return *global
}

type Conf struct {
	RunMode        string               `json:"run_mode" mapstructure:"run_mode"`
	Name           string               `json:"name" mapstructure:"name"`
	RedisOptConfig []optredisconfig.Cfg `json:"redis_clients" mapstructure:"redis_clients"`
}

then viper config file look like

package cfg

import "github.com/spf13/viper"

func InitConfigByViper(configPath string) error {
	viper.SetConfigFile(configPath)
	viper.SetConfigType("yaml")
	if err := viper.ReadInConfig(); err != nil {
		return err
	}
	err := viper.Unmarshal(&global)
	if err != nil {
		return err
	}
	return nil
}
  • config file conf.yml
run_mode: debug                # run mode: debug, test, release
name: test-server              # name

redis_clients:
  - name: default
    addr: localhost:35021
    password:
    db: 0
    max_retries: 0 # Default is to not retry failed commands
    dial_timeout: 5 # Default is 5 seconds.
    read_timeout: 3 # Default is 3 seconds.
    write_timeout: 3 # Default is ReadTimeout
  • optredis client and tools of default.go
package cacheDefault
import (
	"github.com/sinlovgo/optredis"
)
const (
	name string = "default"
)

var defaultOptRedis *optredis.OptRedis

func Init() error {
	if defaultOptRedis == nil {
		config := optredis.NewConfig(
			optredis.WithName("default"),
		)
		// or use bloom filter
		config := optredis.NewConfig(
			optredis.WithName("default"),
			optredis.WithUseBloomFilter(true),
			optredis.WithUseBloomK(20),
			optredis.WithUseBloomN(1000),
			optredis.WithUseBloomM(5),
		)
		optRedis, err := optredis.NewOptRedis(*config).InitByName().Ping()
		if err != nil {
			return err
		}
		defaultOptRedis = &optRedis
	}
	return nil
}

func Opt() *optredis.OptRedis {
	return defaultOptRedis
}
  • init at main.go
package main

import (
	"cacheDefault"
	"cfg"
	"fmt"
	"github.com/sinlovgo/optredis"
)
func main()  {
	err :=cfg.InitConfigByViper("conf.yml")
	if err!= nil {
		fmt.Printf("init InitConfigByViper err: %v", err)
	}
	err = InitRedisOpt()
	if err!= nil {
		fmt.Printf("init optredis err: %v", err)
	}
}

func InitRedisOpt() error {
	err := optredis.InitByConfigList(cfg.Global().RedisOptConfig)
	if err != nil {
		return err
	}
	return cacheDefault.Init()
}
  • then can use topic of cache student, student struct at package at demo
package cacheTopicDemo
import (
	"cacheDefault"
	"demo"
	"time"
)

const (
	cpStudentPrefix string = "cache-student-"
	// one week
	cpStudentExpiration = time.Duration(24) * time.Hour
)

func ExistsStudent(key string) (bool, error) {
	return cacheDefault.Opt().Exists(key, cpStudentPrefix)
}

func Set(key string, data *demo.Student) error {
	return cacheDefault.Opt().SetJson(key, cpStudentPrefix, data, cpStudentExpiration)
}

func Get(key string, data *demo.Student) error {
	return cacheDefault.Opt().GetJson(key, cpStudentPrefix, data)
}

dev

make init
  • test code
make test

add main.go file and run

make run

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExistsKey

func ExistsKey(redisCli *redis.Client, key string) (bool, error)

func InitByConfigList

func InitByConfigList(redisClientList []optredisconfig.Cfg) error

init by viper config list as

	redis_clients:
 - name: default
   addr: localhost:6379
   password:
   db: 0
   max_retries: 0 # Default is to not retry failed commands
   dial_timeout: 5 # Default is 5 seconds.
   read_timeout: 3 # Default is 3 seconds.
   write_timeout: 3 # Default is ReadTimeout

func RedisScanKeysMatch

func RedisScanKeysMatch(redisCli *redis.Client, match string, maxCount int64) ([]string, error)

scan keys instead redisCli.Keys()

redisCli *redis.Client
match string
maxCount int64

return

error scan error
[]string removed repeated key

Types

type Config

type Config struct {
	Name           string
	UseBloomFilter bool
	BloomK         uint
	BloomN         uint
	BloomM         uint
}

func NewConfig

func NewConfig(opts ...ConfigOption) (opt *Config)

func NewConfigFull

func NewConfigFull() *Config

type ConfigOption

type ConfigOption func(*Config)

func WithName

func WithName(name string) ConfigOption

func WithUseBloomFilter

func WithUseBloomFilter(useBloomFilter bool) ConfigOption

func WithUseBloomK

func WithUseBloomK(bloomK uint) ConfigOption

func WithUseBloomM

func WithUseBloomM(bloomM uint) ConfigOption

func WithUseBloomN

func WithUseBloomN(bloomN uint) ConfigOption

type OptRedis

type OptRedis struct {
	Name          string
	UseBoomFilter bool
	BloomK        uint
	BloomN        uint
	BloomM        uint

	RedisClient *redis.Client
	BloomFilter *bloom.BloomFilter
	// contains filtered or unexported fields
}

func (OptRedis) Client

func (o OptRedis) Client() *redis.Client

func (OptRedis) Del

func (o OptRedis) Del(key string, prefix string) (int64, error)

func (OptRedis) Exists

func (o OptRedis) Exists(key string, prefix string) (bool, error)

func (OptRedis) Expire

func (o OptRedis) Expire(key string, prefix string, exp time.Duration) (bool, error)

func (OptRedis) GetJson

func (o OptRedis) GetJson(key string, prefix string, v interface{}) error

func (OptRedis) InitByName

func (o OptRedis) InitByName() OptRedis

func (OptRedis) Persist

func (o OptRedis) Persist(key string, prefix string) (bool, error)

func (OptRedis) Ping

func (o OptRedis) Ping() (OptRedis, error)

func (OptRedis) SetJson

func (o OptRedis) SetJson(key string, prefix string, data interface{}, expiration time.Duration) error

func (OptRedis) TTL

func (o OptRedis) TTL(key string, prefix string) (time.Duration, error)

type RedisFunc

type RedisFunc interface {
	InitByName() OptRedis
	Ping() (OptRedis, error)
	Client() *redis.Client
	Exists(key string, prefix string) (bool, error)
	Del(key string, prefix string) (int64, error)
	TTL(key string, prefix string) (time.Duration, error)
	Expire(key string, prefix string, exp time.Duration) (bool, error)
	Persist(key string, prefix string) (bool, error)
	SetJson(key string, prefix string, data interface{}, expiration time.Duration) error
	GetJson(key string, prefix string, v interface{}) error
}

func NewOptRedis

func NewOptRedis(cfg Config) RedisFunc

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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