redis_store.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package sessions
  2. import (
  3. "github.com/boj/redistore"
  4. "github.com/gorilla/sessions"
  5. )
  6. // RedisStore is an interface that represents a Cookie based storage
  7. // for Sessions.
  8. type RediStore interface {
  9. // Store is an embedded interface so that RedisStore can be used
  10. // as a session store.
  11. Store
  12. // Options sets the default options for each session stored in this
  13. // CookieStore.
  14. Options(Options)
  15. }
  16. // NewCookieStore returns a new CookieStore.
  17. //
  18. // Keys are defined in pairs to allow key rotation, but the common case is to set a single
  19. // authentication key and optionally an encryption key.
  20. //
  21. // The first key in a pair is used for authentication and the second for encryption. The
  22. // encryption key can be set to nil or omitted in the last pair, but the authentication key
  23. // is required in all pairs.
  24. //
  25. // It is recommended to use an authentication key with 32 or 64 bytes. The encryption key,
  26. // if set, must be either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256 modes.
  27. func NewRediStore(size int, network, address, password string, keyPairs ...[]byte) (RediStore, error) {
  28. store, err := redistore.NewRediStore(size, network, address, password, keyPairs...)
  29. if err != nil {
  30. return nil, err
  31. }
  32. return &rediStore{store}, nil
  33. }
  34. type rediStore struct {
  35. *redistore.RediStore
  36. }
  37. func (c *rediStore) Options(options Options) {
  38. c.RediStore.Options = &sessions.Options{
  39. Path: options.Path,
  40. Domain: options.Domain,
  41. MaxAge: options.MaxAge,
  42. Secure: options.Secure,
  43. HttpOnly: options.HttpOnly,
  44. }
  45. }