Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ go 1.19

require (
github.com/antonlindstrom/pgstore v0.0.0-20220421113606-e3a6e3fed12a
github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff
github.com/bos-hieu/mongostore v0.0.3
github.com/bradfitz/gomemcache v0.0.0-20230905024940-24af94b03874
github.com/bradleypeabody/gorilla-sessions-memcache v0.0.0-20181103040241-659414f458e1
github.com/gin-gonic/gin v1.9.1
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8
github.com/gomodule/redigo v2.0.0+incompatible
github.com/gomodule/redigo v1.9.2
github.com/gorilla/context v1.1.2
github.com/gorilla/sessions v1.2.2
github.com/kidstuff/mongostore v0.0.0-20181113001930-e650cd85ee4b
github.com/memcachier/mc v2.0.1+incompatible
github.com/quasoft/memstore v0.0.0-20191010062613-2bce066d2b0b
github.com/snowdreamtech/redistore v0.0.0-20231007100540-6364ca2c97b4
github.com/wader/gormstore/v2 v2.0.3
go.mongodb.org/mongo-driver v1.14.0
gorm.io/driver/sqlite v1.4.4
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/antonlindstrom/pgstore v0.0.0-20220421113606-e3a6e3fed12a h1:dIdcLbck6W67B5JFMewU5Dba1yKZA3MsT67i4No/zh0=
github.com/antonlindstrom/pgstore v0.0.0-20220421113606-e3a6e3fed12a/go.mod h1:Sdr/tmSOLEnncCuXS5TwZRxuk7deH1WXVY8cve3eVBM=
github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff h1:RmdPFa+slIr4SCBg4st/l/vZWVe9QJKMXGO60Bxbe04=
github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff/go.mod h1:+RTT1BOk5P97fT2CiHkbFQwkK3mjsFAP6zCYV2aXtjw=
github.com/bos-hieu/mongostore v0.0.3 h1:wla8pz4VQU8JOcbo+sBbuvFVBBHt3yRelVSi6YInC48=
github.com/bos-hieu/mongostore v0.0.3/go.mod h1:8AbbVmDEb0yqJsBrWxZIAZOxIfv/tsP8CDtdHduZHGg=
github.com/bradfitz/gomemcache v0.0.0-20230905024940-24af94b03874 h1:N7oVaKyGp8bttX0bfZGmcGkjz7DLQXhAn3DNd3T0ous=
Expand Down Expand Up @@ -53,19 +51,17 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0=
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/gomodule/redigo v1.9.2 h1:HrutZBLhSIU8abiSfW8pj8mPhOyMYjZT/wcA4/L9L9s=
github.com/gomodule/redigo v1.9.2/go.mod h1:KsU3hiK/Ay8U42qpaJk+kuNa3C+spxapWpM+ywhcgtw=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/context v1.1.2 h1:WRkNAv2uoa03QNIc1A6u4O7DAGMUVoopZhkiXWA2V1o=
github.com/gorilla/context v1.1.2/go.mod h1:KDPwT9i/MeWHiLl90fuTgrt4/wPcv75vFAZLaOOcbxM=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA=
github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo=
github.com/gorilla/sessions v1.1.1/go.mod h1:8KCfur6+4Mqcc6S0FEfKuN15Vl5MgXW92AE8ovaJD0w=
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/gorilla/sessions v1.2.2 h1:lqzMYz6bOfvn2WriPUjNByzeXIlVzURcPmgMczkmTjY=
github.com/gorilla/sessions v1.2.2/go.mod h1:ePLdVu+jbEgHH+KWw8I1z2wqd0BAdAQh/8LRvBeoNcQ=
Expand Down Expand Up @@ -185,6 +181,8 @@ github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9Nz
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/snowdreamtech/redistore v0.0.0-20231007100540-6364ca2c97b4 h1:HniAbmj6IsZzZuAouulfsyTDjODtBymeWqbh5lK3EmY=
github.com/snowdreamtech/redistore v0.0.0-20231007100540-6364ca2c97b4/go.mod h1:VTV42RFvMAoztNB+4GFSAbINm6ZioJjYQvdT/RrIGIM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
Expand Down
53 changes: 44 additions & 9 deletions redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package redis
import (
"errors"

"github.com/boj/redistore"
"github.com/gin-contrib/sessions"

"github.com/gomodule/redigo/redis"
"github.com/snowdreamtech/redistore"
)

type Store interface {
Expand Down Expand Up @@ -33,10 +34,19 @@ func NewStore(size int, network, address, password string, keyPairs ...[]byte) (
return &store{s}, nil
}

// NewStoreWithDB - like NewStore but accepts `DB` parameter to select
// redis DB instead of using the default one ("0")
// NewStoreWithDB creates a new Redis-based session store with the specified parameters.
//
// Parameters:
// - size: The maximum number of idle connections in the pool.
// - network: The network type (e.g., "tcp").
// - address: The address of the Redis server (e.g., "localhost:6379").
// - password: The password for the Redis server (if any).
// - DB: The Redis database to be selected after connecting.
// - keyPairs: A variadic list of byte slices used for authentication and encryption.
//
// Ref: https://godoc.org/github.com/boj/redistore#NewRediStoreWithDB
// Returns:
// - Store: The created session store.
// - error: An error if the store could not be created.
func NewStoreWithDB(size int, network, address, password, DB string, keyPairs ...[]byte) (Store, error) {
s, err := redistore.NewRediStoreWithDB(size, network, address, password, DB, keyPairs...)
if err != nil {
Expand All @@ -45,9 +55,18 @@ func NewStoreWithDB(size int, network, address, password, DB string, keyPairs ..
return &store{s}, nil
}

// NewStoreWithPool instantiates a RediStore with a *redis.Pool passed in.
// NewStoreWithPool creates a new session store using a Redis connection pool.
// It takes a redis.Pool and an optional variadic list of key pairs for
// authentication and encryption of session data.
//
// Ref: https://godoc.org/github.com/boj/redistore#NewRediStoreWithPool
// Parameters:
// - pool: A redis.Pool object that manages a pool of Redis connections.
// - keyPairs: Optional variadic list of byte slices used for authentication
// and encryption of session data.
//
// Returns:
// - Store: A new session store backed by Redis.
// - error: An error if the store could not be created.
func NewStoreWithPool(pool *redis.Pool, keyPairs ...[]byte) (Store, error) {
s, err := redistore.NewRediStoreWithPool(pool, keyPairs...)
if err != nil {
Expand All @@ -60,8 +79,15 @@ type store struct {
*redistore.RediStore
}

// GetRedisStore get the actual woking store.
// Ref: https://godoc.org/github.com/boj/redistore#RediStore
// GetRedisStore retrieves the Redis store from the provided Store interface.
// It returns an error if the provided Store is not of the expected type.
//
// Parameters:
// - s: The Store interface from which to retrieve the Redis store.
//
// Returns:
// - err: An error if the provided Store is not of the expected type.
// - rediStore: The retrieved Redis store, or nil if there was an error.
func GetRedisStore(s Store) (err error, rediStore *redistore.RediStore) {
realStore, ok := s.(*store)
if !ok {
Expand All @@ -73,7 +99,16 @@ func GetRedisStore(s Store) (err error, rediStore *redistore.RediStore) {
return
}

// SetKeyPrefix sets the key prefix in the redis database.
// SetKeyPrefix sets a key prefix for the given Redis store.
// It retrieves the Redis store from the provided Store interface and sets the key prefix.
// If there is an error retrieving the Redis store, it returns the error.
//
// Parameters:
// - s: The Store interface from which the Redis store will be retrieved.
// - prefix: The key prefix to be set for the Redis store.
//
// Returns:
// - error: An error if there is an issue retrieving the Redis store, otherwise nil.
func SetKeyPrefix(s Store, prefix string) error {
err, rediStore := GetRedisStore(s)
if err != nil {
Expand Down