sessions

package
v1.0.1-0...-860e614 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2021 License: MIT Imports: 9 Imported by: 0

README

dp-redis-clients-go/sessions

dp-redis is a Go client for adding/retrieving user session objects to/from a Redis Cache instance.

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

    dpRedis "github.com/ONSdigital/dp-redis-clients-go/sessions"
)

func main() {
    cfg := dpRedis.Config{
        Addr:     "redis_address",
        Password: "redis_password",
        Database: "database_name",
        TTL:      0, // Time to live config
        TLS: &tls.Config{
            // configure as required
        },
    }

    cli, err := dpredis.NewClient(cfg)
    if err != nil {
        // handle err
    }
    ...
}   

Get session by ID:

s, err := cache.GetByID("the_session_id")

if err != nil {
    // handle error
}

Get session by email:

s, err := cache.GetByEmail("user_email")

if err != nil {
    // handle error
}

Set session:

startTime := time.Now()

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

if err := cache.Set(s); err != nil {
    // handle error
}

Delete all sessions:

if err := cache.DeleteAll(); err != nil {
    // handle error
    ...
}
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

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

GetByEmail - gets a session from redis using its ID

func (*Client) GetByID

func (c *Client) GetByID(id string) (*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) 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())

type Session

type Session struct {
	ID           string    `json:"id"`
	Email        string    `json:"email"`
	Start        time.Time `json:"start"`
	LastAccessed time.Time `json:"lastAccessed"`
}

Session defines the format of a user session object as it is stored in the cache.

func (*Session) MarshalJSON

func (s *Session) MarshalJSON() ([]byte, error)

MarshalJSON is custom JSON marshaller for the Session object ensuring the date fields are marshalled into the correct format

Jump to

Keyboard shortcuts

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