-
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
Showing
9 changed files
with
106 additions
and
8 deletions.
There are no files selected for viewing
File renamed without changes.
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package geecache | ||
|
||
import ( | ||
"geecache/lru" | ||
"gee/geecache/lru" | ||
"sync" | ||
) | ||
|
||
|
File renamed without changes.
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,63 @@ | ||
package geecache | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
const defaultBasePath = "/_geecache/" | ||
|
||
// HTTPPool implements PeerPicker for a pool of HTTP peers. | ||
type HTTPPool struct { | ||
self string // 记录自己的地址,包括主机名/ IP 和端口 | ||
basePath string // 节点间通讯地址的前缀 | ||
} | ||
|
||
// NewHTTPPool initializes an HTTP pool of peers. | ||
func NewHTTPPool(self string) *HTTPPool { | ||
return &HTTPPool{ | ||
self: self, | ||
basePath: defaultBasePath, | ||
} | ||
} | ||
|
||
// Log info with server name | ||
func (p *HTTPPool) Log(format string, v ...interface{}) { | ||
log.Printf("[Server %s] %s", p.self, fmt.Sprintf(format, v...)) | ||
} | ||
|
||
func (p *HTTPPool) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
if !strings.HasPrefix(r.URL.Path, p.basePath) { | ||
panic("HTTPPool serving unexpected path: " + r.URL.Path) | ||
} | ||
p.Log("%s %s", r.Method, r.URL.Path) | ||
// /<basepath>/<groupname>/<key> required | ||
// GET /_geecache/scores/Tom | ||
parts := strings.SplitN(r.URL.Path[len(p.basePath):], "/", 2) | ||
// log.Println(parts) // get [scores Tom] | ||
if len(parts) != 2 { | ||
http.Error(w, "bad request", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
groupName := parts[0] | ||
key := parts[1] | ||
|
||
group := GetGroup(groupName) | ||
if group == nil { | ||
http.Error(w, "no such group: "+groupName, http.StatusNotFound) | ||
return | ||
} | ||
|
||
view, err := group.Get(key) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/octet-stream") | ||
w.Write(view.ByteSlice()) | ||
// log.Println(view.ByteSlice()) | ||
} |
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
module geecache | ||
module gee | ||
|
||
go 1.18 | ||
|
||
replace geecache => ./geecache | ||
|
||
// require geecache v0.0.0-00010101000000-000000000000 // indirect |
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,31 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"gee/geecache" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
var db = map[string]string{ | ||
"Tom": "630", | ||
"Jack": "589", | ||
"Sam": "567", | ||
} | ||
|
||
func main() { | ||
geecache.NewGroup("scores", 2<<10, geecache.GetterFunc( | ||
func(key string) ([]byte, error) { | ||
log.Println("[SlowDB] search key", key) | ||
if v, ok := db[key]; ok { | ||
return []byte(v), nil | ||
} | ||
return nil, fmt.Errorf("%s not exist", key) | ||
}, | ||
)) | ||
|
||
addr := "localhost:9999" | ||
peers := geecache.NewHTTPPool(addr) | ||
log.Println("geecache is running at", addr) | ||
log.Fatal(http.ListenAndServe(addr, peers)) | ||
} |