-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit ede93a2
Showing
18 changed files
with
807 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Lazhar Ichir | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,5 @@ | ||
test: | ||
go test -v ./... | ||
|
||
testrace: | ||
go test -race github.com/lazharichir/gocached -v -count=1 ./... |
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,119 @@ | ||
package gocached | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type Options struct { | ||
DefaultTTL *time.Duration | ||
} | ||
|
||
func WithDefaultTTL(ttl time.Duration) func(*Options) { | ||
return func(options *Options) { | ||
options.DefaultTTL = &ttl | ||
} | ||
} | ||
|
||
func NewCache[K comparable, V any]( | ||
evicter Evicter[K], | ||
optFns ...func(*Options), | ||
) *Cache[K, V] { | ||
if evicter == nil { | ||
evicter = NewNoEvicter[K]() | ||
} | ||
|
||
options := &Options{} | ||
for _, optFn := range optFns { | ||
optFn(options) | ||
} | ||
|
||
return &Cache[K, V]{ | ||
options: *options, | ||
data: make(map[K]Entry[K, V]), | ||
lock: sync.RWMutex{}, | ||
evicter: evicter, | ||
} | ||
} | ||
|
||
type Cache[K comparable, V any] struct { | ||
options Options | ||
evicter Evicter[K] | ||
data map[K]Entry[K, V] | ||
lock sync.RWMutex | ||
} | ||
|
||
func (cache *Cache[K, V]) Set(ctx context.Context, key K, value V, opts ...EntryFn) error { | ||
cache.lock.Lock() | ||
defer cache.lock.Unlock() | ||
return cache.set(ctx, key, value, opts...) | ||
} | ||
|
||
func (cache *Cache[K, V]) Get(ctx context.Context, key K) (V, bool, error) { | ||
cache.lock.RLock() | ||
defer cache.lock.RUnlock() | ||
return cache.get(ctx, key) | ||
} | ||
|
||
func (cache *Cache[K, V]) Del(ctx context.Context, key K) error { | ||
cache.lock.Lock() | ||
defer cache.lock.Unlock() | ||
return cache.del(ctx, key) | ||
} | ||
|
||
func (cache *Cache[K, V]) Has(ctx context.Context, key K) bool { | ||
cache.lock.RLock() | ||
defer cache.lock.RUnlock() | ||
return cache.has(ctx, key) | ||
} | ||
|
||
func (cache *Cache[K, V]) set(ctx context.Context, key K, value V, opts ...EntryFn) error { | ||
entry := Entry[K, V]{ | ||
Key: key, | ||
Value: value, | ||
Written: time.Now(), | ||
Options: makeEntryOptions(opts), | ||
} | ||
cache.data[key] = entry | ||
cache.evicter.Promote(ctx, key, 1) | ||
return nil | ||
} | ||
|
||
func (cache *Cache[K, V]) getEntry(ctx context.Context, key K) (Entry[K, V], bool) { | ||
if cache.has(ctx, key) { | ||
entry := cache.data[key] | ||
return entry, true | ||
} | ||
return Entry[K, V]{}, false | ||
} | ||
|
||
func (cache *Cache[K, V]) get(ctx context.Context, key K) (V, bool, error) { | ||
entry, ok := cache.getEntry(ctx, key) | ||
|
||
if !ok { | ||
return *new(V), false, nil | ||
} | ||
|
||
entryIsExplicitlyOutdated := entry.IsOutdated() | ||
entryIsExpiredDueToDefaultTTL := cache.options.DefaultTTL != nil && time.Now().Sub(entry.Written) > *cache.options.DefaultTTL | ||
|
||
if entryIsExplicitlyOutdated || entryIsExpiredDueToDefaultTTL { | ||
cache.del(ctx, key) | ||
return *new(V), false, nil | ||
} | ||
|
||
return entry.Value, true, nil | ||
} | ||
|
||
func (cache *Cache[K, V]) del(ctx context.Context, key K) error { | ||
delete(cache.data, key) | ||
cache.evicter.Evict(ctx, key) | ||
return nil | ||
} | ||
|
||
func (cache *Cache[K, V]) has(ctx context.Context, key K) bool { | ||
_, ok := cache.data[key] | ||
cache.evicter.Promote(ctx, key, 1) | ||
return ok | ||
} |
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,114 @@ | ||
package gocached_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/lazharichir/gocached" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCache(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
// Initialize cache | ||
c := gocached.NewCache[string, string](nil) | ||
|
||
// Test Set and Get | ||
c.Set(ctx, "key1", "value1") | ||
c.Set(ctx, "key2", "value2") | ||
|
||
v1, found, err := c.Get(ctx, "key1") | ||
assert.NoError(t, err) | ||
assert.True(t, found) | ||
assert.Equal(t, "value1", v1) | ||
|
||
v2, found, err := c.Get(ctx, "key2") | ||
assert.NoError(t, err) | ||
assert.True(t, found) | ||
assert.Equal(t, "value2", v2) | ||
|
||
// Test Get with non-existing key | ||
v3, found, err := c.Get(ctx, "key3") | ||
assert.NoError(t, err) | ||
assert.False(t, found) | ||
assert.Equal(t, *new(string), v3) | ||
|
||
// Test Has | ||
assert.True(t, c.Has(ctx, "key1")) | ||
assert.False(t, c.Has(ctx, "key3")) | ||
|
||
// Test Del | ||
c.Del(ctx, "key1") | ||
assert.False(t, c.Has(ctx, "key1")) | ||
} | ||
|
||
func TestDefaultTTL(t *testing.T) { | ||
ctx := context.Background() | ||
ttl := 250 * time.Millisecond | ||
cache := gocached.NewCache[string, int](nil, gocached.WithDefaultTTL(ttl)) | ||
cache.Set(ctx, "1", 1) | ||
|
||
v1, ok, _ := cache.Get(ctx, "1") | ||
assert.True(t, ok, "key 1 should be present") | ||
assert.Equal(t, 1, v1, "key 1 should be present") | ||
|
||
time.Sleep(ttl + (20 * time.Millisecond)) | ||
|
||
v1, ok, _ = cache.Get(ctx, "1") | ||
assert.False(t, ok, "key 1 should have expired") | ||
assert.Equal(t, 0, v1, "key 1 should have expired") | ||
} | ||
|
||
func TestEntryWithTTL(t *testing.T) { | ||
ttl := 100 * time.Millisecond | ||
ctx := context.Background() | ||
cache := gocached.NewCache[string, int](nil) | ||
cache.Set(ctx, "1", 1) | ||
cache.Set(ctx, "2", 2, gocached.WithTTL(ttl)) | ||
|
||
v1, ok, _ := cache.Get(ctx, "1") | ||
assert.True(t, ok, "key 1 should be present") | ||
assert.Equal(t, 1, v1, "key 1 should be present") | ||
|
||
v2, ok, _ := cache.Get(ctx, "2") | ||
assert.True(t, ok, "key 2 should be present") | ||
assert.Equal(t, 2, v2, "key 2 should be present") | ||
|
||
time.Sleep(ttl + (20 * time.Millisecond)) | ||
|
||
v1, ok, _ = cache.Get(ctx, "1") | ||
assert.True(t, ok, "key 1 should still be present") | ||
assert.Equal(t, 1, v1, "key 1 should still be present") | ||
|
||
v2, ok, _ = cache.Get(ctx, "2") | ||
assert.False(t, ok, "key 2 should have expired") | ||
assert.Equal(t, 0, v2, "key 2 should have expired") | ||
} | ||
|
||
func TestEntryWithExpiryDate(t *testing.T) { | ||
ctx := context.Background() | ||
expiryDate := time.Now().Add(100 * time.Millisecond) | ||
cache := gocached.NewCache[string, int](nil) | ||
cache.Set(ctx, "1", 1) | ||
cache.Set(ctx, "2", 2, gocached.WithExpiryDate(expiryDate)) | ||
|
||
v1, ok, _ := cache.Get(ctx, "1") | ||
assert.True(t, ok, "key 1 should be present") | ||
assert.Equal(t, 1, v1, "key 1 should be present") | ||
|
||
v2, ok, _ := cache.Get(ctx, "2") | ||
assert.True(t, ok, "key 2 should be present") | ||
assert.Equal(t, 2, v2, "key 2 should be present") | ||
|
||
time.Sleep(100 * time.Millisecond) | ||
|
||
v1, ok, _ = cache.Get(ctx, "1") | ||
assert.True(t, ok, "key 1 should still be present") | ||
assert.Equal(t, 1, v1, "key 1 should still be present") | ||
|
||
v2, ok, _ = cache.Get(ctx, "2") | ||
assert.False(t, ok, "key 2 should have expired") | ||
assert.Equal(t, 0, v2, "key 2 should have expired") | ||
} |
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,12 @@ | ||
package gocached | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type Cacher[K comparable, V any] interface { | ||
Set(ctx context.Context, key K, value V, opts ...EntryFn) error | ||
Get(ctx context.Context, key K) (V, bool, error) | ||
Has(ctx context.Context, key K) bool | ||
Del(ctx context.Context, key K) error | ||
} |
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,52 @@ | ||
package gocached | ||
|
||
import "time" | ||
|
||
type Entry[K comparable, V any] struct { | ||
Key K | ||
Value V | ||
Written time.Time | ||
Options EntryOptions | ||
} | ||
|
||
func (entry *Entry[K, V]) IsOutdated() bool { | ||
if entry.IsPastExpiryDate() || entry.IsPastTTL() { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (entry *Entry[K, V]) IsPastExpiryDate() bool { | ||
return entry.Options.ExpiryDate != nil && time.Now().After(*entry.Options.ExpiryDate) | ||
} | ||
|
||
func (entry *Entry[K, V]) IsPastTTL() bool { | ||
return entry.Options.TTL != nil && time.Now().Sub(entry.Written) > *entry.Options.TTL | ||
} | ||
|
||
type EntryOptions struct { | ||
ExpiryDate *time.Time | ||
TTL *time.Duration | ||
} | ||
|
||
type EntryFn func(*EntryOptions) | ||
|
||
func makeEntryOptions(opts []EntryFn) EntryOptions { | ||
options := EntryOptions{} | ||
for _, optFn := range opts { | ||
optFn(&options) | ||
} | ||
return options | ||
} | ||
|
||
func WithTTL(ttl time.Duration) EntryFn { | ||
return func(opts *EntryOptions) { | ||
opts.TTL = &ttl | ||
} | ||
} | ||
|
||
func WithExpiryDate(expiryDate time.Time) EntryFn { | ||
return func(opts *EntryOptions) { | ||
opts.ExpiryDate = &expiryDate | ||
} | ||
} |
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,46 @@ | ||
package gocached_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/lazharichir/gocached" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEntry(t *testing.T) { | ||
expiryDate := time.Now().Add(1 * time.Hour) | ||
ttl := 10 * time.Minute | ||
entry := &gocached.Entry[string, int]{ | ||
Key: "foo", | ||
Value: 42, | ||
Written: time.Now(), | ||
Options: gocached.EntryOptions{ | ||
ExpiryDate: &expiryDate, | ||
TTL: &ttl, | ||
}, | ||
} | ||
|
||
// Test IsOutdated method | ||
assert.False(t, entry.IsOutdated(), "Expected entry to not be outdated") | ||
|
||
// Test isPastExpiryDate method | ||
assert.False(t, entry.IsPastExpiryDate(), "Expected entry to not be past expiry date") | ||
|
||
// Test isPastTTL method | ||
assert.False(t, entry.IsPastTTL(), "Expected entry to not be past TTL") | ||
} | ||
|
||
func TestEntryOptions(t *testing.T) { | ||
expiryDate := time.Now().Add(1 * time.Hour) | ||
ttl := 10 * time.Minute | ||
|
||
// Test EntryOptions | ||
options := gocached.EntryOptions{ | ||
ExpiryDate: &expiryDate, | ||
TTL: &ttl, | ||
} | ||
|
||
assert.Equal(t, expiryDate, *options.ExpiryDate, "Expected expiry date to be %v, got %v", expiryDate, *options.ExpiryDate) | ||
assert.Equal(t, ttl, *options.TTL, "Expected TTL to be %v, got %v", ttl, *options.TTL) | ||
} |
Oops, something went wrong.