feat: cache add single-flight and timing-wheel.

This commit is contained in:
Gordon
2024-01-12 13:27:54 +08:00
parent 006e4a1e93
commit 1f1ab65375
13 changed files with 316 additions and 184 deletions
+16
View File
@@ -0,0 +1,16 @@
package redispubsub
import "github.com/redis/go-redis/v9"
type Publisher struct {
client redis.UniversalClient
channel string
}
func NewPublisher(client redis.UniversalClient, channel string) *Publisher {
return &Publisher{client: client, channel: channel}
}
func (p *Publisher) Publish(message string) error {
return p.client.Publish(ctx, p.channel, message).Err()
}
+27
View File
@@ -0,0 +1,27 @@
package redispubsub
import (
"context"
"github.com/redis/go-redis/v9"
)
var ctx = context.Background()
type Subscriber struct {
client redis.UniversalClient
channel string
}
func NewSubscriber(client redis.UniversalClient, channel string) *Subscriber {
return &Subscriber{client: client, channel: channel}
}
func (s *Subscriber) OnMessage(callback func(string)) error {
messageChannel := s.client.Subscribe(ctx, s.channel).Channel()
go func() {
for msg := range messageChannel {
callback(msg.Payload)
}
}()
return nil
}