cache

package module
v1.1.15 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2022 License: MIT Imports: 22 Imported by: 0

README

特性

  1. 采用sync.Map为基础进行设计,具有高可用性与安全并发特性。
  2. 具有持久化与临时储存功能,持久化采用主备双文件保存,每个数据均使用crc32校验以保证数据的安全、准确性。
  3. 支持多种格式直接保存读取,string,bool,int-int64,uint-uint64,nil,float32,float64,[]byte,其他格式使用json反序列化获取

使用说明

Start(path) 启动

必须使用Start来启动cache,path为空的时候将缓存保存到./cache文件夹

StartWebServer(ipPort)

开启web监控页面,方便查看当前缓存内存

  cache.StartWebServer("0.0.0.0:8080")

NoPersistence

cache.NoPersistence = true //关闭持久化功能,缓存增删改不写入硬盘,建议把生成的本地缓存./cache/db.cache ./cache/db.cache.bak一起删除

Hset(name,value path,expire)

  • value支持一下三个格式,会转化为string的key与interface{}的value
    • 任意map
    • struct,这时候会按照struct的成员名字转化key,成员值转为value
    • sync.Map
  cache.Hset("luyu6056",map[string]interface{}{"login_time":1591174524},"member_info") //第四个参数,expire为过期时间,可以省略,默认永久保存

Hget(name,path)

获取取一个cache的结构体用于后续操作,如果不存在则创建一个新的缓存,这里命名为_hset

  _hset := cache.Hget("luyu6056","member_info")  可以利用不同的patch进行批量删除

Has(name,path)

判断缓存是否存在,存在返回缓存对象

  _hset,ok:=cache.Has("luyu6056","member_info")
  if !ok{
    fmt.Println("缓存不存在")
  }

Hdel(name,path)

删除一个缓存

  cache.Hdel("luyu6056","member_info")

Hdel_all(path)

针对path删除一整组缓存

  cache.Hdel("member_info")

_hset.Load(key)

从_hset读取一个缓存数据,返回interface{}和bool

  v,ok := _hset.Load("login_time")
  if ok {
    fmt.Println(v) //int 1591174524
  }

_hset.Get(key,&value)

使用该缓存的[]byte,对value进行反序列化赋值,建议之前保存的格式一致,否则调用Unmarshal会出现其它问题

  var login_time int
  ok := _hset.Get("login_time",&s)
  if ok {
    fmt.Println(login_time) //1591174524
  }

Load_str,Load_int,Load_int64,Load_int32,Load_int16,Load_int8,Load_uint64,Load_float64,Load_float32,Load_bool

尝试对改缓存进行强行转换输出

  s := _hset.Load_str("login_time")
  fmt.Println(s) //1591174524

_hset.Len()

返回hset的长度

  fmt.Println(_hset.Len()) //1

_hset.Range(func(key value)bool)

遍历_hset,key为string,value为interface{}

  _hset.range(func(key string,value interface)bool{
    fmt.Println(key,value) //login_time 1591174524
    return true
  })

_hset.GetExpire()

获取超时时间,返回时间戳

  fmt.Println(_hset.GetExpire()) //-1无限长

_hset.EXPIRE(expire)

在当前时间基础上,设置过期时间,单位秒

  _hset.EXPIRE(30) //30秒后删除

_hset.EXPlREAT(timestamp)

设置过期时间,传入秒级时间戳

  _hset.EXPIRE(time.Now().Unix()+30) //超过当前时间会立即删除

_hset.Hset(value)

写入key-value值,与Hset()一样,支持传入任意map,struct与sync.Map

  _hset.Hset(map[string]interface{}{"login_time":1591174524})

_hset.Set(key,value)

只写入一个key-value

  _hset.Hset("login_time",1591174524)

_hset.Store(key,value)

写入一个临时key,重启失效

  _hset.Store("temp",true)

_hset.Save()

对所有key进行异步保存,包括上面Store临时新建的key

  _hset.Save()

_hset.Save_r()

对所有key进行立刻保存,程序会阻塞到写入完成

  _hset.Save_r()

_hset.Delete(key)

删掉某个key

  _hset.Delete("temp")

_hset.INCRBY(key,add)

对某个key的值进行进行加减,类型为int64,负值为减,正值为增加

  _hset.INCRBY("uid",1)

Documentation

Index

Constants

View Source
const (
	CACHE_FILE_NAME = "db.cache" //持久化储存的文件名

	SAVE_TIME  = 1          //持久化间隔时间,单位秒
	GZIP_LIMIT = 4096       //大于这个尺寸就压缩
	MAXLEN     = 1073741824 //128M 缓存单条消息大于这个尺寸就抛弃
	GZIP_LEVEL = 6          //压缩等级
)

