Compare commits
3 Commits
e755e846c1
...
485a6031ae
Author | SHA1 | Date | |
---|---|---|---|
485a6031ae
|
|||
a1e778afe3
|
|||
991e24c1a4
|
@ -12,7 +12,7 @@ type LRU[T any] struct {
|
||||
}
|
||||
type item[T any] struct {
|
||||
key string
|
||||
value *T
|
||||
value T
|
||||
eol time.Time
|
||||
}
|
||||
|
||||
@ -30,7 +30,7 @@ func (lru *LRU[T]) Set(key string, value T, ttl time.Duration) (evicted bool) {
|
||||
if element, ok := lru.items[key]; ok {
|
||||
lru.queue.MoveToFront(element)
|
||||
item := element.Value.(*item[T])
|
||||
item.value = &value
|
||||
item.value = value
|
||||
item.eol = eol
|
||||
|
||||
return
|
||||
@ -43,7 +43,7 @@ func (lru *LRU[T]) Set(key string, value T, ttl time.Duration) (evicted bool) {
|
||||
|
||||
item := &item[T]{
|
||||
key: key,
|
||||
value: &value,
|
||||
value: value,
|
||||
eol: eol,
|
||||
}
|
||||
|
||||
@ -68,7 +68,7 @@ func (lru *LRU[T]) Get(name string) (value T, ok bool) {
|
||||
|
||||
lru.queue.MoveToFront(element)
|
||||
|
||||
return *item.value, true
|
||||
return item.value, true
|
||||
}
|
||||
|
||||
func (lru *LRU[T]) evict() {
|
||||
|
@ -1,6 +1,7 @@
|
||||
package lru
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
"time"
|
||||
@ -76,3 +77,24 @@ func TestDummy(t *testing.T) { // for coverage 100% coverage
|
||||
cache := New[int](0)
|
||||
cache.evict()
|
||||
}
|
||||
|
||||
// We will be storing many short strings as the key and value
|
||||
func Benchmark(b *testing.B) {
|
||||
cache := New[string](10)
|
||||
ttl := 1000 * time.Millisecond
|
||||
|
||||
b.Run("Set", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
cache.Set(fmt.Sprintf("item:%d", i), fmt.Sprintf("item:%d", i), ttl)
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("Get", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
value, ok := cache.Get(fmt.Sprintf("item:%d", i))
|
||||
if ok {
|
||||
_ = value
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -1,9 +1,12 @@
|
||||
package uniqueschars
|
||||
|
||||
import "unicode"
|
||||
import (
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func Unique(s string) bool {
|
||||
m := make(map[rune]struct{}, 50)
|
||||
m := make(map[rune]struct{}, utf8.RuneCountInString(s)/2)
|
||||
|
||||
for _, c := range s {
|
||||
c := unicode.ToLower(c)
|
||||
|
Reference in New Issue
Block a user