redis

package module
v3.0.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2020 License: MIT Imports: 6 Imported by: 0

README

Redis store for Session

Build Codecov ReportCard GoDoc License

Quick Start

Download and install
$ go get -u -v github.com/go-session/redis
Create file server.go
package main

import (
	"context"
	"fmt"
	"net/http"

	"github.com/go-session/redis"
	"github.com/go-session/session"
)

func main() {
	session.InitManager(
		session.SetStore(redis.NewRedisStore(&redis.Options{
			Addr: "127.0.0.1:6379",
			DB:   15,
		})),
	)

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		store, err := session.Start(context.Background(), w, r)
		if err != nil {
			fmt.Fprint(w, err)
			return
		}

		store.Set("foo", "bar")
		err = store.Save()
		if err != nil {
			fmt.Fprint(w, err)
			return
		}

		http.Redirect(w, r, "/foo", 302)
	})

	http.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
		store, err := session.Start(context.Background(), w, r)
		if err != nil {
			fmt.Fprint(w, err)
			return
		}

		foo, ok := store.Get("foo")
		if ok {
			fmt.Fprintf(w, "foo:%s", foo)
			return
		}
		fmt.Fprint(w, "does not exist")
	})

	http.ListenAndServe(":8080", nil)
}
Build and run
$ go build server.go
$ ./server
Open in your web browser

http://localhost:8080

foo:bar

MIT License

Copyright (c) 2018 Lyric

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRedisClusterStore

func NewRedisClusterStore(opts *redis.ClusterOptions, prefix ...string) session.ManagerStore

NewRedisClusterStore create an instance of a redis cluster store

func NewRedisClusterStoreWithCli

func NewRedisClusterStoreWithCli(cli *redis.ClusterClient, prefix ...string) session.ManagerStore

NewRedisClusterStoreWithCli create an instance of a redis cluster store

func NewRedisStore

func NewRedisStore(opts *redis.Options, prefix ...string) session.ManagerStore

NewRedisStore create an instance of a redis store

func NewRedisStoreWithCli

func NewRedisStoreWithCli(cli *redis.Client, prefix ...string) session.ManagerStore

NewRedisStoreWithCli create an instance of a redis store

Types

This section is empty.

Jump to

Keyboard shortcuts

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