cache

package module
v0.0.0-...-8fda7c6 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

并发控制

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloneBytes

func CloneBytes(b []byte) []byte

Types

type ByteView

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

只读数据结构,表示缓存值

func (ByteView) ByteSlice

func (View ByteView) ByteSlice() []byte

ByteView是只读的,使用该方法返回一个拷贝,防止缓存值被外部程序修改

func (ByteView) Len

func (View ByteView) Len() int

实现Value接口

func (ByteView) String

func (View ByteView) String() string

type CacheClient

type CacheClient struct {
	BaseURL string //BaseURL example : http://localhost:8888
}

rpc客户端

func (*CacheClient) Get

func (CC *CacheClient) Get(Req *cachepb.Request) (*cachepb.Response, error)

启动rpc客户端调用

type CacheServer

type CacheServer struct {
	cachepb.UnimplementedGroupCacheServer
	Self string //Self example : http://localhost:8888

	Getters map[string]*CacheClient
	// contains filtered or unexported fields
}

rpc服务器

func NewCacheServer

func NewCacheServer(addr string) *CacheServer

构造函数

func (*CacheServer) Get

func (*CacheServer) Log

func (CS *CacheServer) Log(format string, args ...any)

日志

func (*CacheServer) PickPeer

func (CS *CacheServer) PickPeer(key string) (PeerGetter, bool)

利用一致性哈希选择远端节点

func (*CacheServer) Run

func (CS *CacheServer) Run() error

启动rpc服务

func (*CacheServer) Set

func (CS *CacheServer) Set(peers ...string)

注册分布式系统中的节点

type Getter

type Getter interface {
	Get(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

通过name寻找对应的Group实例

func NewGroup

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

实例化Gruop对象

func (*Group) Get

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

查询key对应的value

func (*Group) GetFromPeer

func (g *Group) GetFromPeer(peer PeerGetter, key string) (ByteView, error)

从远端节点获取缓存

func (*Group) GetLocally

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

使用回调函数从本地数据源获取key对应的value值并加载到缓存

func (*Group) Load

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

尝试从远端节点获取缓存,失败则调用GetLocally方法,利用singleflight防止缓存击穿

func (*Group) PopulateCache

func (g *Group) PopulateCache(key string, value ByteView)

将key-value加载到缓存

func (*Group) RegisterPeers

func (g *Group) RegisterPeers(peers PeerPicker)

注册一个peers以选择远端节点

type PeerGetter

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

type PeerPicker

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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