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

session_redis.go 835B

123456789101112131415161718192021222324252627282930313233343536373839
  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.client = redis.NewClient(&redis.Options{
  14. Addr: address,
  15. Password: password,
  16. })
  17. if err := session.client.Ping().Err(); err != nil {
  18. panic(err)
  19. }
  20. return session
  21. }
  22. func (rss *RedisSessionStore) StoreJwtToken(key string, value string, timeout time.Duration) error {
  23. return rss.client.Set(key, value, timeout).Err()
  24. }
  25. func (rss *RedisSessionStore) IsExistsJwtToken(key string) bool {
  26. return rss.client.Exists(key).Val() == 1
  27. }
  28. func (rss *RedisSessionStore) DeleteJwtToken(key string) bool {
  29. return rss.client.Del(key).Val() == 1
  30. }