Skip to content

Commit

Permalink
Handle missing corner case
Browse files Browse the repository at this point in the history
  • Loading branch information
aduffeck committed Jun 19, 2023
1 parent 4e1cf9b commit f3b4dc1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ func (c *Cache[K, T]) Store(key K, mtime time.Time, value T) error {
return nil
}

func (c *Cache[K, T]) Load(key K) T {
entry, _ := c.entries.Load(key)
return entry.value
func (c *Cache[K, T]) Load(key K) (T, bool) {
entry, ok := c.entries.Load(key)
if !ok {
var t T
return t, false
}
return entry.value, true
}

func (c *Cache[K, T]) LoadOrStore(key K, mtime time.Time, f func() (T, error)) (T, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,21 @@ var _ = Describe("Mtimesyncedcache", func() {
err := cache.Store(key, time, value)
Expect(err).ToNot(HaveOccurred())
})

PIt("returns an error when the mtime is older")
})

Describe("Load", func() {
It("loads the stored value", func() {
err := cache.Store(key, time.Now(), value)
Expect(err).ToNot(HaveOccurred())

v := cache.Load(key)
v, ok := cache.Load(key)
Expect(ok).To(BeTrue())
Expect(v).To(Equal(value))
})

PIt("fails the value doesn't exist", func() {
v := cache.Load(key)
Expect(v).To(Equal(value))
It("reports when the key doesn't exist", func() {
_, ok := cache.Load("doesnotexist")
Expect(ok).To(BeFalse())
})
})

Expand Down

0 comments on commit f3b4dc1

Please sign in to comment.