mirror of
https://github.com/bit1001/tdl.git
synced 2024-11-10 16:33:04 +08:00
36 lines
622 B
Go
36 lines
622 B
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/iyear/tdl/pkg/key"
|
|
"github.com/iyear/tdl/pkg/kv"
|
|
)
|
|
|
|
type Session struct {
|
|
kv *kv.KV
|
|
login bool
|
|
}
|
|
|
|
func NewSession(kv *kv.KV, login bool) *Session {
|
|
return &Session{kv: kv, login: login}
|
|
}
|
|
|
|
func (s *Session) LoadSession(_ context.Context) ([]byte, error) {
|
|
if s.login {
|
|
return nil, nil
|
|
}
|
|
|
|
b, err := s.kv.Get(key.Session())
|
|
if err != nil {
|
|
if errors.Is(err, kv.ErrNotFound) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
return b, nil
|
|
}
|
|
|
|
func (s *Session) StoreSession(_ context.Context, data []byte) error {
|
|
return s.kv.Set(key.Session(), data)
|
|
}
|