Skip to content

Remove entry mutex #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions entry.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package icache

import "sync"

type entries[T any] map[uint64]*entry[T]

type entrySlice[T any] []*entry[T]
Expand All @@ -13,5 +11,4 @@ type entry[T any] struct {
expiresAt int64
tags []uint64
deleted bool
rw sync.RWMutex
}
32 changes: 2 additions & 30 deletions pot.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,22 @@ func (p *pot[T]) Purge() {
}

func (p *pot[T]) Len() int {
p.windowRW.RLock()
defer p.windowRW.RUnlock()

return p.shards.EntriesLen()
}

func (p *pot[T]) Exists(key string) (ok bool) {
p.windowRW.RLock()
defer p.windowRW.RUnlock()

k, shard := keyGen(key)
exists := p.shards[shard].EntryExists(k)

return exists
}

func (p *pot[T]) ExpireTime(key string) (t *time.Time, err error) {
p.windowRW.RLock()
defer p.windowRW.RUnlock()

e, ok := p.getEntry(key)
if !ok {
return nil, ErrNotFound
}

e.rw.RLock()
defer e.rw.RUnlock()
ti := time.UnixMilli(e.expiresAt)
return &ti, nil
}
Expand All @@ -100,9 +89,6 @@ func (p *pot[T]) getEntry(key string) (*entry[T], bool) {
}

func (p *pot[T]) GetByTag(tag string) ([]T, error) {
p.windowRW.RLock()
defer p.windowRW.RUnlock()

entries := p.tags.getEntriesWithTags(tagKeyGen(tag)...)
if len(entries) == 0 {
return nil, ErrNotFound
Expand All @@ -115,16 +101,11 @@ func (p *pot[T]) GetByTag(tag string) ([]T, error) {
}

func (p *pot[T]) Get(key string) (v T, err error) {
p.windowRW.RLock()
defer p.windowRW.RUnlock()

e, ok := p.getEntry(key)
if !ok {
return v, ErrNotFound
}

e.rw.RLock()
defer e.rw.RUnlock()
if e.deleted {
p.dropEntry(e)
return v, ErrNotFound
Expand All @@ -135,9 +116,6 @@ func (p *pot[T]) Get(key string) (v T, err error) {

func (p *pot[T]) Set(key string, v T, tags ...string) {
expireTime := time.Now().Add(p.ttl).UnixNano()
p.windowRW.Lock()
defer p.windowRW.Unlock()

k, shard := keyGen(key)
e, found := p.shards[shard].GetEntry(k)
if found {
Expand All @@ -153,25 +131,23 @@ func (p *pot[T]) Set(key string, v T, tags ...string) {
}

if p.ttl > 0 {
p.windowRW.Lock()
p.window = append(p.window, e)
p.windowRW.Unlock()
}

p.tags.add(e)
p.shards[shard].SetEntry(k, e)
}

func (p *pot[T]) DropTags(tags ...string) {
p.windowRW.Lock()
defer p.windowRW.Unlock()
entriesToDrop := p.tags.getEntriesWithTags(tagKeyGen(tags...)...)
for _, e := range entriesToDrop {
p.dropEntry(e)
}
}

func (p *pot[T]) Drop(keys ...string) {
p.windowRW.Lock()
defer p.windowRW.Unlock()
for _, key := range keys {
if e, ok := p.getEntry(key); ok {
p.dropEntry(e)
Expand All @@ -181,7 +157,6 @@ func (p *pot[T]) Drop(keys ...string) {

func (p *pot[T]) dropExpiredEntries(at time.Time) {
now := at.UnixNano()

p.windowRW.Lock()
defer p.windowRW.Unlock()

Expand All @@ -191,15 +166,12 @@ func (p *pot[T]) dropExpiredEntries(at time.Time) {
expiredWindows++
continue
}
e.rw.Lock()
if e.expiresAt >= now { // not expired yet
e.rw.Unlock()
break
}
e.deleted = true
p.dropEntry(e)
expiredWindows++
e.rw.Unlock()
}
if expiredWindows > 0 {
remaining := len(p.window) - expiredWindows
Expand Down
2 changes: 0 additions & 2 deletions shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ func (s *shard[T]) EntryExists(key uint64) bool {
if !ok || e == nil {
return false
}
e.rw.RLock()
defer e.rw.RUnlock()
return !e.deleted
}

Expand Down
3 changes: 1 addition & 2 deletions tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (t *tags[T]) dropTagIfNoOtherEntriesExist(tag uint64) {
}

func (t *tags[T]) getEntriesWithTags(tags ...uint64) entrySlice[T] {

t.rw.RLock()
defer t.rw.RUnlock()

Expand All @@ -58,11 +59,9 @@ func (t *tags[T]) getEntriesWithTags(tags ...uint64) entrySlice[T] {
if e == nil {
continue
}
e.rw.RLock()
if !e.deleted {
results = append(results, e)
}
e.rw.RUnlock()
}
}
return results
Expand Down