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

session_redis.go 810B

123456789101112131415161718192021222324252627282930313233343536
  1. package auth
  2. import (
  3. "time"
  4. "github.com/go-redis/redis"
  5. )
  6. type RedisSessionStore struct{ client *redis.Client }
  7. func NewRedisSessionStore(address string, password string) *RedisSessionStore {
  8. session := &RedisSessionStore{}
  9. session.client = redis.NewClient(&redis.Options{
  10. Addr: address,
  11. Password: password,
  12. })
  13. if err := session.client.Ping().Err(); err != nil {
  14. panic(err)
  15. }
  16. return session
  17. }
  18. func (rss *RedisSessionStore) StoreJwtToken(key string, value string, timeout int64) error {
  19. return rss.client.Set(key, value, time.Duration(timeout) * time.Second).Err()
  20. }
  21. func (rss *RedisSessionStore) IsExistsJwtToken(key string) bool {
  22. return rss.client.Exists(key).Val() == 1
  23. }
  24. func (rss *RedisSessionStore) DeleteJwtToken(key string) bool {
  25. return rss.client.Del(key).Val() == 1
  26. }