accessToken.go 928 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package wechat
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/go-redis/redis"
  6. "time"
  7. )
  8. const AccessTokenKey = "wechat:access_token:%s"
  9. type AccessToken struct {
  10. Client redis.UniversalClient
  11. ctx context.Context
  12. }
  13. func NewAccessToken(client redis.UniversalClient) *AccessToken {
  14. return &AccessToken{Client: client, ctx: context.Background()}
  15. }
  16. func (this *AccessToken) Get(key string) interface{} {
  17. val := this.Client.Get(fmt.Sprintf(AccessTokenKey, key)).Val()
  18. if val == "" {
  19. return nil
  20. }
  21. return val
  22. }
  23. func (this *AccessToken) Set(key string, val interface{}, timeout time.Duration) error {
  24. return this.Client.Set(fmt.Sprintf(AccessTokenKey, key), val, timeout).Err()
  25. }
  26. func (this *AccessToken) IsExist(key string) bool {
  27. return this.Client.Exists(fmt.Sprintf(AccessTokenKey, key)).Val() == 1
  28. }
  29. func (this *AccessToken) Delete(key string) error {
  30. return this.Client.Del(fmt.Sprintf(AccessTokenKey, key)).Err()
  31. }