You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

redis_test.go 1.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright © 2023 OpenIM open source community. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cache
  15. import (
  16. "context"
  17. "fmt"
  18. "log"
  19. "testing"
  20. "time"
  21. "github.com/go-redis/redis/v8"
  22. )
  23. func TestSub(t *testing.T) {
  24. client := redis.NewClient(&redis.Options{
  25. Addr: "localhost:6380",
  26. Password: "openkf",
  27. DB: 0,
  28. })
  29. ctx := context.Background()
  30. pubsub := client.PSubscribe(ctx, "__keyevent@0__:expired")
  31. ch := pubsub.Channel()
  32. go func() {
  33. for msg := range ch {
  34. fmt.Println("Events:", msg.Payload)
  35. }
  36. }()
  37. err := client.Set(ctx, "mykey", "myvalue", 5*time.Second).Err()
  38. if err != nil {
  39. log.Fatal(err)
  40. }
  41. time.Sleep(10 * time.Second)
  42. err = pubsub.Close()
  43. if err != nil {
  44. log.Fatal(err)
  45. }
  46. err = client.Close()
  47. if err != nil {
  48. log.Fatal(err)
  49. }
  50. }