fexcache

package module
v0.0.0-...-474052a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2024 License: MIT Imports: 12 Imported by: 0

README

分布式缓存 - FexCache

参考 7 days golang programs from scratch

特性

  • 缓存淘汰策略:LRU
  • 并发访问:互斥锁
  • 分布式缓存:基于 HTTP 协议的客户端/服务端通信;基于一致性哈希算法选择节点
  • 防止缓存击穿:实现 Singleflight 合并并发请求
  • Probobuf 序列化:(可有可无的功能)
                            是
接收 key --> 检查是否被缓存 -----> 返回缓存值 ⑴
                |  否                         是
                |-----> 是否应当从远程节点获取 -----> 与远程节点交互 --> 返回缓存值 ⑵
                            |  否
                            |-----> 调用`回调函数`,获取值并添加到缓存 --> 返回缓存值 ⑶

安装 Protobuf

On MacOS

brew install protobuf

go install github.com/golang/protobuf/protoc-gen-go

$GOPATH/bin 加入环境变量中

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ByteView

type ByteView struct {
	// contains filtered or unexported fields
}

func (ByteView) ByteSlice

func (v ByteView) ByteSlice() []byte

func (ByteView) Len

func (v ByteView) Len() int

func (ByteView) String

func (v ByteView) String() string

type Getter

type Getter interface {
	Get(string) ([]byte, error)
}

Getter 是缓存不存在时的回调接口。函数类型或结构体类型都可以实现该接口

type GetterFunc

type GetterFunc func(key string) ([]byte, error)

GetterFunc 是接口型函数

func (GetterFunc) Get

func (f GetterFunc) Get(key string) ([]byte, error)

type Group

type Group struct {
	// contains filtered or unexported fields
}

Group 是核心结构。负责与外部交互

func GetGroup

func GetGroup(name string) *Group

func NewGroup

func NewGroup(name string, cacheBytes int64, getter Getter) *Group

func (*Group) Get

func (g *Group) Get(key string) (ByteView, error)

Get 获取数据的主要逻辑 1. 本节点查询 2. 远程节点查询 3. 缓存不存在,执行回调函数

func (*Group) RegisterPeers

func (g *Group) RegisterPeers(peers PeerPicker)

type HTTPPool

type HTTPPool struct {
	// contains filtered or unexported fields
}

HTTPPool 基于 HTTP 协议建立分布式缓存中<节点间通信>

func NewHTTPPool

func NewHTTPPool(self string) *HTTPPool

func (*HTTPPool) Log

func (p *HTTPPool) Log(format string, v ...interface{})

func (*HTTPPool) PickPeer

func (p *HTTPPool) PickPeer(key string) (PeerGetter, bool)

PickPeer 根据 key 和一致性哈希选择兄弟节点,返回与该节点通信的客户端

func (*HTTPPool) ServeHTTP

func (p *HTTPPool) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP 节点缓存访问路径:/basePath/groupname/key

func (*HTTPPool) SetPeers

func (p *HTTPPool) SetPeers(peers ...string)

type PeerGetter

type PeerGetter interface {
	Get(*pb.Request, *pb.Response) error
}

分布式缓存的目的是不同key缓存在不同的节点上,增加总的吞吐量

type PeerPicker

type PeerPicker interface {
	PickPeer(string) (PeerGetter, bool)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