59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package cache
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/apex/log"
|
|
"github.com/sparetimecoders/goamqp"
|
|
|
|
"gitlab.com/unboundsoftware/schemas/domain"
|
|
)
|
|
|
|
const subGraphKey = "%s<->%s"
|
|
|
|
type Cache struct {
|
|
services map[string]map[string]struct{}
|
|
subGraphs map[string]string
|
|
logger log.Interface
|
|
}
|
|
|
|
func (c *Cache) Services(ref string) []string {
|
|
var services []string
|
|
for k := range c.services[ref] {
|
|
services = append(services, k)
|
|
}
|
|
return services
|
|
}
|
|
|
|
func (c *Cache) SubGraphId(ref, service string) string {
|
|
return c.subGraphs[fmt.Sprintf(subGraphKey, ref, service)]
|
|
}
|
|
|
|
func (c *Cache) Update(msg any, _ goamqp.Headers) (any, error) {
|
|
switch m := msg.(type) {
|
|
case *domain.SubGraphUpdated:
|
|
if _, exists := c.services[m.Ref]; !exists {
|
|
c.services[m.Ref] = make(map[string]struct{})
|
|
}
|
|
c.services[m.Ref][m.ID.String()] = struct{}{}
|
|
c.subGraphs[fmt.Sprintf(subGraphKey, m.Ref, m.Service)] = m.ID.String()
|
|
case *domain.SubGraph:
|
|
if _, exists := c.services[m.Ref]; !exists {
|
|
c.services[m.Ref] = make(map[string]struct{})
|
|
}
|
|
c.services[m.Ref][m.ID.String()] = struct{}{}
|
|
c.subGraphs[fmt.Sprintf(subGraphKey, m.Ref, m.Service)] = m.ID.String()
|
|
default:
|
|
c.logger.Warnf("unexpected message received: %+v", msg)
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func New(logger log.Interface) *Cache {
|
|
return &Cache{
|
|
subGraphs: make(map[string]string),
|
|
services: make(map[string]map[string]struct{}),
|
|
logger: logger,
|
|
}
|
|
}
|