-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
revert: redis to []chan event.Event for subscribers
- Loading branch information
Showing
4 changed files
with
28 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package storage | ||
|
||
import "github.com/sreeram77/pubsub/event" | ||
|
||
type Cache interface { | ||
Get(string) []any | ||
Set(string, any) | ||
Get(string) []chan event.Event | ||
Set(string, chan event.Event) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,35 @@ | ||
package storage | ||
|
||
import ( | ||
"github.com/redis/go-redis/v9" | ||
) | ||
import "github.com/sreeram77/pubsub/event" | ||
|
||
type subscribers []chan event.Event | ||
|
||
type cache struct { | ||
client *redis.Client | ||
connections map[string]subscribers | ||
} | ||
|
||
func New() Cache { | ||
rdb := redis.NewClient(&redis.Options{ | ||
Addr: "localhost:6379", | ||
Password: "", // no password set | ||
DB: 0, // use default DB | ||
}) | ||
connections := make(map[string]subscribers) | ||
|
||
return &cache{ | ||
client: rdb, | ||
connections: connections, | ||
} | ||
} | ||
|
||
func (c *cache) Get(key string) []any { | ||
return []any{} | ||
func (c *cache) Get(key string) []chan event.Event { | ||
if value, found := c.connections[key]; found { | ||
return value | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *cache) Set(key string, value any) { | ||
func (c *cache) Set(key string, value chan event.Event) { | ||
_, found := c.connections[key] | ||
if found { | ||
c.connections[key] = append(c.connections[key], value) | ||
return | ||
} | ||
|
||
c.connections[key] = []chan event.Event{value} | ||
} |