另客网go项目公用的代码库

session_redis.go 982B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package auth
  2. import (
  3. "github.com/go-redis/redis"
  4. "sync"
  5. "time"
  6. )
  7. type RedisSessionStore struct {
  8. once *sync.Once
  9. client *redis.Client
  10. }
  11. func NewRedisSessionStore(address string, password string) *RedisSessionStore {
  12. session := &RedisSessionStore{once:&sync.Once{}}
  13. session.once.Do(func() {
  14. session.client = redis.NewClient(&redis.Options{
  15. Addr: address,
  16. Password: password,
  17. })
  18. if err := session.client.Ping().Err(); err != nil {
  19. panic(err)
  20. }
  21. })
  22. return session
  23. }
  24. func (rss *RedisSessionStore) StoreJwtToken(key string, value string, timeout time.Duration) error {
  25. return rss.client.Set(key, value, timeout).Err()
  26. }
  27. func (rss *RedisSessionStore) IsExistsJwtToken(key string) bool {
  28. if v := rss.client.Exists(key).Val(); v != 1 {
  29. return false
  30. }
  31. return true
  32. }
  33. // DeleteJwtToken delete jwt token
  34. func (rss *RedisSessionStore) DeleteJwtToken(key string) bool {
  35. if v := rss.client.Del(key).Val(); v != 1 {
  36. return false
  37. }
  38. return true
  39. }