speed

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

README

speed

快速缓存库,支持超时设置,超时/删除回调

安装

go get github.com/cb252389238/speed
c, err := New()
if err != nil {
    panic(err)
}
//绑定回调删除,当元素过期、被删除得时候触发。v是对应得缓存值
c.BindDeleteCallBackFunc(func(v interface{}) {
    fmt.Println("触发回调函数", v)
})
//设置普通缓存
//k:键 v:值 d:过期时间 callBack:是否触发回调
c.Set(k string, v interface{}, d time.Duration, callBack bool)
//当key不存在时设置成功 否则失败
//k:键 v:值 d:过期时间 callBack:是否触发回调
//false 失败 true 成功
c.SetNx(k string, v interface{}, d time.Duration, callBack bool)bool
//获取缓存值
//k:键
c.GetGet(k string) (interface{}, bool)
//获取值和过期时间
c.GetEx(k string) (interface{}, time.Time, bool)
//删除缓存
c.Del(k string)
//获取所有普通缓存值
c.Items() map[string]interface{}
//获取缓存数量
c.ItemCount() int 
//判断缓存是否存在
c.Exists(k string) bool

//hash设置
c.HSet(key, field string, val interface{})
//为hash设置过期时间
c.HSetEx(key string, d time.Duration, callBack bool)bool
//设置多个字段值
c.HMSet(key string, data map[string]interface{})
//hash字段不存在设置成功,否则失败
c.HSetNx(key, field string, val interface{}) bool
//删除hash  fields为空删除整个hash  否则删除对应得字段
c.HDel(key string, fields ...string)
//判断hash是否存在字段,存在为true。多个字段全部都存在为true 否则为false
c.HExists(key string, fields ...string) bool
//获取hash字段值
c.HGet(key string, fields ...string) map[string]interface{}
//获取hash所有字段值
c.HGetAll(key string) map[string]interface{}
//获取hash所有field值
c.HKeys(key string) []string
//获取hash所有字段值
c.HVAls(key string) []interface{}


//无序集合添加值
c.SAdd(key string, d time.Duration, callBack bool, members ...interface{})
//获取无序集合成员个数
c.SCard(key string) int
//删除无序集合成员  返回删除个数
c.SRem(key string, members ...interface{}) int
//获取所有无序集合成员
c.SMembers(key string) []interface{}
//判断成员是否包含在无序集合中
c.SISMembers(key string, member interface{}) bool

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Epoch is set to the twitter snowflake epoch of Nov 04 2010 01:42:54 UTC in milliseconds
	// You may customize this to set a different epoch for your application.
	Epoch int64 = 1288834974657

	// NodeBits holds the number of bits to use for Node
	// Remember, you have a total 22 bits to share between Node/Step
	NodeBits uint8 = 10

	// StepBits holds the number of bits to use for Step
	// Remember, you have a total 22 bits to share between Node/Step
	StepBits uint8 = 12
)
View Source
var ErrInvalidBase32 = errors.New("invalid base32")

ErrInvalidBase32 is returned by ParseBase32 when given an invalid []byte

View Source
var ErrInvalidBase58 = errors.New("invalid base58")

ErrInvalidBase58 is returned by ParseBase58 when given an invalid []byte

Functions

func GetLoaclIp

func GetLoaclIp() string

func Ipv4StringToInt

func Ipv4StringToInt(ip string) int64

Types

type Cache

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

func New

func New() (*Cache, error)

func (Cache) BindDeleteCallBackFunc

func (c Cache) BindDeleteCallBackFunc(f func(string, interface{}))

func (Cache) Del

func (c Cache) Del(k string)

k-v删除

func (Cache) Exists

func (c Cache) Exists(k string) bool

判断k-v值是否存在

func (Cache) Get

func (c Cache) Get(k string) (interface{}, bool)

func (Cache) GetEx

func (c Cache) GetEx(k string) (interface{}, time.Time, bool)

获取k-v 过期时间

func (Cache) HDel

func (c Cache) HDel(key string, fields ...string)

func (Cache) HExists

func (c Cache) HExists(key string, fields ...string) bool

func (Cache) HGet

func (c Cache) HGet(key string, fields ...string) map[string]interface{}

func (Cache) HGetAll

func (c Cache) HGetAll(key string) map[string]interface{}

func (Cache) HKeys

func (c Cache) HKeys(key string) []string

func (Cache) HMSet

func (c Cache) HMSet(key string, data map[string]interface{})

func (Cache) HSet

func (c Cache) HSet(key, field string, val interface{})

func (Cache) HSetEx

func (c Cache) HSetEx(key string, d time.Duration, callBack bool) bool

func (Cache) HSetNx

func (c Cache) HSetNx(key, field string, val interface{}) bool

func (Cache) HVAls

func (c Cache) HVAls(key string) []interface{}

func (Cache) ItemCount

func (c Cache) ItemCount() int

获取k-v数量

func (Cache) Items

func (c Cache) Items() map[string]interface{}

获取k-v所有值

func (Cache) SAdd

func (c Cache) SAdd(key string, d time.Duration, callBack bool, members ...interface{})

func (Cache) SCard

func (c Cache) SCard(key string) int

func (Cache) SISMembers

func (c Cache) SISMembers(key string, member interface{}) bool

func (Cache) SMembers

func (c Cache) SMembers(key string) []interface{}

func (Cache) SRem

func (c Cache) SRem(key string, members ...interface{}) int

func (Cache) Set

func (c Cache) Set(k string, v interface{}, d time.Duration, callBack bool)

func (Cache) SetNx

