Add cache worker

Add dedicated worker to maintain cache.

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
Gabriel Adrian Samfira 2025-05-07 08:01:36 +00:00
parent e49b35d3d0
commit d0c9462a5d
13 changed files with 355 additions and 208 deletions

2
cache/cache_test.go vendored
View file

@ -324,7 +324,7 @@ func (c *CacheTestSuite) TestReplaceEntityScaleSets() {
}
SetEntity(entity)
ReplaceEntityScaleSets(entity.ID, map[uint]params.ScaleSet{1: scaleSet1, 2: scaleSet2})
ReplaceEntityScaleSets(entity.ID, []params.ScaleSet{scaleSet1, scaleSet2})
cachedEntity, ok := GetEntity(entity.ID)
c.Require().True(ok)
c.Require().Equal(entity.ID, cachedEntity.ID)

17
cache/entity_cache.go vendored
View file

@ -82,14 +82,21 @@ func (e *EntityCache) ReplaceEntityPools(entityID string, pools []params.Pool) {
e.entities[entityID] = cache
}
func (e *EntityCache) ReplaceEntityScaleSets(entityID string, scaleSets map[uint]params.ScaleSet) {
func (e *EntityCache) ReplaceEntityScaleSets(entityID string, scaleSets []params.ScaleSet) {
e.mux.Lock()
defer e.mux.Unlock()
if cache, ok := e.entities[entityID]; ok {
cache.ScaleSets = scaleSets
e.entities[entityID] = cache
cache, ok := e.entities[entityID]
if !ok {
return
}
scaleSetsByID := map[uint]params.ScaleSet{}
for _, scaleSet := range scaleSets {
scaleSetsByID[scaleSet.ID] = scaleSet
}
cache.ScaleSets = scaleSetsByID
e.entities[entityID] = cache
}
func (e *EntityCache) DeleteEntity(entityID string) {
@ -219,7 +226,7 @@ func ReplaceEntityPools(entityID string, pools []params.Pool) {
entityCache.ReplaceEntityPools(entityID, pools)
}
func ReplaceEntityScaleSets(entityID string, scaleSets map[uint]params.ScaleSet) {
func ReplaceEntityScaleSets(entityID string, scaleSets []params.ScaleSet) {
entityCache.ReplaceEntityScaleSets(entityID, scaleSets)
}