-
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
1 parent
2add68b
commit 80251de
Showing
3 changed files
with
96 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,93 @@ | ||
package golru | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"time" | ||
|
||
"github.com/juliaqiuxy/wfcache" | ||
"github.com/manucorporat/golru" | ||
) | ||
|
||
type GoLRUStorage struct { | ||
golru *golru.Cache | ||
ttl time.Duration | ||
} | ||
|
||
func Create(capacity int, ttl time.Duration) wfcache.StorageMaker { | ||
return CreateWithConfig(capacity, golru.DefaultLRUSamples, ttl) | ||
} | ||
|
||
func CreateWithConfig(capacity int, samples int, ttl time.Duration) wfcache.StorageMaker { | ||
return func() (wfcache.Storage, error) { | ||
golru := golru.New(capacity, golru.DefaultLRUSamples) | ||
|
||
s := &GoLRUStorage{ | ||
golru: golru, | ||
ttl: ttl, | ||
} | ||
|
||
return s, nil | ||
} | ||
} | ||
|
||
func (s *GoLRUStorage) TimeToLive() time.Duration { | ||
return s.ttl | ||
} | ||
|
||
func (s *GoLRUStorage) Get(ctx context.Context, key string) *wfcache.CacheItem { | ||
result := s.golru.Get(key) | ||
|
||
cacheItem := wfcache.CacheItem{} | ||
err := json.Unmarshal(result, &cacheItem) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
return &cacheItem | ||
} | ||
|
||
func (s *GoLRUStorage) BatchGet(ctx context.Context, keys []string) (results []*wfcache.CacheItem) { | ||
for _, key := range keys { | ||
m := s.Get(ctx, key) | ||
|
||
if m != nil { | ||
results = append(results, m) | ||
} | ||
} | ||
|
||
return results | ||
} | ||
|
||
func (s *GoLRUStorage) Set(ctx context.Context, key string, data []byte) error { | ||
item, err := json.Marshal(wfcache.CacheItem{ | ||
Key: key, | ||
Value: data, | ||
ExpiresAt: time.Now().UTC().Add(s.ttl).Unix(), | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
s.golru.Set(key, item) | ||
|
||
return nil | ||
} | ||
|
||
func (s *GoLRUStorage) BatchSet(ctx context.Context, pairs map[string][]byte) error { | ||
for key, data := range pairs { | ||
err := s.Set(ctx, key, data) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *GoLRUStorage) Del(ctx context.Context, key string) error { | ||
s.golru.Del(key) | ||
|
||
return nil | ||
} |