Skip to content

Commit

Permalink
code(pkg/references) - provide more flexible references
Browse files Browse the repository at this point in the history
  • Loading branch information
PxyUp committed Jan 25, 2024
1 parent ca3c0a7 commit ebafa37
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ type Reference struct {
```

- [ModelField](#model-field) - is embedded struct, you can use same fields
- Expire[sec] - duration when reference is expired after fetching (0 means no expired)
- Expire[sec] - duration when reference is expired after fetching. Not set => **forever cached**. Set to 0 => **every time re-fetch**. Set to n > 0 => **cached for n second**

For [Fitter](#how-to-use-fitter)
```go
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type RefMap map[string]*Reference
type Reference struct {
*ModelField

Expire uint32 `yaml:"expire" json:"expire"`
Expire *uint32 `yaml:"expire" json:"expire"`
}

type Limits struct {
Expand Down
13 changes: 9 additions & 4 deletions pkg/references/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,18 @@ func getValueFromFetcher(fetcher refFetcher, name string, cfg *config.ModelField
return res
}

func getExpireTime(expire uint32) *time.Time {
if expire > 0 {
expireTime := time.Now().Add(time.Nanosecond * time.Duration(expire))
func getExpireTime(expire *uint32) *time.Time {
if expire == nil {
return nil
}

if *expire > 0 {
expireTime := time.Now().Add(time.Nanosecond * time.Duration(*expire))
return &expireTime
}

return nil
zero := time.Time{}
return &zero
}

func Get(name string) builder.Jsonable {
Expand Down

0 comments on commit ebafa37

Please sign in to comment.