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) init(home string)
close() close()
get(key string) (string, bool) get(key string) (string, bool)
// ttl in seconds // ttl in minutes
set(key, value string, ttl int64) set(key, value string, ttl int)
} }
type environmentInfo interface { type environmentInfo interface {

View file

@ -13,7 +13,7 @@ const (
type cacheObject struct { type cacheObject struct {
Value string `json:"value"` Value string `json:"value"`
Timestamp int64 `json:"timestamp"` Timestamp int64 `json:"timestamp"`
TTL int64 `json:"ttl"` TTL int `json:"ttl"`
} }
type fileCache struct { 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) { func (fc *fileCache) get(key string) (string, bool) {
val, found := fc.cache.get(key) val, found := fc.cache.get(key)
if !found { if !found {
@ -58,7 +60,7 @@ func (fc *fileCache) get(key string) (string, bool) {
if !ok { if !ok {
return "", false return "", false
} }
expired := time.Now().Unix() >= (co.Timestamp + co.TTL*60) expired := time.Now().Unix() >= (co.Timestamp + int64(co.TTL)*60)
if expired { if expired {
fc.cache.remove(key) fc.cache.remove(key)
return "", false return "", false
@ -66,7 +68,8 @@ func (fc *fileCache) get(key string) (string, bool) {
return co.Value, true 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{ fc.cache.set(key, &cacheObject{
Value: value, Value: value,
Timestamp: time.Now().Unix(), 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 // 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) _m.Called(key, value, ttl)
} }

View file

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