package test_redis_handler
import (
"fmt"
"github.com/go-redis/redis"
"testing"
)
type redisConnStructTu struct {
redisConn *redis.Client
}
func (redisT *redisConnStructTu) handleMessage(message *redis.Message) {
fmt.Println("Received message:", message.Channel, "===", message.Payload)
}
func (redisT *redisConnStructTu) redisRub() {
hannels := []string{"channel:1", "channel:2"}
pubsub := redisT.redisConn.Subscribe(hannels...)
defer pubsub.Close()
for {
msg, err := pubsub.ReceiveMessage()
if err != nil {
fmt.Println("Error receiving message:", err)
break
}
redisT.handleMessage(msg)
}
}
func TestExecRedisRu(test *testing.T) {
redisConnTu := redisConnStructTu{}
redisConnTu.redisConn = getRedisConn()
redisConnTu.redisRub()
}
package test_redis_handler
import (
"github.com/go-redis/redis"
"testing"
)
type redisConnStructPu struct {
redisConn *redis.Client
}
func (redisS *redisConnStructPu) publishMessage(channel, message string) {
redisS.redisConn.Publish(channel, message)
}
func (redisS *redisConnStructPu) redisPublish() {
redisS.publishMessage("channel:1", "Hello, Redis!")
redisS.publishMessage("channel:2", "你好,Redis!")
}
func TestExecuteRedisPu(t *testing.T) {
redisStruct := redisConnStructPu{}
redisStruct.redisConn = getRedisConn()
redisStruct.redisPublish()
}
func getRedisConn() *redis.Client {
redisConn := redis.NewClient(&redis.Options{
Addr: "101.43.29.136:6379",
Password: "",
})
return redisConn
}