Skip to content

Commit

Permalink
Bugfix for Put not updating existing data
Browse files Browse the repository at this point in the history
  • Loading branch information
jftuga committed Nov 13, 2023
1 parent 513c9a3 commit a0ed693
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
9 changes: 2 additions & 7 deletions ttlMap.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"time"
)

const version string = "1.5.0"
const version string = "1.5.1"

type CustomKeyType interface {
comparable
Expand Down Expand Up @@ -77,12 +77,7 @@ func (m *TtlMap[T]) Len() int {

func (m *TtlMap[T]) Put(k T, v interface{}) {
m.l.Lock()
it, ok := m.m[k]
if !ok {
it = &item{Value: v}
m.m[k] = it
}
it.lastAccess = time.Now().Unix()
m.m[k] = &item{Value: v, lastAccess: time.Now().Unix()}
m.l.Unlock()
}

Expand Down
22 changes: 22 additions & 0 deletions ttlMap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,25 @@ func TestByteKey(t *testing.T) {
t.Errorf("t.Len should be 0, but actually equals %v\n", tm.Len())
}
}

func TestMultiplePuts(t *testing.T) {
maxTTL := time.Duration(time.Second * 2) // time in seconds
startSize := 3 // initial number of items in map
pruneInterval := time.Duration(time.Second * 4) // search for expired items every 'pruneInterval' seconds
refreshLastAccessOnGet := true // update item's lastAccessTime on a .Get()
tm := New[string](maxTTL, startSize, pruneInterval, refreshLastAccessOnGet)
defer tm.Close()

key := "example"
tm.Put(key, "original")

tm.Put(key, "revised")
if tm.Get(key) != "revised" {
t.Errorf("The '%v' should equal 'revised', but actually equals: '%v'\n", key, tm.Get(key))
}

tm.Put(key, "revised-2")
if tm.Get(key) != "revised-2" {
t.Errorf("The '%v' should equal 'revised', but actually equals: '%v'\n", key, tm.Get(key))
}
}

0 comments on commit a0ed693

Please sign in to comment.