本cache包会监听ctrl+c事件以保证缓存被正确保存

Variables

View Source
var (
	CACHE_Path    = "./cache"
	ISDEBUG       = true  //是否fmt打印错误
	NoPersistence = false //true则对所有缓存不持久化写入

)

Functions

func Bytes2str

func Bytes2str(b []byte) string

func CopyFile

func CopyFile(srcName, dstName string) (written int64, err error)

func Crc32_check

func Crc32_check(s []byte) []byte

仅用于校验数据完整性

func DEBUG

func DEBUG(v ...interface{})

func Destroy

func Destroy()

func DogzipCompress

func DogzipCompress(src []byte) []byte

gzip压缩

func DogzipUnCompress

func DogzipUnCompress(compressSrc []byte) []byte

进行gzip解压缩

func Hdel

func Hdel(name string, path string)

hash删除

func Hdel_all

func Hdel_all(path string)

hash删除path下所有key

func Hset

func Hset(name string, value interface{}, path string, expire ...int64) bool

写入数据

func LINDEX

func LINDEX(key string, index int) (result string, ok bool)

*

  • 通过索引获取队列的元素
  • 获取失败返回nil,false *

func LLEN

func LLEN(key string) int

*

  • 获取列表长度 *

func LPOP

func LPOP(key string, timeout ...int) (result string, ok bool)

*

*取出指定列表的第一个元素,如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
*LPOP(list1,100)取出名字为list1的列表,没有会等待100秒
*LPOP(list1)取出列表,没有直接返回
*当ok返回值为false,则为超时取队列失败

func LPUSH

func LPUSH(key string, list ...string) bool

*

*将一个或多个值插入到列表的头部(最左边),用法同Rpush
*

func LRANGE

func LRANGE(key string, start int, param ...int) ([]string, bool)

*

  • 获取列表指定范围内的元素,起始元素是0
  • 表不存在返回false
  • LRANGE("list",2,3)取第2到3个元素
  • LRANGE("list",5,2)如果start比stop小,调换他们的顺序,取第2到第5个元素
  • LRANGE("list",-2,1)取第1个到倒数第2个元素,假如10个元素,等同于1,8
  • LRANGE("list",2)如果stop为空,则取第0到2个元素
  • LRANGE("list",-3) 取最后3个元素
  • 假如stop超过列表长度,返回空 *

func LREM

func LREM(key string, count int, value string) bool

*

*根据参数 COUNT 的值,移除列表中与参数 VALUE 相等的元素。
*count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素,数量为 COUNT 。
*count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值。
*count = 0 : 移除表中所有与 VALUE 相等的值。

func LTRIM

func LTRIM(key string, start int, param ...int) bool

*

  • LTRIM 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。
  • start 与 stop定义参照LRANGE
  • 设置超过最大值的start会清空列表
  • 设置超过最大值的stop等同于最大值 *

func Log

func Log(format string, v ...interface{})

func PathExists added in v1.0.1

func PathExists(path string) (bool, error)

判断文件夹是否存在

func RPOP

func RPOP(key string, timeout ...int) (result string, ok bool)

*

*取出指定列表的最后一个元素,如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
*RPOP(list1,100)取出名字为list1的列表,没有会等待100秒
*RPOP(list1)取出列表,没有直接返回
*当ok返回值为false,则为超时失败

func RPUSH

func RPUSH(key string, list ...string) bool

* *将一个或多个值插入到列表的尾部(最右边)。 *插入一个值Rpush("mylist","hello") *插入多个值Rpush("mylist","1","2","3") *插入[]interface{}切片:

var list []interface{}
list = append(list,"1")
list = append(list,map[string]string{"name":"luyu"})
list = append(list,100)
Rpush("mylist",list...)

*

func RangePath added in v1.0.5

func RangePath(path string, f func(string, *Hashvalue) bool)

func Start added in v1.1.13

func Start(workdir string)

func StartWebServer added in v1.1.0

func StartWebServer(ipPort string)

Types

type Gzip_writer

type Gzip_writer struct {
	Buf    *MsgBuffer
	Writer *gzip.Writer
}

type Hash_file

type Hash_file struct {
	P string
	K string
	V map[string][]byte
	T int64
}

*

  • 持久化写出增量文件

type Hashvalue

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

func Has

func Has(name string, path string) (*Hashvalue, bool)

hash读取

func Hget

func Hget(name string, path string) *Hashvalue

func (*Hashvalue) Delete

func (this *Hashvalue) Delete(key string)

func (*Hashvalue) Expire

func (this *Hashvalue) Expire(expire int64)

