Skip to content

Commit

Permalink
chore: switch old store to snowdreamtech/redistore (#269)
Browse files Browse the repository at this point in the history
- Remove `github.com/boj/redistore` dependency from `go.mod`
- Update `github.com/gomodule/redigo` to version `v1.9.2` in `go.mod`
- Add `github.com/snowdreamtech/redistore` dependency to `go.mod`
- Replace `github.com/boj/redistore` import with `github.com/snowdreamtech/redistore` in `redis.go`
- Add detailed comments for `NewStoreWithDB` function parameters and return values
- Add detailed comments for `NewStoreWithPool` function parameters and return values
- Add detailed comments for `GetRedisStore` function parameters and return values
- Add detailed comments for `SetKeyPrefix` function parameters and return values

cc @snowdream

Signed-off-by: appleboy <appleboy.tw@gmail.com>
  • Loading branch information
appleboy authored Dec 26, 2024
1 parent 8e4ea71 commit ace1fd0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
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

0 comments on commit ace1fd0

Please sign in to comment.