/hit

分布式缓存系统

Primary LanguageGoApache License 2.0Apache-2.0

分布式缓存

1. 缓存算法

默认使用了LRU(最近最久未使用)缓存算法

2. 使用

2.1 服务端编译

方法一:自动编译未可执行文件,生成的文件在$GOPATH/bin中,确保将$GOPATH/bin其添加到您的中$PATH

go get -u github.com/chenquan/hit/cmd/hit

方法二:手动编译

git clone http://github.com/chenquan/hit # 或 git@github.com:chenquan/hit.git
cd hit/cmd/hit
go build -o hit # windows:go build -o hit.exe

查看帮助:

hit -h # windows:hit.exe -h
Usage of hit.exe:
  -path string
        配置文件地址 (default "hit.toml")

2.2 部署

配置:hit.toml

Endpoints=["localhost:2379"]
LeaseTtl=10
DialTimeout=5
NodeAddr="localhost"
NodeName="node1"
Protocol="http"
Port="2020"

单机单例:

hit

运行成功:

2020-09-19 22:48:20.066321 I | 注册 name: hit/node1 addr: http://localhost:2020
2020-09-19 22:48:20.690561 I | 续租成功节点:node1.

单机多例:

hit -path=test/hit-1.toml
hit -path=test/hit-2.toml
hit -path=test/hit-3.toml

2.3 客户端

config := &hit.Config{
		Endpoints: []string{"localhost:2379"},
		Replicas:  3,
	}
hitClient = NewHit(config)
var f GetterFunc = func(string2 string) ([]byte, error) {

		return []byte("not found"), nil
	}

groupDefault := hitClient.NewGroupDefault("node1", "", 1000, f)
rand.Seed(time.Now().Unix())
_, _ = groupDefault.Set("chenquan"+index, lru.NewValue([]byte("data"), time.Now().Add(time.Minute).Unix(), "test"+strconv.Itoa(rand.Int())), true)
_, _ = groupDefault.Get("chenquan" + index)