memproxy

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2023 License: MIT Imports: 6 Imported by: 0

README

memproxy Coverage Status

Golang Memcache Proxy Library

Why this library?

This library helps to utilize memcached in a consistent and efficient way.

Supporting features:

  • Deal with Consistency between Memcached and Database using the Lease Mechanism.
  • Prevent thundering herd (a.k.a Cache Stampede).
  • Efficient batching get to the underlining database, batching between lease gets and between retries for preventing thundering-herd.
  • Memcached replication similar to MCRouter, without the need for external proxies.
  • Memory-weighted load-balancing for replication.

Table of Contents

  1. Usage
  2. Consistency between Memcached and Database
  3. Preventing Thundering Herd
  4. Efficient Batching
  5. Memcache Replication & Memory-Weighted Load Balancing

Usage

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidLeaseGetResponse = errors.New("invalid lease get response")

ErrInvalidLeaseGetResponse ...

Functions

This section is empty.

Types

type CallbackFunc added in v1.1.0

type CallbackFunc struct {
	Object unsafe.Pointer
	Func   func(obj unsafe.Pointer)
}

CallbackFunc for session

func NewEmptyCallback added in v1.1.0

func NewEmptyCallback(fn func()) CallbackFunc

NewEmptyCallback creates CallbackFunc from empty args function

func (CallbackFunc) Call added in v1.1.0

func (f CallbackFunc) Call()

Call ...

type DeleteOptions

type DeleteOptions struct {
}

DeleteOptions delete options

type DeleteResponse

type DeleteResponse struct {
}

DeleteResponse delete response

type LeaseGetErrorResult added in v1.1.0

type LeaseGetErrorResult struct {
	Error error
}

LeaseGetErrorResult for error only result

func (LeaseGetErrorResult) Result added in v1.1.0

Result ...

type LeaseGetOptions

type LeaseGetOptions struct {
}

LeaseGetOptions lease get options

type LeaseGetResponse

type LeaseGetResponse struct {
	Status LeaseGetStatus
	CAS    uint64
	Data   []byte
}

LeaseGetResponse lease get response

type LeaseGetResult added in v1.1.0

type LeaseGetResult interface {
	Result() (LeaseGetResponse, error)
}

LeaseGetResult is the response of LeaseGet, method Result MUST only be called Once. Calling Result more than once is undefined behaviour

type LeaseGetResultFunc added in v1.1.0

type LeaseGetResultFunc func() (LeaseGetResponse, error)

LeaseGetResultFunc for function implementation of LeaseGetResult

func (LeaseGetResultFunc) Result added in v1.1.0

Result ...

type LeaseGetStatus

type LeaseGetStatus uint32

LeaseGetStatus status of lease get

const (
	// LeaseGetStatusFound returns Data
	LeaseGetStatusFound LeaseGetStatus = iota + 1

	// LeaseGetStatusLeaseGranted lease granted
	LeaseGetStatusLeaseGranted

	// LeaseGetStatusLeaseRejected lease rejected
	LeaseGetStatusLeaseRejected
)

type LeaseSetOptions

type LeaseSetOptions struct {
	TTL uint32
}

LeaseSetOptions lease set options

type LeaseSetResponse

type LeaseSetResponse struct {
	Status LeaseSetStatus
}

LeaseSetResponse lease set response

type LeaseSetStatus

type LeaseSetStatus uint32

LeaseSetStatus ...

const (
	// LeaseSetStatusStored ...
	LeaseSetStatusStored LeaseSetStatus = iota + 1

	// LeaseSetStatusNotStored NOT stored because of key already been deleted or CAS has changed
	LeaseSetStatusNotStored
)

type Memcache

type Memcache interface {
	// Pipeline creates a Pipeline, a NON thread safe object
	Pipeline(ctx context.Context, options ...PipelineOption) Pipeline

	// Close shutdowns memcache client
	Close() error
}

Memcache represents a generic Memcache interface implementations of this interface must be thread safe

func NewPlainMemcache

func NewPlainMemcache(
	client *memcache.Client,
	options ...PlainMemcacheOption,
) Memcache

NewPlainMemcache a light wrapper around memcached client

type Pipeline

type Pipeline interface {
	// LeaseGet should not be used directly, use the item or mmap package instead
	LeaseGet(key string, options LeaseGetOptions) LeaseGetResult

	LeaseSet(key string, data []byte, cas uint64, options LeaseSetOptions) func() (LeaseSetResponse, error)
	Delete(key string, options DeleteOptions) func() (DeleteResponse, error)

	// Execute flush commands to the network
	Execute()

	// Finish must be called after create a Pipeline, often by defer
	Finish()

	// LowerSession returns a lower priority session
	LowerSession() Session
}

Pipeline represents a generic Pipeline

type PipelineConfig

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

PipelineConfig ...

func ComputePipelineConfig

func ComputePipelineConfig(options []PipelineOption) *PipelineConfig

ComputePipelineConfig ...

func (*PipelineConfig) GetSession

func (c *PipelineConfig) GetSession(provider SessionProvider) Session

GetSession ...

type PipelineOption

type PipelineOption func(conf *PipelineConfig)

PipelineOption ...

func WithPipelineExistingSession

func WithPipelineExistingSession(sess Session) PipelineOption

WithPipelineExistingSession ...

type PlainMemcacheOption

type PlainMemcacheOption func(opts *plainMemcacheConfig)

PlainMemcacheOption ...

func WithPlainMemcacheLeaseDuration

func WithPlainMemcacheLeaseDuration(leaseDurationSeconds uint32) PlainMemcacheOption

WithPlainMemcacheLeaseDuration ...

func WithPlainMemcacheSessionProvider

func WithPlainMemcacheSessionProvider(sessProvider SessionProvider) PlainMemcacheOption

WithPlainMemcacheSessionProvider ...

type Session

type Session interface {
	AddNextCall(fn CallbackFunc)
	AddDelayedCall(d time.Duration, fn CallbackFunc)
	Execute()

	GetLower() Session
}

Session controlling session values & delayed tasks, this object is NOT Thread Safe

type SessionProvider

type SessionProvider interface {
	New() Session
}

SessionProvider for controlling delayed tasks, this object is Thread Safe

func NewSessionProvider

func NewSessionProvider(options ...SessionProviderOption) SessionProvider

NewSessionProvider is THREAD SAFE

type SessionProviderOption

type SessionProviderOption func(conf *sessionProviderConf)

SessionProviderOption ...

func WithSessionNowFunc

func WithSessionNowFunc(nowFn func() time.Time) SessionProviderOption

WithSessionNowFunc ...

func WithSessionSleepFunc

func WithSessionSleepFunc(sleepFn func(d time.Duration)) SessionProviderOption

WithSessionSleepFunc ...

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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