-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package store | ||
|
||
import ( | ||
"net/http" | ||
"sync" | ||
|
||
"github.com/golang/groupcache/lru" | ||
) | ||
|
||
// LRU implements a fixed-size thread safe LRU cache. | ||
// It is based on the LRU cache in Groupcache. | ||
type LRU struct { | ||
Cache *lru.Cache | ||
MU *sync.Mutex | ||
} | ||
|
||
// Load returns the value stored in the Cache for a key, or nil if no value is present. | ||
// The ok result indicates whether value was found in the Cache. | ||
func (l *LRU) Load(key string, _ *http.Request) (interface{}, bool, error) { | ||
l.MU.Lock() | ||
defer l.MU.Unlock() | ||
v, ok := l.Cache.Get(key) | ||
return v, ok, nil | ||
} | ||
|
||
// Store sets the value for a key. | ||
func (l *LRU) Store(key string, value interface{}, _ *http.Request) error { | ||
l.MU.Lock() | ||
defer l.MU.Unlock() | ||
l.Cache.Add(key, value) | ||
return nil | ||
} | ||
|
||
// Delete the value for a key. | ||
func (l *LRU) Delete(key string, r *http.Request) error { | ||
l.MU.Lock() | ||
defer l.MU.Unlock() | ||
l.Cache.Remove(key) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package store | ||
|
||
import ( | ||
"net/http" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/golang/groupcache/lru" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLRU(t *testing.T) { | ||
table := []struct { | ||
name string | ||
key string | ||
value interface{} | ||
op string | ||
expectedErr bool | ||
found bool | ||
}{ | ||
{ | ||
name: "it return false when key does not exist", | ||
op: "load", | ||
key: "key", | ||
found: false, | ||
}, | ||
{ | ||
name: "it return true and value when exist", | ||
op: "load", | ||
key: "test", | ||
value: "test", | ||
found: true, | ||
}, | ||
{ | ||
name: "it overwrite exist key and value when store", | ||
op: "store", | ||
key: "test", | ||
value: "test2", | ||
found: true, | ||
}, | ||
{ | ||
name: "it create new record when store", | ||
op: "store", | ||
key: "key", | ||
value: "value", | ||
found: true, | ||
}, | ||
{ | ||
name: "it's not crash when trying to delete a non exist record", | ||
key: "key", | ||
found: false, | ||
}, | ||
{ | ||
name: "it delete a exist record", | ||
op: "delete", | ||
key: "test", | ||
found: false, | ||
}, | ||
} | ||
|
||
for _, tt := range table { | ||
t.Run(tt.name, func(t *testing.T) { | ||
cache := &LRU{ | ||
Cache: lru.New(2), | ||
MU: &sync.Mutex{}, | ||
} | ||
|
||
cache.Cache.Add("test", "test") | ||
|
||
r, _ := http.NewRequest("GET", "/", nil) | ||
var err error | ||
|
||
switch tt.op { | ||
case "load": | ||
v, ok, err := cache.Load(tt.key, r) | ||
assert.Equal(t, tt.value, v) | ||
assert.Equal(t, tt.found, ok) | ||
assert.NoError(t, err) | ||
return | ||
case "store": | ||
err = cache.Store(tt.key, tt.value, r) | ||
case "delete": | ||
err = cache.Delete(tt.key, r) | ||
} | ||
|
||
v, ok := cache.Cache.Get(tt.key) | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.found, ok) | ||
assert.Equal(t, tt.value, v) | ||
|
||
}) | ||
} | ||
|
||
} |