Git Product home page Git Product logo

gocache's Introduction

GoCache

a simple distributed cache

Features

  • 实现LRU缓存淘汰算法,并对数据进行冷热分区,提高缓存命中率
  • 实现一致性哈希环,提高了节点的均匀性和可拓展性
  • 多节点间基于HTTP, 使用protobuf进行通信
  • 定时心跳检测,及时移除不可用的节点
  • 使用singleflight合并高并发的相同请求,防止缓存击穿

Getting GoCache

docker pull erfeiyu/go-cache:latest

docker run -e PORT=8002 -p 8002:8002 erfeiyu/go-cache:latest  # run a cache node in http://0.0.0.0:8002

Usage

Single node

package main

import (
	gcache "github.com/454270186/GoCache/api"
)

func main() {
	g := gcache.NewGoCache()
	g.Put("xiaofei", "100")
	val, _ := g.Get("xiaofei") // val ==> 100
}

You can also store the data in specific group

package main

import (
	gcache "github.com/454270186/GoCache/api"
)

func main() {
	g := gcache.NewGoCache()

	g.NewGroup("people", 20)
	g.PutWithGroup("people", "xiaofei", "1")
	g.NewGroup("student", 20)
	g.PutWithGroup("student", "xiaofei", "100")

	val1, _ := g.GetWithGroup("people", "xiaofei")  // val1 ==> 1
	val2, _ := g.GetWithGroup("student", "xiaofei") // val2 ==> 100
}

PS: If dont specify a group, the data will store in "base" group

Multi nodes

package main

import (
	"fmt"

	gcache "github.com/454270186/GoCache/api"
)

func main() {
	g := gcache.NewGoCache(
		"http://0.0.0.0:8002",
		"http://0.0.0.0:8003",
	)
	g.Put("xiaofei", "123")
	g.Put("dafei", "456")
	val, _ := g.Get("xiaofei") // val ==> 123
}

Output

PeerPicker works

2023/09/24 11:43:49 [Server ] Pick peer http://0.0.0.0:8002
2023/09/24 11:43:49 [Put] Put <xiaofei -- 123>
2023/09/24 11:43:49 [Server ] Pick peer http://0.0.0.0:8003
2023/09/24 11:43:49 [Put] Put <dafei -- 456>
2023/09/24 11:43:49 [Server ] Pick peer http://0.0.0.0:8002
123

gocache's People

Contributors

454270186 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.