gocache

package module
v0.0.0-...-4841b2c Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2020 License: MIT Imports: 11 Imported by: 0

README

🦜 Gocache

MIT PkgGoDev

gocache is a distributed caching library, adapted from groupcache, intended as a replacement for memcached in many cases.

Installation

Install and update this package with go get -u github.com/sinomoe/gocache.

Usage

  1. define getter function(used when cache missed) implement Getter interface

    getFromDB := gocache.GetterFunc(func(key string) ([]byte, error) {
        data, err :=  db.Get(key) // get data from database by the key
        if err == nil {
            return data, nil
        }
    	return nil, fmt.Errorf("%s not exist", key)
    })
    
  2. initialize a group named as "scores" with max 2048 cached items

    goc := gocache.NewGroup("scores", 2<<10, getFromDB)
    
  3. create httpPool instance using local addr

    peers := gocache.NewHTTPPool("http://localhost:8001")
    
  4. set peers on httpPool instance, there are three peers, every addr is meant to a gocache peer

    peers.SetPeers("http://localhost:8002", "http://localhost:8002", "http://localhost:8003")
    
  5. register httpPool(peers) instance on group instance

    goc.RegisterPeers(peers)
    

License

MIT © sino

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
}

ByteView holds immutable view of bytes

func (ByteView) ByteSlice

func (v ByteView) ByteSlice() []byte

ByteSlice returns a copy of the data as a slice

func (ByteView) Len

func (v ByteView) Len() int

Len returns the length of the byteview

func (ByteView) String

func (v ByteView) String() string

String stringifies the bytes of the byteview

type Getter

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

Getter describes user defined function used to get data when data not exists in the cache

type GetterFunc

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

GetterFunc is a function which implements Getter interface

func (GetterFunc) Get

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

Get implements Getter interface

type Group

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

Group is a cache namespace and associated data loaded spread over

func GetGroup

func GetGroup(name string) *Group

GetGroup return the group according to the specified group name if there is not a group named as name, it returns nil

func NewGroup

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

NewGroup creates a new group instance and hold it in groups

func (*Group) Get

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

Get gets the byteview according to the specified key

func (*Group) RegisterPeers

func (g *Group) RegisterPeers(peers PeerPicker)

RegisterPeers registers a PeerPicker for choosing remote peer

type HTTPPool

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

HTTPPool is a http server contains a peer's name and path

func NewHTTPPool

func NewHTTPPool(self string) *HTTPPool

NewHTTPPool creates an instace of http server which is used to communicate with the other peers

func (*HTTPPool) Log

func (pool *HTTPPool) Log(format string, value ...interface{})

Log logs info with server name

func (*HTTPPool) PickPeer

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

PickPeer picks a peer according to the specified key

func (*HTTPPool) ServeHTTP

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

ServeHTTP serves HTTP connections

func (*HTTPPool) SetPeers

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

SetPeers sets pool's list of peers

type PeerGetter

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

PeerGetter is the interface that must be implemented by a peer

type PeerPicker

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

PeerPicker is the interface that must be implemented to locate the peer that specified by the key

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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