...

Source file src/github.com/gin-contrib/sessions/redis/redis.go

Documentation: github.com/gin-contrib/sessions/redis

     1  package redis
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/boj/redistore"
     7  	"github.com/gin-contrib/sessions"
     8  	"github.com/gomodule/redigo/redis"
     9  )
    10  
    11  type Store interface {
    12  	sessions.Store
    13  }
    14  
    15  // size: maximum number of idle connections.
    16  // network: tcp or udp
    17  // address: host:port
    18  // password: redis-password
    19  // Keys are defined in pairs to allow key rotation, but the common case is to set a single
    20  // authentication key and optionally an encryption key.
    21  //
    22  // The first key in a pair is used for authentication and the second for encryption. The
    23  // encryption key can be set to nil or omitted in the last pair, but the authentication key
    24  // is required in all pairs.
    25  //
    26  // It is recommended to use an authentication key with 32 or 64 bytes. The encryption key,
    27  // if set, must be either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256 modes.
    28  func NewStore(size int, network, address, password string, keyPairs ...[]byte) (Store, error) {
    29  	s, err := redistore.NewRediStore(size, network, address, password, keyPairs...)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	return &store{s}, nil
    34  }
    35  
    36  // NewStoreWithDB - like NewStore but accepts `DB` parameter to select
    37  // redis DB instead of using the default one ("0")
    38  //
    39  // Ref: https://godoc.org/github.com/boj/redistore#NewRediStoreWithDB
    40  func NewStoreWithDB(size int, network, address, password, DB string, keyPairs ...[]byte) (Store, error) {
    41  	s, err := redistore.NewRediStoreWithDB(size, network, address, password, DB, keyPairs...)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	return &store{s}, nil
    46  }
    47  
    48  // NewStoreWithPool instantiates a RediStore with a *redis.Pool passed in.
    49  //
    50  // Ref: https://godoc.org/github.com/boj/redistore#NewRediStoreWithPool
    51  func NewStoreWithPool(pool *redis.Pool, keyPairs ...[]byte) (Store, error) {
    52  	s, err := redistore.NewRediStoreWithPool(pool, keyPairs...)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	return &store{s}, nil
    57  }
    58  
    59  type store struct {
    60  	*redistore.RediStore
    61  }
    62  
    63  // GetRedisStore get the actual woking store.
    64  // Ref: https://godoc.org/github.com/boj/redistore#RediStore
    65  func GetRedisStore(s Store) (err error, rediStore *redistore.RediStore) {
    66  	realStore, ok := s.(*store)
    67  	if !ok {
    68  		err = errors.New("unable to get the redis store: Store isn't *store")
    69  		return
    70  	}
    71  
    72  	rediStore = realStore.RediStore
    73  	return
    74  }
    75  
    76  // SetKeyPrefix sets the key prefix in the redis database.
    77  func SetKeyPrefix(s Store, prefix string) error {
    78  	err, rediStore := GetRedisStore(s)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	rediStore.SetKeyPrefix(prefix)
    84  	return nil
    85  }
    86  
    87  func (c *store) Options(options sessions.Options) {
    88  	c.RediStore.Options = options.ToGorillaOptions()
    89  }
    90  

View as plain text