2 Commits

Author SHA1 Message Date
73882dae8d Add go sum. 2022-02-24 22:31:36 -08:00
c200f51afd bug fixes 2022-02-24 21:50:54 -08:00
6 changed files with 17 additions and 26 deletions

4
go.mod
View File

@@ -1,5 +1,5 @@
module git.wntrmute.dev/kyle/go-mru
go 1.22
go 1.17
require github.com/benbjohnson/clock v1.3.5
require github.com/benbjohnson/clock v1.3.0

4
go.sum
View File

@@ -1,2 +1,2 @@
github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o=
github.com/benbjohnson/clock v1.3.5/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=

16
mru.go
View File

@@ -10,7 +10,7 @@ import (
)
type item struct {
V any
V interface{}
access int64
}
@@ -28,11 +28,11 @@ type Cache struct {
}
// New must be used to create a new Cache.
func New(icap int) *Cache {
func New(cap int) *Cache {
return &Cache{
store: map[string]*item{},
access: newTimestamps(icap),
cap: icap,
access: newTimestamps(cap),
cap: cap,
clock: clock.New(),
mtx: &sync.Mutex{},
}
@@ -53,10 +53,6 @@ func (c *Cache) Len() int {
// evict should remove the least-recently-used cache item.
func (c *Cache) evict() {
if c.access.Len() == 0 {
return
}
k := c.access.K(0)
c.evictKey(k)
}
@@ -114,7 +110,7 @@ func (c *Cache) ConsistencyCheck() error {
}
// Store adds the value v to the cache under the k.
func (c *Cache) Store(k string, v any) {
func (c *Cache) Store(k string, v interface{}) {
c.lock()
defer c.unlock()
@@ -139,7 +135,7 @@ func (c *Cache) Store(k string, v any) {
// Get returns the value stored in the cache. If the item isn't present,
// it will return false.
func (c *Cache) Get(k string) (any, bool) {
func (c *Cache) Get(k string) (interface{}, bool) {
c.lock()
defer c.unlock()

View File

@@ -20,11 +20,6 @@ func TestBasicCacheEviction(t *testing.T) {
t.Fatal("cache should have size 0")
}
c.evict()
if err := c.ConsistencyCheck(); err != nil {
t.Fatal(err)
}
c.Store("raven", 1)
if err := c.ConsistencyCheck(); err != nil {
t.Fatal(err)

View File

@@ -3,7 +3,6 @@ package mru
import (
"errors"
"fmt"
"io"
"sort"
)
@@ -20,10 +19,10 @@ type timestamps struct {
cap int
}
func newTimestamps(icap int) *timestamps {
func newTimestamps(cap int) *timestamps {
return &timestamps{
ts: make([]timestamp, 0, icap),
cap: icap,
ts: make([]timestamp, 0, cap),
cap: cap,
}
}
@@ -48,7 +47,7 @@ func (ts *timestamps) Swap(i, j int) {
}
func (ts *timestamps) Find(k string) (int, bool) {
for i := range len(ts.ts) {
for i := 0; i < len(ts.ts); i++ {
if ts.ts[i].k == k {
return i, true
}
@@ -94,8 +93,8 @@ func (ts *timestamps) Delete(i int) {
ts.ts = append(ts.ts[:i], ts.ts[i+1:]...)
}
func (ts *timestamps) Dump(w io.Writer) {
func (ts *timestamps) Dump() {
for i := range ts.ts {
fmt.Fprintf(w, "%d: %s, %d\n", i, ts.K(i), ts.T(i))
fmt.Printf("%d: %s, %d\n", i, ts.K(i), ts.T(i))
}
}

View File

@@ -46,4 +46,5 @@ func TestTimestamps(t *testing.T) {
if ts.K(2) != "owl" {
t.Fatalf("third key should be owl, have %s", ts.K(2))
}
}