Ai
1 Star 0 Fork 15

shyaoht/cachego

forked from 水不要鱼/cachego 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
value.go 1.45 KB
一键复制 编辑 原始数据 按行查看 历史
水不要鱼 提交于 2022-02-27 16:36 +08:00 . 不兼容的公测版本
// Copyright 2020 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cachego
import (
"time"
)
// value is a box of data.
type value struct {
// data stores the real thing inside.
data interface{}
// ttl is the life of value.
ttl time.Duration
// createTime is the created time of value.
createTime time.Time
}
// newValue returns a new value with data and ttl.
func newValue(data interface{}, ttl time.Duration) *value {
if ttl < 0 {
ttl = noTTL // Should panic if ttl < 0?
}
return &value{
data: data,
ttl: ttl,
createTime: time.Now(),
}
}
// alive returns if this value is alive or not.
func (v *value) alive() bool {
return v != nil && (v.ttl == noTTL || time.Since(v.createTime) <= v.ttl)
}
// renew sets v to a new one.
func (v *value) renew(data interface{}, ttl time.Duration) *value {
if v == nil {
return nil
}
v.data = data
v.ttl = ttl
v.createTime = time.Now()
return v
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/shyaoht/cachego.git
git@gitee.com:shyaoht/cachego.git
shyaoht
cachego
cachego
master

搜索帮助