设置超时

func (*Hashvalue) ExpireAt

func (this *Hashvalue) ExpireAt(timestamp int64)

设置超时删除,到期删除这一组数据,参数时间戳

func (*Hashvalue) Get

func (this *Hashvalue) Get(key string, value interface{}) bool

func (*Hashvalue) GetExpire

func (this *Hashvalue) GetExpire() int64

func (*Hashvalue) Hdel

func (this *Hashvalue) Hdel()

删除掉所有数据

func (*Hashvalue) Hset

func (this *Hashvalue) Hset(value interface{}) bool

func (*Hashvalue) INCRBY added in v1.1.3

func (this *Hashvalue) INCRBY(key string, add int64) int64

func (*Hashvalue) Len

func (this *Hashvalue) Len() (length int)

func (*Hashvalue) Load

func (this *Hashvalue) Load(key string) (interface{}, bool)

path层

type cache_path struct {
	cache *sync.Map
	//hot_step    int64
	//hot_max     int64
	//hot_num_max int64
}

func (*Hashvalue) Load_bool

func (this *Hashvalue) Load_bool(key string) bool

func (*Hashvalue) Load_float32

func (this *Hashvalue) Load_float32(key string) float32

load返回float32

func (*Hashvalue) Load_float64

func (this *Hashvalue) Load_float64(key string) float64

load返回float64

func (*Hashvalue) Load_int

func (this *Hashvalue) Load_int(key string) int

load返回int

func (*Hashvalue) Load_int16

func (this *Hashvalue) Load_int16(key string) int16

load返回int16

func (*Hashvalue) Load_int32

func (this *Hashvalue) Load_int32(key string) int32

load返回int32

func (*Hashvalue) Load_int64

func (this *Hashvalue) Load_int64(key string) int64

load返回int64

func (*Hashvalue) Load_int8

func (this *Hashvalue) Load_int8(key string) int8

load返回int8

func (*Hashvalue) Load_str

func (this *Hashvalue) Load_str(key string) string

Load返回string,以下load扩展方法不支持传入struct

func (*Hashvalue) Load_uint64

func (this *Hashvalue) Load_uint64(key string) uint64

load返回uint64

func (*Hashvalue) Range

func (this *Hashvalue) Range(f func(string, interface{}) bool)

func (*Hashvalue) Save

func (this *Hashvalue) Save()

保存整条缓存

func (*Hashvalue) Save_r

func (this *Hashvalue) Save_r()

保存整条缓存

func (*Hashvalue) Set

func (this *Hashvalue) Set(key string, value interface{}) bool

func (*Hashvalue) Store

func (this *Hashvalue) Store(key string, value interface{})

*

*使用Hset,Hset_r可以保存本地文件持久化
*使用Store方法,可以临时保存内容到缓存,重启失效
*

type MsgBuffer

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

func New

func New(size int) *MsgBuffer

New returns a new MsgBuffer whose buffer has the given size.

func NewBuffer

func NewBuffer(n int) *MsgBuffer

func (*MsgBuffer) Bytes

func (w *MsgBuffer) Bytes() []byte

func (*MsgBuffer) Close

func (r *MsgBuffer) Close() error

func (*MsgBuffer) Len

func (w *MsgBuffer) Len() int

func (*MsgBuffer) Make

func (w *MsgBuffer) Make(l int) []byte

func (*MsgBuffer) Next

func (w *MsgBuffer) Next(l int) []byte

func (*MsgBuffer) PreBytes

func (w *MsgBuffer) PreBytes(n int) []byte

func (*MsgBuffer) Read

func (r *MsgBuffer) Read(p []byte) (n int, err error)

func (*MsgBuffer) ReadByte

func (r *MsgBuffer) ReadByte() (b byte, err error)

ReadByte reads and returns the next byte from the input or ErrIsEmpty.

func (*MsgBuffer) Reset

func (w *MsgBuffer) Reset()

func (*MsgBuffer) ResetBuf

func (w *MsgBuffer) ResetBuf(b []byte)

func (*MsgBuffer) Shift

func (r *MsgBuffer) Shift(len int)

Shift shifts the "read" pointer.

func (*MsgBuffer) String

func (w *MsgBuffer) String() string

func (*MsgBuffer) Truncate

func (w *MsgBuffer) Truncate(i int)

func (*MsgBuffer) Write

func (w *MsgBuffer) Write(b []byte) (int, error)

func (*MsgBuffer) WriteByte

func (w *MsgBuffer) WriteByte(s byte)

func (*MsgBuffer) WriteString

func (w *MsgBuffer) WriteString(s string)

Jump to

Keyboard shortcuts

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