Dragonfly

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2019 License: Apache-2.0 Imports: 5 Imported by: 0

README

Dragonfly

CircleCI report

Codes for accessing Redis DB

Install

go get github.com/NBCFB/Dragonfly

How-To-Use

Configuration

Put the configuration for Redis connection into config.json, the resulted config.json should look like:

{
  "mode": "local_test",
  ...
  "local_test": {
    "DB": {
      ...
    },
    
    "redisDB": {
      "host":             "localhost",
      "pass":             "",
      "maxIdle":          5,
      "maxActive":        12000,
      "maxConnLifetime":  3,
      "idleTimeout":      200
    }
    ...
  }
}

You can add multiple configuration blocks for different mode such as 'prod', 'dev'.

Creat a client (a connection)
...
cfg, err := LoadServerConfig()
...
caller = NewCaller(cfg)
...

Dont worry about closing the client. The conn pool will automatically manage the connection. But you can always manually close one:

caller.client.Close()
Set

Add/update a record in redis DB.

v, err := caller.Set("key:1", "val:1", 0)
if err != nil {
    fmt.Printf("error:%v", err)
}
Set In Batch

Add/update multiple records (using one connection) in the redis DB. In this case, you need create a slice of RedisObj first.

objs := make([]RedisObj, 3)
objs[0] = RedisObj{K: "key:1", V: "val:1"}
objs[1] = RedisObj{K: "key:2", V: "val:2"}
objs[2] = RedisObj{K: "key:3", V: "val:3"}
...
err := caller.SetInBatch(objs)
if err != nil {
    fmt.Printf("error:%v", err)
}
Get

Get a record from the redis DB.

v, err := caller.Get("key:1")
if err != nil {
    fmt.Printf("error:%v", err)
}

Search record(s) using patten string.

You can search with without keywords. In this case, records that match the search pattern return.

objs, err := caller.Search("key*", nil)
if err != nil {
    fmt.Printf("error:%v", err)
}

You can also search with keywords. In this case, records that match the search pattern and (the values) match one of the keywords return.

objs, err := caller.Search("key:*", []string{"val:2"})
if err != nil {
    fmt.Printf("error:%v", err)
}

The returned result is a slice of RedisObj which has the structure:

type RedisObj struct {
    K string
    V string
}
Delete

You can delete records by keys.

err = caller.Del("key:1", "key:2")
if err != nil {
    fmt.Printf("error:%v", err)
}

Write you test

We use Behavioral Driven Test Framework Ginkgo to write our test. You have to install Ginkgo and its preferred matcher libs.

go get github.com/onsi/ginkgo
go get github.com/onsi/gomega/...

Make sure you also install the ginkgo CLI so that you can generate your own test suite.

go get github.com/onsi/ginkgo/ginkgo

Every time we write a behavioral driven test, we need create a new client. Thus, we make it happen in BeforeEach and AfterEach:

var _ = Describe("Dragonfly", func() {
    var caller *RedisCallers

    BeforeEach(func() {
	caller = NewCaller(nil)
	Expect(caller.Client.FlushDB().Err()).NotTo(HaveOccurred())
    })

    AfterEach(func() {
	Expect(caller.Client.Close()).NotTo(HaveOccurred())
    })
    ...
}

Then you can write your test case like this:

var _ = Describe("Dragonfly", func() {
    var caller *RedisCallers

    BeforeEach(func() {
	caller = NewCaller(nil)
	Expect(caller.Client.FlushDB().Err()).NotTo(HaveOccurred())
    })

    AfterEach(func() {
	Expect(caller.Client.Close()).NotTo(HaveOccurred())
    })
    ...
    
    It("can set in batch", func() {
    	objs := make([]RedisObj, 3)
    	objs[0] = RedisObj{K: "key:1", V: "val:1"}
    	objs[1] = RedisObj{K: "key:2", V: "val:2"}
    	objs[2] = RedisObj{K: "key:3", V: "val:3"}

    	err := caller.SetInBatch(objs)
	Expect(err).NotTo(HaveOccurred())

	expected, _ := caller.Search("key*", nil)
	Expect(len(expected)).To(Equal(3))

    })
...
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RedisCallers

type RedisCallers struct {
	Client *redis.Client
}

RedisCallers represent a client that connects to our redis DB.

func NewCaller

func NewCaller(config *viper.Viper) *RedisCallers

func NewCallerSentinel

func NewCallerSentinel(config *viper.Viper) *RedisCallers

NewCaller initialises a new connection client to the redis DB.

func (*RedisCallers) Del

func (c *RedisCallers) Del(keys ...string) error

Del deletes entries by their keys

func (*RedisCallers) Get

func (c *RedisCallers) Get(k string) (v string, err error)

Get gets an entry by its key.

func (*RedisCallers) SearchByKeys

func (c *RedisCallers) SearchByKeys(patten string, keywords []string) (objs []RedisObj, err error)

* Using scan when keySpace is small

func (*RedisCallers) SearchByScan

func (c *RedisCallers) SearchByScan(patten string, keywords []string, count int64) (objs []RedisObj, err error)

Search returns any entries that fulfills the given search patten and keywords. Pattern is used to check the key while keywords are used to check the values of the matched keys. For example, assume we have {'user:1', 'aaa'}, {'user:2', 'abc'}, {'user:3', 'bbb'} three entries, if the given pattern is 'user:*' and the keywords is ['a'], the returned entries are: {'user:1', 'aaa'}, {'user:2', 'abc'}. * Using scan when keySpace is big

func (*RedisCallers) Set

func (c *RedisCallers) Set(k, v string, exp time.Duration) (newVal string, err error)

Set creates a new entry or updates an existed entry with a specified lifetime, the default lifetime is zero.

func (*RedisCallers) SetInBatch

func (c *RedisCallers) SetInBatch(objs []RedisObj) (err error)

SetInBatch creates multiple new entries or updates multiple existed entries given by a slice of RedisObj

type RedisError

type RedisError struct {
	Action string
	Key    string
	Val    string
	ErrMsg string
}

A RedisError represents a structured redis error.

func (*RedisError) Error

func (e *RedisError) Error() string

Error returns a formatted redis error message.

type RedisObj

type RedisObj struct {
	K string
	V string
}

A RedisObj represents an object with string typed key and value.

Jump to

Keyboard shortcuts

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