up
This commit is contained in:
45
get.go
45
get.go
@@ -14,14 +14,14 @@ import (
|
||||
// Get 获取数据
|
||||
// key: 键 返回值: 数据 剩余时间 是否存在
|
||||
func (c *Cache[K, V]) Get(key K) (V, int64, bool) {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
|
||||
if data, ok := c.data[key]; ok {
|
||||
if data.End.IsZero() {
|
||||
return data.Val, -1, true
|
||||
} else if data.End.Before(time.Now()) {
|
||||
return data.Val, int64(data.End.Sub(time.Now()).Seconds()), true
|
||||
if dataAny, ok := c.data.Load(key); ok {
|
||||
data, ok := dataAny.(*Data[V])
|
||||
if ok {
|
||||
if data.End.IsZero() {
|
||||
return data.Val, -1, true
|
||||
} else if data.End.After(time.Now()) {
|
||||
return data.Val, int64(data.End.Sub(time.Now()).Seconds()), true
|
||||
}
|
||||
}
|
||||
}
|
||||
return c.zero(), 0, false
|
||||
@@ -37,14 +37,14 @@ func (c *Cache[K, V]) GetData(key K) (V, bool) {
|
||||
// GetTime 获取数据到期时间
|
||||
// key: 键 返回值: 到期时间 是否存在
|
||||
func (c *Cache[K, V]) GetTime(key K) (time.Time, bool) {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
|
||||
if data, ok := c.data[key]; ok {
|
||||
if c.cGetData != nil {
|
||||
c.cGetData(key, *data)
|
||||
if dataAny, ok := c.data.Load(key); ok {
|
||||
data, ok := dataAny.(*Data[V])
|
||||
if ok {
|
||||
if c.cGetTTL != nil {
|
||||
c.cGetTTL(key, *data)
|
||||
}
|
||||
return data.End, true
|
||||
}
|
||||
return data.End, true
|
||||
}
|
||||
return time.Time{}, false
|
||||
}
|
||||
@@ -61,13 +61,11 @@ func (c *Cache[K, V]) GetTTL(key K) (int64, bool) {
|
||||
// 返回值: 数据
|
||||
// 该功能为一些状态API提供支持
|
||||
func (c *Cache[K, V]) GetAll() map[K]Data[V] {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
|
||||
data := make(map[K]Data[V], len(c.data))
|
||||
for k, v := range c.data {
|
||||
data[k] = *v
|
||||
}
|
||||
data := make(map[K]Data[V])
|
||||
c.data.Range(func(key, val any) bool {
|
||||
data[key.(K)] = *val.(*Data[V])
|
||||
return true
|
||||
})
|
||||
return data
|
||||
}
|
||||
|
||||
@@ -85,6 +83,9 @@ var NegativeNumberErrMsg = errors.New("negative number")
|
||||
// NumberBigErrMsg 数字过大
|
||||
var NumberBigErrMsg = errors.New("number too big")
|
||||
|
||||
// KeyNotExists 不存在
|
||||
var KeyNotExists = errors.New("key not exists")
|
||||
|
||||
// GetString 获取数据并转为string
|
||||
// key: 键 返回值: 数据 是否存在 error
|
||||
// 支持类型: string,int*,uint*,float*,bool
|
||||
|
||||
Reference in New Issue
Block a user