Skip to content

Commit

Permalink
GO-4171: fix cache
Browse files Browse the repository at this point in the history
Signed-off-by: AnastasiaShemyakinskaya <shem98a@mail.ru>
  • Loading branch information
AnastasiaShemyakinskaya committed Oct 15, 2024
1 parent 93b6daf commit ad7ee9d
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions core/subscription/cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package subscription

import (
"sync"

"github.com/gogo/protobuf/types"

"github.com/anyproto/anytype-heart/util/slice"
Expand Down Expand Up @@ -86,13 +88,18 @@ func (e *entry) Get(key string) *types.Value {

type cache struct {
entries map[string]*entry
sync.Mutex
}

func (c *cache) Get(id string) *entry {
c.Lock()
defer c.Unlock()
return c.entries[id]
}

func (c *cache) GetOrSet(e *entry) *entry {
c.Lock()
defer c.Unlock()
if res, ok := c.entries[e.id]; ok {
return res
}
Expand All @@ -101,10 +108,14 @@ func (c *cache) GetOrSet(e *entry) *entry {
}

func (c *cache) Set(e *entry) {
c.Lock()
defer c.Unlock()
c.entries[e.id] = e
}

func (c *cache) Remove(id string) {
c.Lock()
defer c.Unlock()
delete(c.entries, id)
}

Expand Down

0 comments on commit ad7ee9d

Please sign in to comment.