ezcache

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 9, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

README

EZCache

Install

go get -u github.com/justlorain/ezcache

Usage

package main

import (
	"flag"
	"strings"

	"github.com/justlorain/ezcache"
)

func main() {
	addrs := ParseFlags()
	e := ezcache.NewEngine(addrs[0])
	e.RegisterNodes(addrs...)
	if err := e.Run(); err != nil {
		return
	}
}

// ParseFlags e.g.
// go run . --addrs=http://localhost:7246,http://localhost:7247,http://localhost:7248
// go run . --addrs=http://localhost:7247,http://localhost:7248,http://localhost:7246
// go run . --addrs=http://localhost:7248,http://localhost:7246,http://localhost:7247
// NOTE: first addr is local node
func ParseFlags() []string {
	var addrsFlag string
	flag.StringVar(&addrsFlag, "addrs", "", "nodes addresses")
	flag.Parse()
	return strings.Split(addrsFlag, ",")
}

Configuration

Option Default Description
WithBasePath /_ezcache Define base path of server
WithReplicationFactor 10 Define consistent hash replication factor
WithHashFunc crc32.ChecksumIEEE Define hash func used by consistent hash

License

EZCache is distributed under the Apache License 2.0.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyParam = errors.New("error empty param")
	ErrPickNote   = errors.New("error pick node")
)

Functions

func CopyBytes

func CopyBytes(b []byte) []byte

func DoHTTPRequest

func DoHTTPRequest(method, url string, body io.Reader) error

func SearchUint32s

func SearchUint32s(s []uint32, target uint32) int

func StandardizeAddr

func StandardizeAddr(addr string) string

Types

type ByteView

type ByteView struct {
	B []byte
}

func (ByteView) ByteSlice

func (v ByteView) ByteSlice() []byte

ByteSlice returns the copy of the data as a byte slice

func (ByteView) Len

func (v ByteView) Len() int

Len returns the length of the data

func (ByteView) String

func (v ByteView) String() string

String returns the data as a string

type Cache

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

func NewCache

func NewCache() *Cache

func (*Cache) Delete

func (c *Cache) Delete(key string)

func (*Cache) Get

func (c *Cache) Get(key string) (ByteView, bool)

func (*Cache) Set

func (c *Cache) Set(key string, value ByteView)

type Engine

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

func NewEngine

func NewEngine(addr string, opts ...Option) *Engine

func (*Engine) Delete

func (e *Engine) Delete(w http.ResponseWriter, r *http.Request)

func (*Engine) Get

func (e *Engine) Get(w http.ResponseWriter, r *http.Request)

func (*Engine) RegisterNodes

func (e *Engine) RegisterNodes(addrs ...string)

func (*Engine) Run

func (e *Engine) Run() error

func (*Engine) Set

func (e *Engine) Set(w http.ResponseWriter, r *http.Request)

type Hash

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

func NewHash

func NewHash(factor int, fn HashFunc) *Hash

NewHash consistent hash

func (*Hash) Add

func (h *Hash) Add(nodes ...string)

func (*Hash) Get

func (h *Hash) Get(key string) string

func (*Hash) Remove

func (h *Hash) Remove(key string)

type HashFunc

type HashFunc func(data []byte) uint32

type Option

type Option func(o *Options)

func WithBasePath

func WithBasePath(path string) Option

WithBasePath used to define base path of server

func WithHashFunc

func WithHashFunc(fn HashFunc) Option

WithHashFunc used to define hash func used by consistent hash

func WithReplicationFactor

func WithReplicationFactor(factor int) Option

WithReplicationFactor used to define consistent hash replication factor

type Options

type Options struct {
	BasePath          string
	ReplicationFactor int
	HashFunc          HashFunc
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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