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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package auth
  2. import (
  3. "encoding/json"
  4. "time"
  5. "git.links123.net/links123.com/pkg/utils"
  6. "github.com/go-redis/redis"
  7. )
  8. type (
  9. RedisSessionStore struct{ client *redis.Client }
  10. item map[string]interface{}
  11. )
  12. func NewRedisSessionStore(address string, password string) *RedisSessionStore {
  13. session := &RedisSessionStore{}
  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. return session
  22. }
  23. func (rss *RedisSessionStore) StoreJwtToken(token string, uid int64, timeout int64) error {
  24. k := utils.Md5(token)
  25. v := item{"user_id": uid, "created": time.Now().Unix()}
  26. return rss.client.Set(k, v, time.Duration(timeout)*time.Second).Err()
  27. }
  28. func (rss *RedisSessionStore) IsExistsJwtToken(token string) bool {
  29. k := utils.Md5(token)
  30. return rss.client.Exists(k).Val() == 1
  31. }
  32. func (rss *RedisSessionStore) DeleteJwtToken(token string) bool {
  33. k := utils.Md5(token)
  34. return rss.client.Del(k).Val() == 1
  35. }
  36. func (i item) MarshalBinary() (data []byte, err error) {
  37. return json.Marshal(i)
  38. }