refactor(cache): add correct unit indication

This commit is contained in:
Jan De Dobbeleer 2021-11-23 11:13:32 +01:00 committed by Jan De Dobbeleer
parent ea35416363
commit c5fb63885f
4 changed files with 10 additions and 7 deletions

View file

@ -52,8 +52,8 @@ type cache interface {
init(home string)
close()
get(key string) (string, bool)
// ttl in seconds
set(key, value string, ttl int64)
// ttl in minutes
set(key, value string, ttl int)
}
type environmentInfo interface {

View file

@ -13,7 +13,7 @@ const (
type cacheObject struct {
Value string `json:"value"`
Timestamp int64 `json:"timestamp"`
TTL int64 `json:"ttl"`
TTL int `json:"ttl"`
}
type fileCache struct {
@ -49,6 +49,8 @@ func (fc *fileCache) close() {
}
}
// returns the value for the given key as long as
// the TTL (minutes) is not expired
func (fc *fileCache) get(key string) (string, bool) {
val, found := fc.cache.get(key)
if !found {
@ -58,7 +60,7 @@ func (fc *fileCache) get(key string) (string, bool) {
if !ok {
return "", false
}
expired := time.Now().Unix() >= (co.Timestamp + co.TTL*60)
expired := time.Now().Unix() >= (co.Timestamp + int64(co.TTL)*60)
if expired {
fc.cache.remove(key)
return "", false
@ -66,7 +68,8 @@ func (fc *fileCache) get(key string) (string, bool) {
return co.Value, true
}
func (fc *fileCache) set(key, value string, ttl int64) {
// sets the value for the given key with a TTL (minutes)
func (fc *fileCache) set(key, value string, ttl int) {
fc.cache.set(key, &cacheObject{
Value: value,
Timestamp: time.Now().Unix(),

View file

@ -39,6 +39,6 @@ func (_m *MockedCache) init(home string) {
}
// set provides a mock function with given fields: key, value, ttl
func (_m *MockedCache) set(key, value string, ttl int64) {
func (_m *MockedCache) set(key, value string, ttl int) {
_m.Called(key, value, ttl)
}

View file

@ -76,7 +76,7 @@ func (d *owm) string() string {
}
func (d *owm) getResult() (*owmDataResponse, error) {
cacheTimeout := int64(d.props.getInt(CacheTimeout, DefaultCacheTimeout))
cacheTimeout := d.props.getInt(CacheTimeout, DefaultCacheTimeout)
response := new(owmDataResponse)
if cacheTimeout > 0 {
// check if data stored in cache