redis.go 647 B

12345678910111213141516171819202122232425262728293031323334
  1. package ipblack
  2. import (
  3. "context"
  4. "time"
  5. "github.com/labring/aiproxy/core/common"
  6. )
  7. const (
  8. ipBlackKey = "ip_black:%s"
  9. )
  10. func redisSetIPBlack(ctx context.Context, ip string, duration time.Duration) (bool, error) {
  11. key := common.RedisKeyf(ipBlackKey, ip)
  12. success, err := common.RDB.SetNX(ctx, key, duration.Seconds(), duration).Result()
  13. if err != nil {
  14. return false, err
  15. }
  16. return !success, nil
  17. }
  18. func redisGetIPIsBlock(ctx context.Context, ip string) (bool, error) {
  19. key := common.RedisKeyf(ipBlackKey, ip)
  20. exists, err := common.RDB.Exists(ctx, key).Result()
  21. if err != nil {
  22. return false, err
  23. }
  24. return exists > 0, nil
  25. }