forked from micro/go-micro
-
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.
Merge pull request micro#1656 from micro/client-cache
Client Cache
- Loading branch information
Showing
18 changed files
with
1,405 additions
and
1,130 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 was deleted.
Oops, something went wrong.
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,66 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"hash/fnv" | ||
"time" | ||
|
||
"github.com/micro/go-micro/v2/metadata" | ||
cache "github.com/patrickmn/go-cache" | ||
) | ||
|
||
// NewCache returns an initialised cache. | ||
func NewCache() *Cache { | ||
return &Cache{ | ||
cache: cache.New(cache.NoExpiration, 30*time.Second), | ||
} | ||
} | ||
|
||
// Cache for responses | ||
type Cache struct { | ||
cache *cache.Cache | ||
} | ||
|
||
// Get a response from the cache | ||
func (c *Cache) Get(ctx context.Context, req *Request) (interface{}, bool) { | ||
return c.cache.Get(key(ctx, req)) | ||
} | ||
|
||
// Set a response in the cache | ||
func (c *Cache) Set(ctx context.Context, req *Request, rsp interface{}, expiry time.Duration) { | ||
c.cache.Set(key(ctx, req), rsp, expiry) | ||
} | ||
|
||
// List the key value pairs in the cache | ||
func (c *Cache) List() map[string]string { | ||
items := c.cache.Items() | ||
|
||
rsp := make(map[string]string, len(items)) | ||
for k, v := range items { | ||
bytes, _ := json.Marshal(v.Object) | ||
rsp[k] = string(bytes) | ||
} | ||
|
||
return rsp | ||
} | ||
|
||
// key returns a hash for the context and request | ||
func key(ctx context.Context, req *Request) string { | ||
md, _ := metadata.FromContext(ctx) | ||
|
||
bytes, _ := json.Marshal(map[string]interface{}{ | ||
"metadata": md, | ||
"request": map[string]interface{}{ | ||
"service": (*req).Service(), | ||
"endpoint": (*req).Endpoint(), | ||
"method": (*req).Method(), | ||
"body": (*req).Body(), | ||
}, | ||
}) | ||
|
||
h := fnv.New64() | ||
h.Write(bytes) | ||
return fmt.Sprintf("%x", h.Sum(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,76 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/micro/go-micro/v2/metadata" | ||
) | ||
|
||
func TestCache(t *testing.T) { | ||
ctx := context.TODO() | ||
req := NewRequest("go.micro.service.foo", "Foo.Bar", nil) | ||
|
||
t.Run("CacheMiss", func(t *testing.T) { | ||
if _, ok := NewCache().Get(ctx, &req); ok { | ||
t.Errorf("Expected to get no result from Get") | ||
} | ||
}) | ||
|
||
t.Run("CacheHit", func(t *testing.T) { | ||
c := NewCache() | ||
|
||
rsp := "theresponse" | ||
c.Set(ctx, &req, rsp, time.Minute) | ||
|
||
if res, ok := c.Get(ctx, &req); !ok { | ||
t.Errorf("Expected a result, got nothing") | ||
} else if res != rsp { | ||
t.Errorf("Expected '%v' result, got '%v'", rsp, res) | ||
} | ||
}) | ||
} | ||
|
||
func TestCacheKey(t *testing.T) { | ||
ctx := context.TODO() | ||
req1 := NewRequest("go.micro.service.foo", "Foo.Bar", nil) | ||
req2 := NewRequest("go.micro.service.foo", "Foo.Baz", nil) | ||
req3 := NewRequest("go.micro.service.foo", "Foo.Baz", "customquery") | ||
|
||
t.Run("IdenticalRequests", func(t *testing.T) { | ||
key1 := key(ctx, &req1) | ||
key2 := key(ctx, &req1) | ||
if key1 != key2 { | ||
t.Errorf("Expected the keys to match for identical requests and context") | ||
} | ||
}) | ||
|
||
t.Run("DifferentRequestEndpoints", func(t *testing.T) { | ||
key1 := key(ctx, &req1) | ||
key2 := key(ctx, &req2) | ||
|
||
if key1 == key2 { | ||
t.Errorf("Expected the keys to differ for different request endpoints") | ||
} | ||
}) | ||
|
||
t.Run("DifferentRequestBody", func(t *testing.T) { | ||
key1 := key(ctx, &req2) | ||
key2 := key(ctx, &req3) | ||
|
||
if key1 == key2 { | ||
t.Errorf("Expected the keys to differ for different request bodies") | ||
} | ||
}) | ||
|
||
t.Run("DifferentMetadata", func(t *testing.T) { | ||
mdCtx := metadata.Set(context.TODO(), "foo", "bar") | ||
key1 := key(mdCtx, &req1) | ||
key2 := key(ctx, &req1) | ||
|
||
if key1 == key2 { | ||
t.Errorf("Expected the keys to differ for different metadata") | ||
} | ||
}) | ||
} |
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
Oops, something went wrong.