redis

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2023 License: MIT Imports: 6 Imported by: 0

README

Redis Sessions for RiveScript

GoDoc

This package provides support for using a Redis cache to store user variables for RiveScript.

go get github.com/aichaos/rivescript-go/sessions/redis

Quick Start

package main

import (
    "fmt"

    rivescript "github.com/aichaos/rivescript-go"
    "github.com/aichaos/rivescript-go/sessions/redis"
    goRedis "gopkg.in/redis.v5"
)

func main() {
    // Verbose example with ALL options spelled out. All the settings are
    // optional, and their default values are shown here.
    bot := rivescript.New(&rivescript.Config{
        // Initialize the Redis session manager here.
        SessionManager: redis.New(&redis.Config{
            // The prefix is added before all the usernames in the Redis cache.
            // For a username of 'alice' it would go into 'rivescript/alice'
            Prefix: "rivescript/",

            // The prefix used to store 'frozen' copies of user variables. The
            // default takes the form "frozen:<prefix>" using your Prefix,
            // so this field is doubly optional unless you wanna customize it.
            FrozenPrefix: "frozen:rivescript/",

            // If you need to configure the underlying Redis instance, you can
            // pass its options along here.
            Redis: &goRedis.Options{
                Addr: "localhost:6379",
                DB:   0,
            },
        }),
    })

    // A minimal version of the above that uses all the default options.
    bot = rivescript.New(&rivescript.Config{
        SessionManager: redis.New(nil),
    })

    bot.LoadDirectory("eg/brain")
    bot.SortReplies()

    // And go on as normal.
    reply, err := bot.Reply("soandso", "hello bot")
    if err != nil {
        fmt.Printf("Error: %s\n", err)
    } else {
        fmt.Printf("Reply: %s\n", reply)
    }
}

Testing

Running these unit tests requires a local Redis server to be running. In the future I'll look into mocking the server.

License

Released under the same terms as RiveScript itself (MIT license).

Documentation

Overview

Package redis implements a Redis backed session manager for RiveScript.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// The key prefix to use in Redis. For example, with a username of 'alice',
	// the Redis key might be 'rivescript/alice'.
	//
	// The default prefix is 'rivescript/'
	Prefix string

	// The key used to prefix frozen user variables (those created by
	// `Freeze()`). The default is `frozen:<prefix>`
	FrozenPrefix string

	// Settings for the Redis client.
	Redis *redis.Options

	// Session timeout using time.duration
	SessionTimeout time.Duration
}

Config allows for configuring the Redis instance and key prefix.

type Session

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

Session wraps a Redis client connection.

func New

func New(options *Config) *Session

New creates a new Redis session instance.

func (*Session) AddHistory

func (s *Session) AddHistory(username, input, reply string)

AddHistory adds to a user's history data.

func (*Session) Clear

func (s *Session) Clear(username string)

Clear deletes all variables about a user.

func (*Session) ClearAll

func (s *Session) ClearAll()

ClearAll resets all user data for all users.

func (*Session) Freeze

func (s *Session) Freeze(username string) error

Freeze makes a snapshot of user variables.

func (*Session) Get

func (s *Session) Get(username, name string) (string, error)

Get a user variable out of Redis.

func (*Session) GetAll

func (s *Session) GetAll() map[string]*sessions.UserData

GetAll gets all data for all users.

func (*Session) GetAny

func (s *Session) GetAny(username string) (*sessions.UserData, error)

GetAny returns all variables about a user.

func (*Session) GetHistory

func (s *Session) GetHistory(username string) (*sessions.History, error)

GetHistory gets the user's history.

func (*Session) GetLastMatch

func (s *Session) GetLastMatch(username string) (string, error)

GetLastMatch retrieves the user's last matched trigger.

func (*Session) Init

func (s *Session) Init(username string) *sessions.UserData

Init makes sure that a username has a session (creates one if not), and returns the pointer to it in any event.

func (*Session) Set

func (s *Session) Set(username string, vars map[string]string)

Set puts a user variable into Redis.

func (*Session) SetLastMatch

func (s *Session) SetLastMatch(username, trigger string)

SetLastMatch sets the user's last matched trigger.

func (*Session) Thaw

func (s *Session) Thaw(username string, action sessions.ThawAction) error

Thaw restores user variables from a snapshot.

Jump to

Keyboard shortcuts

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