gencache

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2024 License: MIT Imports: 12 Imported by: 0

README

GenCache

Just a demo distributed cache implemented by go.

Structure

geecache
    |--consistenthash
        |--consistenthash.go // 一致性哈希选择节点
    |--gencachepb
        |--gencachepb.pb.go  // protobuf 具体实现
        |--gencachepb.proto  // protobuf 数据结构的定义
    |--lru
        |--lru.go  // lru 缓存淘汰策略
    |--byteview.go // 缓存值的抽象与封装
    |--cache.go    // 并发控制
    |--geecache.go // 负责与外部交互,控制缓存存储和获取的主流程
    |--http.go     // 提供节点间通讯的能力(基于http)

Procedure

                          是
接收 key --> 检查是否被缓存 -----> 返回缓存值
                 |  否                        
                 |-----> 使用一致性哈希选择节点             是                                   是
                                 |-----> 是否是远程节点 -----> HTTP 客户端访问远程节点 --> 是否成功-----> 服务端返回混存值
                                              |  否                                     ↓  否
                                              |---------------------------------> 回退到本地节点处理
                                                                                         |-----> 调用`回调函数`,获取值并添加到缓存 --> 返回缓存值 

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 (bv ByteView) ByteSlice() []byte

ByteSlice returns a copy of the data as a byte slice.

func (ByteView) Len

func (bv ByteView) Len() int

type Getter

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

type GetterFunc

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

func (GetterFunc) Get

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

type Group

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

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)

func (*Group) RegisterPeers

func (g *Group) RegisterPeers(peers PeerPiker)

type HTTPPool

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

func NewHTTPPool

func NewHTTPPool(self string) *HTTPPool

func (*HTTPPool) Log

func (hp *HTTPPool) Log(format string, a ...any)

func (*HTTPPool) PickPeer

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

PickPeer picks a peer according to the key

func (*HTTPPool) ServeHTTP

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

func (*HTTPPool) Set

func (hp *HTTPPool) Set(peers ...string)

Set reset the pool's list of peers, including self

type PeerGetter

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

type PeerPiker

type PeerPiker interface {
	PickPeer(key string) (PeerGetter, bool)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL