memcache

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2024 License: MIT Imports: 11 Imported by: 0

README

把 memcache go client 修改为适用于 改版fatcache 的客户端

大致修改:

memcache.go 中的几个函数

legalKey()	由Get()调用
取消了key的长度限制(原为250字节)
取消了对空格符的非法判定,使用时直接把多个key连同空格作为一个string一起传入

Get()
传入参数中增加了 start_time uint64, end_time uint64 ,用于查询时间范围
返回值增加了 itemValues []string ,把从fatcache终端读取到的结果直接按行存储在string数组中
getFromAddr()	由Get()调用
传入参数增加了 start_time uint64, end_time uint64, itemValues *[]string
parseGetResponse() 	由getFromAddr()调用
传入参数增加了itemValues *[]string
直接从终端根据换行符读取每一行数据,遇到 "END\r\n" 结束读取
结果存入string数组中返回到Get()
	*itemValues = append(*itemValues, string(it.Value))

Documentation

Overview

Package memcache provides a client for the memcached cache server. memcache包为缓存服务器提供客户端

Index

Constants

View Source
const (
	// DefaultTimeout is the default socket read/write timeout.		默认超时时间
	DefaultTimeout = 500 * time.Millisecond

	// DefaultMaxIdleConns is the default maximum number of idle connections	默认最多有两个闲置的连接
	// kept for any single address.
	DefaultMaxIdleConns = 2
)

Variables

View Source
var (
	// ErrCacheMiss means that a Get failed because the item wasn't present.	//未匹配
	ErrCacheMiss = errors.New("memcache: cache miss")

	// ErrCASConflict means that a CompareAndSwap call failed due to the
	// cached value being modified between the Get and the CompareAndSwap.
	// If the cached value was simply evicted rather than replaced,
	// ErrNotStored will be returned instead.
	ErrCASConflict = errors.New("memcache: compare-and-swap conflict")

	// ErrNotStored means that a conditional write operation (i.e. Add or
	// CompareAndSwap) failed because the condition was not satisfied.		//带条件的写,条件不符合
	ErrNotStored = errors.New("memcache: item not stored")

	// ErrServer means that a server error occurred.
	ErrServerError = errors.New("memcache: server error")

	// ErrNoStats means that no statistics were available.
	ErrNoStats = errors.New("memcache: no statistics available")

	// ErrMalformedKey is returned when an invalid key is used.
	// Keys must be at maximum 250 bytes long and not
	// contain whitespace or control characters.				// key的格式不对,过长或包含非法字符
	ErrMalformedKey = errors.New("malformed: key is too long or contains invalid characters")

	// ErrNoServers is returned when no servers are configured or available.	//没有可用的服务器
	ErrNoServers = errors.New("memcache: no servers configured or available")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	// DialContext connects to the address on the named network using the
	// provided context.
	//
	// To connect to servers using TLS (memcached running with "--enable-ssl"),
	// use a DialContext func that uses tls.Dialer.DialContext. See this
	// package's tests as an example.
	DialContext func(ctx context.Context, network, address string) (net.Conn, error)

	// Timeout specifies the socket read/write timeout.
	// If zero, DefaultTimeout is used.
	Timeout time.Duration

	// MaxIdleConns specifies the maximum number of idle connections that will
	// be maintained per address. If less than one, DefaultMaxIdleConns will be
	// used.
	//
	// Consider your expected traffic rates and latency carefully. This should
	// be set to a number higher than your peak parallel requests.
	MaxIdleConns int
	// contains filtered or unexported fields
}

Client is a memcache client. It is safe for unlocked use by multiple concurrent goroutines.

func New

func New(server ...string) *Client

New returns a memcache client using the provided server(s) with equal weight. If a server is listed multiple times, it gets a proportional amount of weight.

func NewFromSelector

func NewFromSelector(ss ServerSelector) *Client

NewFromSelector returns a new Client using the provided ServerSelector.

func (*Client) Add

func (c *Client) Add(item *Item) error

Add writes the given item, if no value already exists for its key. ErrNotStored is returned if that condition is not met.

func (*Client) Append

func (c *Client) Append(item *Item) error

Append appends the given item to the existing item, if a value already exists for its key. ErrNotStored is returned if that condition is not met.

func (*Client) Close

func (c *Client) Close() error

Close closes any open connections.

It returns the first error encountered closing connections, but always closes all connections.

After Close, the Client may still be used.

func (*Client) CompareAndSwap

func (c *Client) CompareAndSwap(item *Item) error

CompareAndSwap writes the given item that was previously returned by Get, if the value was neither modified nor evicted between the Get and the CompareAndSwap calls. The item's Key should not change between calls but all other item fields may differ. ErrCASConflict is returned if the value was modified in between the calls. ErrNotStored is returned if the value was evicted in between the calls.

func (*Client) Decrement

func (c *Client) Decrement(key string, delta uint64) (newValue uint64, err error)