func (c Cache) SetNx(k string, v interface{}, d time.Duration, callBack bool) bool

func (Cache) Stop

func (c Cache) Stop()

type HASHItem

type HASHItem struct {
	Object     map[string]interface{} //存储体
	Expiration int64                  //过期时间
	CallBack   bool                   //是否回调
	Key        string
}

type ID

type ID int64

An ID is a custom type used for a snowflake ID. This is used so we can attach methods onto the ID.

func ParseBase2

func ParseBase2(id string) (ID, error)

ParseBase2 converts a Base2 string into a snowflake ID

func ParseBase32

func ParseBase32(b []byte) (ID, error)

ParseBase32 parses a base32 []byte into a snowflake ID NOTE: There are many different base32 implementations so becareful when doing any interoperation.

func ParseBase36

func ParseBase36(id string) (ID, error)

ParseBase36 converts a Base36 string into a snowflake ID

func ParseBase58

func ParseBase58(b []byte) (ID, error)

ParseBase58 parses a base58 []byte into a snowflake ID

func ParseBase64

func ParseBase64(id string) (ID, error)

ParseBase64 converts a base64 string into a snowflake ID

func ParseBytes

func ParseBytes(id []byte) (ID, error)

ParseBytes converts a byte slice into a snowflake ID

func ParseInt64

func ParseInt64(id int64) ID

ParseInt64 converts an int64 into a snowflake ID

func ParseIntBytes

func ParseIntBytes(id [8]byte) ID

ParseIntBytes converts an array of bytes encoded as big endian integer as a snowflake ID

func ParseString

func ParseString(id string) (ID, error)

ParseString converts a string into a snowflake ID

func (ID) Base2

func (f ID) Base2() string

Base2 returns a string base2 of the snowflake ID

func (ID) Base32

func (f ID) Base32() string

Base32 uses the z-base-32 character set but encodes and decodes similar to base58, allowing it to create an even smaller result string. NOTE: There are many different base32 implementations so becareful when doing any interoperation.

func (ID) Base36

func (f ID) Base36() string

Base36 returns a base36 string of the snowflake ID

func (ID) Base58

func (f ID) Base58() string

Base58 returns a base58 string of the snowflake ID

func (ID) Base64

func (f ID) Base64() string

Base64 returns a base64 string of the snowflake ID

func (ID) Bytes

func (f ID) Bytes() []byte

Bytes returns a byte slice of the snowflake ID

func (ID) Int64

func (f ID) Int64() int64

Int64 returns an int64 of the snowflake ID

func (ID) IntBytes

func (f ID) IntBytes() [8]byte

IntBytes returns an array of bytes of the snowflake ID, encoded as a big endian integer.

func (ID) MarshalJSON

func (f ID) MarshalJSON() ([]byte, error)

MarshalJSON returns a json byte array string of the snowflake ID.

func (ID) Node

func (f ID) Node() int64

Node returns an int64 of the snowflake ID node number DEPRECATED: the below function will be removed in a future release.

func (ID) Step

func (f ID) Step() int64

Step returns an int64 of the snowflake step (or sequence) number DEPRECATED: the below function will be removed in a future release.

func (ID) String

func (f ID) String() string

String returns a string of the snowflake ID

func (ID) Time

func (f ID) Time() int64

Time returns an int64 unix timestamp in milliseconds of the snowflake ID time DEPRECATED: the below function will be removed in a future release.

func (*ID) UnmarshalJSON

func (f *ID) UnmarshalJSON(b []byte) error

UnmarshalJSON converts a json byte array of a snowflake ID into an ID type.

type JSONSyntaxError

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

A JSONSyntaxError is returned from UnmarshalJSON if an invalid ID is provided.

func (JSONSyntaxError) Error

func (j JSONSyntaxError) Error() string

type Job

type Job func(interface{})

Job 延时任务回调函数

type KVItem

type KVItem struct {
	Object     interface{} //存储体
	Expiration int64       //过期时间
	CallBack   bool        //是否回调
	Key        string
}

type Node

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

A Node struct holds the basic information needed for a snowflake generator node

func NewNode

func NewNode(node int64) (*Node, error)

NewNode returns a new snowflake node that can be used to generate snowflake IDs

func (*Node) Generate

func (n *Node) Generate() ID

Generate creates and returns a unique snowflake ID To help guarantee uniqueness - Make sure your system is keeping accurate system time - Make sure you never have multiple nodes running with the same node ID

type Set

type Set struct {
	Key string

	Member     interface{}
	Expiration int64 //过期时间
	CallBack   bool  //是否回调
	// contains filtered or unexported fields
}

type SetItem

type SetItem struct {
	Object map[interface{}]Set //存储体
}

type Task

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

Task 延时任务

type TimeWheel

type TimeWheel struct {
	C chan interface{} //时间轮通知通道
	// contains filtered or unexported fields
}

TimeWheel 时间轮

func NewTw

func NewTw(interval time.Duration, slotNum int, job Job) *TimeWheel

New 创建时间轮

func (*TimeWheel) AddTimer

func (tw *TimeWheel) AddTimer(delay time.Duration, key interface{}, data interface{})

AddTimer 添加定时器 key为定时器唯一标识

func (*TimeWheel) RemoveTimer

func (tw *TimeWheel) RemoveTimer(key interface{})

RemoveTimer 删除定时器 key为添加定时器时传递的定时器唯一标识

func (*TimeWheel) Start

func (tw *TimeWheel) Start()

Start 启动时间轮

func (*TimeWheel) Stop

func (tw *TimeWheel) Stop()

Stop 停止时间轮

Jump to

Keyboard shortcuts

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