Skip to content

Commit

Permalink
fix(cache): remove old group data if new data is empty on refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
ThinkChaos committed Nov 12, 2023
1 parent 3e0df5f commit 435615b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
15 changes: 14 additions & 1 deletion cache/stringcache/chained_grouped_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ var _ = Describe("Chained grouped cache", func() {
Expect(cache.Contains("both", []string{"group1", "group2"})).Should(ConsistOf("group1", "group2"))
})

It("Should replace group content on refresh", func() {
It("should replace group content on refresh", func() {
factory = cache.Refresh("group1")
factory.AddEntry("newString")
factory.Finish()
Expand All @@ -99,6 +99,19 @@ var _ = Describe("Chained grouped cache", func() {
Expect(cache.Contains("g2", []string{"group1", "group2"})).Should(ConsistOf("group2"))
Expect(cache.Contains("both", []string{"group1", "group2"})).Should(ConsistOf("group2"))
})

It("should replace empty groups on refresh", func() {
factory = cache.Refresh("group")
factory.AddEntry("begone")
factory.Finish()

Expect(cache.ElementCount("group")).Should(BeNumerically("==", 2))

factory = cache.Refresh("group")
factory.Finish()

Expect(cache.ElementCount("group")).Should(BeNumerically("==", 0))
})
})
})
})
9 changes: 7 additions & 2 deletions cache/stringcache/in_memory_grouped_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ func (c *InMemoryGroupedCache) Refresh(group string) GroupFactory {
factory: c.factoryFn(),
finishFn: func(sc stringCache) {
c.lock.Lock()
c.caches[group] = sc
c.lock.Unlock()
defer c.lock.Unlock()

if sc != nil {
c.caches[group] = sc
} else {
delete(c.caches, group)
}
},
}
}
Expand Down
8 changes: 8 additions & 0 deletions cache/stringcache/string_caches.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ func (s *stringCacheFactory) addEntry(entry string) {
}

func (s *stringCacheFactory) create() stringCache {
if len(s.tmp) == 0 {
return nil
}

cache := make(stringMap, len(s.tmp))
for k, v := range s.tmp {
cache[k] = strings.Join(v, "")
Expand Down Expand Up @@ -161,6 +165,10 @@ func (r *regexCacheFactory) count() int {
}

func (r *regexCacheFactory) create() stringCache {
if len(r.cache) == 0 {
return nil
}

return r.cache
}

Expand Down
29 changes: 29 additions & 0 deletions cache/stringcache/string_caches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ var _ = Describe("Caches", func() {
factory cacheFactory
)
Describe("String StringCache", func() {
It("should not return a cache when empty", func() {
Expect(newStringCacheFactory().create()).Should(BeNil())
})

It("should recognise the empty string", func() {
factory := newStringCacheFactory()

factory.addEntry("")

Expect(factory.count()).Should(BeNumerically("==", 0))
Expect(factory.create()).Should(BeNil())
})

When("string StringCache was created", func() {
BeforeEach(func() {
factory = newStringCacheFactory()
Expand Down Expand Up @@ -42,6 +55,22 @@ var _ = Describe("Caches", func() {
})

Describe("Regex StringCache", func() {
It("should not return a cache when empty", func() {
Expect(newRegexCacheFactory().create()).Should(BeNil())
})

It("should recognise invalid regexes", func() {
factory := newRegexCacheFactory()

factory.addEntry("/*/")
factory.addEntry("/?/")
factory.addEntry("/+/")
factory.addEntry("/[/")

Expect(factory.count()).Should(BeNumerically("==", 0))
Expect(factory.create()).Should(BeNil())
})

When("regex StringCache was created", func() {
BeforeEach(func() {
factory = newRegexCacheFactory()
Expand Down

0 comments on commit 435615b

Please sign in to comment.