From ba5ce58640c0c94a4a056b430967e88ede74a433 Mon Sep 17 00:00:00 2001 From: Petrichor <390983386@qq.com> Date: Tue, 5 Apr 2022 16:53:30 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A6=84=20refactor:=20=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 9 +++++++ geecache/byteview.go => byteview.go | 0 geecache/cache.go => cache.go | 2 +- consistenthash/consistenthash.go | 24 +++++++++++++++++++ geecache/geecache.go => geecache.go | 0 geecache/geecache_test.go => geecache_test.go | 0 go.mod | 6 +---- geecache/http.go => http.go | 0 {geecache/lru => lru}/lru.go | 0 {geecache/lru => lru}/lru_test.go | 0 main.go => main/main.go | 2 +- 11 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 README.md rename geecache/byteview.go => byteview.go (100%) rename geecache/cache.go => cache.go (96%) create mode 100644 consistenthash/consistenthash.go rename geecache/geecache.go => geecache.go (100%) rename geecache/geecache_test.go => geecache_test.go (100%) rename geecache/http.go => http.go (100%) rename {geecache/lru => lru}/lru.go (100%) rename {geecache/lru => lru}/lru_test.go (100%) rename main.go => main/main.go (97%) diff --git a/README.md b/README.md new file mode 100644 index 0000000..4d32d04 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +## 分布式缓存 + +#### 对于给定的 key,每次都选择同一个节点 + +节点固定时: + +把 key 的每一个字符的 ASCII 码加起来,除以 10 取余数 + +hash('Tom') % 10 \ No newline at end of file diff --git a/geecache/byteview.go b/byteview.go similarity index 100% rename from geecache/byteview.go rename to byteview.go diff --git a/geecache/cache.go b/cache.go similarity index 96% rename from geecache/cache.go rename to cache.go index 1ac12e7..5952808 100644 --- a/geecache/cache.go +++ b/cache.go @@ -1,7 +1,7 @@ package geecache import ( - "gee/geecache/lru" + "geecache/lru" "sync" ) diff --git a/consistenthash/consistenthash.go b/consistenthash/consistenthash.go new file mode 100644 index 0000000..65c7e87 --- /dev/null +++ b/consistenthash/consistenthash.go @@ -0,0 +1,24 @@ +package consistenthash + +import "hash/crc32" + +type Hash func(data []byte) uint32 + +type Map struct { + hash Hash + replicas int //虚拟节点倍数 + keys []int // 哈希环 + hashMap map[int]string // 虚拟节点与真实节点的映射表,键是哈希值,值是真实节点的名称 +} + +func New(replicas int, fn Hash) *Map { + m := &Map{ + replicas: replicas, + hash: fn, + hashMap: make(map[int]string), + } + if m.hash == nil { + m.hash = crc32.ChecksumIEEE + } + return m +} diff --git a/geecache/geecache.go b/geecache.go similarity index 100% rename from geecache/geecache.go rename to geecache.go diff --git a/geecache/geecache_test.go b/geecache_test.go similarity index 100% rename from geecache/geecache_test.go rename to geecache_test.go diff --git a/go.mod b/go.mod index f471a68..1f7c52b 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,3 @@ -module gee +module geecache go 1.18 - -replace geecache => ./geecache - -// require geecache v0.0.0-00010101000000-000000000000 // indirect diff --git a/geecache/http.go b/http.go similarity index 100% rename from geecache/http.go rename to http.go diff --git a/geecache/lru/lru.go b/lru/lru.go similarity index 100% rename from geecache/lru/lru.go rename to lru/lru.go diff --git a/geecache/lru/lru_test.go b/lru/lru_test.go similarity index 100% rename from geecache/lru/lru_test.go rename to lru/lru_test.go diff --git a/main.go b/main/main.go similarity index 97% rename from main.go rename to main/main.go index 77f4009..4c246ec 100644 --- a/main.go +++ b/main/main.go @@ -2,7 +2,7 @@ package main import ( "fmt" - "gee/geecache" + "geecache" "log" "net/http" )