mc

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: May 31, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

README

mc

memcache 基础库,开放有限接口,支持日志、opentracing 和 prometheus 监控。

配置

MC 配置,格式为 MC_${NAME}_HOSTS = "host1",通过 ${NAME} 可以获取 MC 连接池。初始连接数使用 MC_DEFAULT_INIT_CONNS,最大连接数 MC_DEFAULT_MAX_IDLE_CONNS。

MC_XXX_HOSTS 只能填一个 memcache 实例。

高可用架构请使用 twemcache 等中间件。

示例

import "context" import "sniper/util/mc"

ctx := context.Background() c := mc.Get(ctx, "default")

err := c.Delete(ctx, "foo")

Documentation

Overview

Package mc memcache 客户端组件

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCacheMiss 未命中缓存
	ErrCacheMiss = memcache.ErrCacheMiss
	// ErrNotStored value未存下, 没有满足条件(i.e. Add or CompareAndSwap)
	ErrNotStored = memcache.ErrNotStored
)

Functions

func GatherMetrics

func GatherMetrics()

GatherMetrics 连接池状态指标

func IsCacheMiss

func IsCacheMiss(err error) bool

IsCacheMiss 如果没有命中缓存则返回 true

func IsNotStored

func IsNotStored(err error) bool

IsNotStored value是否因为未满足条件而未存储

func Reset

func Reset()

Reset 关闭所有 MC 连接 新调用 Get 方法时会使用最新 MC 配置创建连接

如果在配置中开启 HOT_LOAD_MC 开关,则每次下发配置都会重置 MC 连接!

Types

type Item

type Item memcache.Item

Item cache item

func (*Item) ParseInt

func (i *Item) ParseInt(base int, bitSize int) (int64, error)

ParseInt strconv.ParseInt(i.Value, base, bitSize)

type MC

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

MC memcache 客户端实例

func Get

func Get(ctx context.Context, name string) *MC

Get get mc by name

func (*MC) Add

func (mc *MC) Add(ctx context.Context, item *Item) error

Add 只在 key 不存在的时候设置新值

func (*MC) CompareAndSwap

func (mc *MC) CompareAndSwap(ctx context.Context, item *Item) error

CompareAndSwap cas

func (*MC) Decrement

func (mc *MC) Decrement(ctx context.Context, key string, delta uint64) (uint64, error)

Decrement 减小 key 对应的值,key 不存在则报错

func (*MC) Delete

func (mc *MC) Delete(ctx context.Context, key string) error

Delete del

func (*MC) Get

func (mc *MC) Get(ctx context.Context, key string) (*Item, error)

Get get

func (*MC) GetMulti

func (mc *MC) GetMulti(ctx context.Context, keys []string) (map[string]*Item, error)

GetMulti mget FIXME keys 为空的时候会报错

func (*MC) Increment

func (mc *MC) Increment(ctx context.Context, key string, delta uint64) (uint64, error)

Increment key 不存在会自动创建,跟原生 mc 客户端不同

func (*MC) Replace

func (mc *MC) Replace(ctx context.Context, item *Item) error

Replace 更新已有数据,没有返回错误

func (*MC) Set

func (mc *MC) Set(ctx context.Context, item *Item) error

Set 无条件设置数据

func (*MC) Touch

func (mc *MC) Touch(ctx context.Context, key string, seconds int32) error

Touch 更新过期时间

Jump to

Keyboard shortcuts

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