dpredis

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2020 License: MIT Imports: 10 Imported by: 0

README

dp-redis

Getting started
  • Add dp-redis to your project using go get github.com/ONSdigital/dp-redis
Dependencies
  • No further dependencies other than those defined in go.mod
Usage

Sample use of GetByID():

s represents a Session in all examples

s, err := cache.GetByID(ID)

if err != nil {
    panic(err)
    return
}

Sample use of SetSession():

s := &session.Session{
        ID:           "1234",
        Email:        "user@email.com",
        Start:        time.Time{},
        LastAccessed: time.Time{}, 
    }

if err := cache.Set(s); err != nil {
    panic(err)
	return
}

Sample use of DeleteAll():

if err := cache.DeleteAll(); err != nil {
    panic(err)
    return
}
Contributing

See CONTRIBUTING for details.

License

Copyright © 2020, Office for National Statistics (https://www.ons.gov.uk)

Released under MIT license, see LICENSE for details.

Documentation

Index

Constants

View Source
const HealthyMessage = "redis is OK"

Variables

View Source
var (
	ErrEmptySessionID    = errors.New("session id required but was empty")
	ErrEmptySessionEmail = errors.New("session email required but was empty")
	ErrEmptySession      = errors.New("session is empty")
	ErrEmptyAddress      = errors.New("address is empty")
	ErrEmptyPassword     = errors.New("password is empty")
	ErrInvalidTTL        = errors.New("ttl should not be zero")
)

Functions

This section is empty.

Types

type Client

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

Client - structure for the redis client

func NewClient

func NewClient(c Config) (*Client, error)

NewClient - returns new redis client with provided config options

func (*Client) Checker

func (c *Client) Checker(ctx context.Context, state *health.CheckState) error

func (*Client) DeleteAll

func (c *Client) DeleteAll() error

DeleteAll - removes all items from redis

func (*Client) Expire

func (c *Client) Expire(key string, expiration time.Duration) error

Expire - sets the expiration of key

func (*Client) GetByEmail added in v1.0.0

func (c *Client) GetByEmail(email string) (*session.Session, error)

GetByEmail - gets a session from redis using its ID

func (*Client) GetByID

func (c *Client) GetByID(id string) (*session.Session, error)

GetByID - gets a session from redis using its ID

func (*Client) Ping

func (c *Client) Ping() error

Ping - checks the connection to redis

func (*Client) SetSession

func (c *Client) SetSession(s *session.Session) error

SetSession - add session to redis

type Config

type Config struct {
	Addr     string
	Password string
	Database int
	TTL      time.Duration
	TLS      *tls.Config
}

Config - config options for the redis client

type RedisClienter

type RedisClienter interface {
	Set(key string, value interface{}, expiration time.Duration) *redis.StatusCmd
	Get(key string) *redis.StringCmd
	Expire(key string, expiration time.Duration) *redis.BoolCmd
	FlushAll() *redis.StatusCmd
	Ping() *redis.StatusCmd
}

RedisClienter - interface for redis

type RedisClienterMock

type RedisClienterMock struct {
	// ExpireFunc mocks the Expire method.
	ExpireFunc func(key string, expiration time.Duration) *redis.BoolCmd

	// FlushAllFunc mocks the FlushAll method.
	FlushAllFunc func() *redis.StatusCmd

	// GetFunc mocks the Get method.
	GetFunc func(key string) *redis.StringCmd

	// PingFunc mocks the Ping method.
	PingFunc func() *redis.StatusCmd

	// SetFunc mocks the Set method.
	SetFunc func(key string, value interface{}, expiration time.Duration) *redis.StatusCmd
	// contains filtered or unexported fields
}

RedisClienterMock is a mock implementation of RedisClienter.

    func TestSomethingThatUsesRedisClienter(t *testing.T) {

        // make and configure a mocked RedisClienter
        mockedRedisClienter := &RedisClienterMock{
            ExpireFunc: func(key string, expiration time.Duration) *redis.BoolCmd {
	               panic("mock out the Expire method")
            },
            FlushAllFunc: func() *redis.StatusCmd {
	               panic("mock out the FlushAll method")
            },
            GetFunc: func(key string) *redis.StringCmd {
	               panic("mock out the Get method")
            },
            PingFunc: func() *redis.StatusCmd {
	               panic("mock out the Ping method")
            },
            SetFunc: func(key string, value interface{}, expiration time.Duration) *redis.StatusCmd {
	               panic("mock out the Set method")
            },
        }

        // use mockedRedisClienter in code that requires RedisClienter
        // and then make assertions.

    }

func (*RedisClienterMock) Expire

func (mock *RedisClienterMock) Expire(key string, expiration time.Duration) *redis.BoolCmd

Expire calls ExpireFunc.

func (*RedisClienterMock) ExpireCalls

func (mock *RedisClienterMock) ExpireCalls() []struct {
	Key        string
	Expiration time.Duration
}

ExpireCalls gets all the calls that were made to Expire. Check the length with:

len(mockedRedisClienter.ExpireCalls())

func (*RedisClienterMock) FlushAll

func (mock *RedisClienterMock) FlushAll() *redis.StatusCmd

FlushAll calls FlushAllFunc.

func (*RedisClienterMock) FlushAllCalls

func (mock *RedisClienterMock) FlushAllCalls() []struct {
}

FlushAllCalls gets all the calls that were made to FlushAll. Check the length with:

len(mockedRedisClienter.FlushAllCalls())

func (*RedisClienterMock) Get

func (mock *RedisClienterMock) Get(key string) *redis.StringCmd

Get calls GetFunc.

func (*RedisClienterMock) GetCalls

func (mock *RedisClienterMock) GetCalls() []struct {
	Key string
}

GetCalls gets all the calls that were made to Get. Check the length with:

len(mockedRedisClienter.GetCalls())

func (*RedisClienterMock) Ping

func (mock *RedisClienterMock) Ping() *redis.StatusCmd

Ping calls PingFunc.

func (*RedisClienterMock) PingCalls

func (mock *RedisClienterMock) PingCalls() []struct {
}

PingCalls gets all the calls that were made to Ping. Check the length with:

len(mockedRedisClienter.PingCalls())

func (*RedisClienterMock) Set

func (mock *RedisClienterMock) Set(key string, value interface{}, expiration time.Duration) *redis.StatusCmd

Set calls SetFunc.

func (*RedisClienterMock) SetCalls

func (mock *RedisClienterMock) SetCalls() []struct {
	Key        string
	Value      interface{}
	Expiration time.Duration
}

SetCalls gets all the calls that were made to Set. Check the length with:

len(mockedRedisClienter.SetCalls())

Jump to

Keyboard shortcuts

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