Decrement atomically decrements key by delta. The return value is the new value after being decremented or an error. If the value didn't exist in memcached the error is ErrCacheMiss. The value in memcached must be a decimal number, or an error will be returned. On underflow, the new value is capped at zero and does not wrap around.

func (*Client) Delete

func (c *Client) Delete(key string) error

Delete deletes the item with the provided key. The error ErrCacheMiss is returned if the item didn't already exist in the cache. 用传入的key删除item

func (*Client) DeleteAll

func (c *Client) DeleteAll() error

DeleteAll deletes all items in the cache. 删除所有item 用 flush_all 命令

func (*Client) FlushAll

func (c *Client) FlushAll() error

func (*Client) Get

func (c *Client) Get(key string, start_time int64, end_time int64) (itemValues []byte, item *Item, err error)

Get gets the item for the given key. ErrCacheMiss is returned for a memcache cache miss. The key must be at most 250 bytes in length.

func (*Client) GetMulti

func (c *Client) GetMulti(keys []string, start_time int64, end_time int64) (map[string]*Item, error)

GetMulti is a batch version of Get. The returned map from keys to items may have fewer elements than the input slice, due to memcache cache misses. Each key must be at most 250 bytes in length. If no error is returned, the returned map will also be non-nil. GetMulti 从多个服务器上获取多个键对应的值,并将结果存储在一个 map 中返回 Get从单个服务器获取

func (*Client) Increment

func (c *Client) Increment(key string, delta uint64) (newValue uint64, err error)

Increment atomically increments key by delta. The return value is the new value after being incremented or an error. If the value didn't exist in memcached the error is ErrCacheMiss. The value in memcached must be a decimal number, or an error will be returned. On 64-bit overflow, the new value wraps around.

func (*Client) Ping

func (c *Client) Ping() error

Ping checks all instances if they are alive. Returns error if any of them is down.

func (*Client) Prepend

func (c *Client) Prepend(item *Item) error

Prepend prepends the given item to the existing item, if a value already exists for its key. ErrNotStored is returned if that condition is not met.

func (*Client) Replace

func (c *Client) Replace(item *Item) error

Replace writes the given item, but only if the server *does* already hold data for this key

func (*Client) Set

func (c *Client) Set(item *Item) error

Set writes the given item, unconditionally. 无条件写入给定的 item

func (*Client) Touch

func (c *Client) Touch(key string, seconds int32) (err error)

Touch updates the expiry for the given key. The seconds parameter is either a Unix timestamp or, if seconds is less than 1 month, the number of seconds into the future at which time the item will expire. Zero means the item has no expiration time. ErrCacheMiss is returned if the key is not in the cache. The key must be at most 250 bytes in length.

type ConnectTimeoutError

type ConnectTimeoutError struct {
	Addr net.Addr
}

ConnectTimeoutError is the error type used when it takes too long to connect to the desired host. This level of detail can generally be ignored.

func (*ConnectTimeoutError) Error

func (cte *ConnectTimeoutError) Error() string

type Item

type Item struct {
	// Key is the Item's key (250 bytes maximum).
	Key string

	// Value is the Item's value.
	Value []byte

	// Flags are server-opaque flags whose semantics are entirely
	// up to the app.
	Flags uint32

	// Expiration is the cache expiration time, in seconds: either a relative
	// time from now (up to 1 month), or an absolute Unix epoch time.
	// Zero means the Item has no expiration time.
	Expiration int32

	// CasID is the compare and swap ID.
	//
	// It's populated by get requests and then the same value is
	// required for a CompareAndSwap request to succeed.			它由get请求填充,然后CompareAndSwap请求需要相同的值才能成功。
	CasID uint64

	Time_start int64

	Time_end int64

	NumOfTables int64
}

Item is an item to be got or stored in a memcached server.

type ServerList

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

ServerList is a simple ServerSelector. Its zero value is usable.

func (*ServerList) Each

func (ss *ServerList) Each(f func(net.Addr) error) error

Each iterates over each server calling the given function 遍历每个调用该函数的服务器

func (*ServerList) PickServer

func (ss *ServerList) PickServer(key string) (net.Addr, error)

根据 key 返回相应的服务器地址

func (*ServerList) SetServers

func (ss *ServerList) SetServers(servers ...string) error

SetServers changes a ServerList's set of servers at runtime and is safe for concurrent use by multiple goroutines.

Each server is given equal weight. A server is given more weight if it's listed multiple times.

SetServers returns an error if any of the server names fail to resolve. No attempt is made to connect to the server. If any error is returned, no changes will be made to the ServerList.

type ServerSelector

type ServerSelector interface {
	// PickServer returns the server address that a given item
	// should be shared onto.
	PickServer(key string) (net.Addr, error) //返回给定 item 应该被分享到的服务器地址
	Each(func(net.Addr) error) error
}

ServerSelector is the interface that selects a memcache server 根据 item 的 key 选择 memcache server as a function of the item's key.

All ServerSelector implementations must be safe for concurrent use 必须支持并发 by multiple goroutines.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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