-
Notifications
You must be signed in to change notification settings - Fork 9
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
2c0117b
commit 7e8b84c
Showing
6 changed files
with
175 additions
and
15 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,51 @@ | ||
// Copyright 2021 Peanutzhen. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package peanutcache | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
// client 模块实现peanutcache访问其他远程节点 | ||
// 从而获取缓存的能力 | ||
|
||
type Client struct { | ||
reqURL string | ||
} | ||
|
||
// Fetch 从remote peer获取对应缓存值 | ||
func (c *Client) Fetch(group string, key string) ([]byte, error) { | ||
// 构造请求url | ||
u := fmt.Sprintf( | ||
"%s%s/%s", | ||
c.reqURL, | ||
url.QueryEscape(group), | ||
url.QueryEscape(key), | ||
) | ||
|
||
resp, err := http.Get(u) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("peer Statuscode: %d", resp.StatusCode) | ||
} | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("read response body failed, %v", err) | ||
} | ||
return body, nil | ||
} | ||
|
||
func NewClient(reqURL string) *Client { | ||
return &Client{reqURL: reqURL} | ||
} | ||
|
||
var _ Fetcher = (*Client)(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
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,18 @@ | ||
// Copyright 2021 Peanutzhen. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package peanutcache | ||
|
||
// peers 模块 | ||
|
||
// Picker 定义了获取分布式节点的能力 | ||
type Picker interface { | ||
Pick(key string) (Fetcher, bool) | ||
} | ||
|
||
// Fetcher 定义了从远端获取缓存的能力 | ||
// 所以每个Peer应实现这个接口 | ||
type Fetcher interface { | ||
Fetch(group string, key string) ([]byte, 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
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