-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: minor goroutine leak * fix: minor goroutine leak * implement file cache for history data * implement DAG file cache * fix cache stale criteria * update go version to 1.20 * chore: update go version for gh action
- Loading branch information
Showing
17 changed files
with
173 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
steps: | ||
- name: "1" | ||
command: "sleep 1" | ||
command: "sleep 1000" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package filecache | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"sync" | ||
) | ||
|
||
type Cache[T any] struct { | ||
entries sync.Map | ||
} | ||
|
||
type Entry[T any] struct { | ||
Data T | ||
Size int64 | ||
LastModified int64 | ||
} | ||
|
||
func New[T any]() *Cache[T] { | ||
return &Cache[T]{} | ||
} | ||
|
||
func (c *Cache[T]) Store(fileName string, data T, fi os.FileInfo) { | ||
c.entries.Store(fileName, Entry[T]{Data: data, Size: fi.Size(), LastModified: fi.ModTime().Unix()}) | ||
} | ||
|
||
func (c *Cache[T]) Invalidate(fileName string) { | ||
c.entries.Delete(fileName) | ||
} | ||
|
||
func (c *Cache[T]) LoadLatest(fileName string, loader func() (T, error)) (T, error) { | ||
stale, lastModified, err := c.IsStale(fileName, c.Entry(fileName)) | ||
if err != nil { | ||
var zero T | ||
return zero, err | ||
} | ||
if stale { | ||
data, err := loader() | ||
if err != nil { | ||
var zero T | ||
return zero, err | ||
} | ||
c.Store(fileName, data, lastModified) | ||
return data, nil | ||
} | ||
item, _ := c.entries.Load(fileName) | ||
entry := item.(Entry[T]) | ||
return entry.Data, nil | ||
} | ||
|
||
func (c *Cache[T]) Entry(fileName string) Entry[T] { | ||
item, ok := c.entries.Load(fileName) | ||
if !ok { | ||
return Entry[T]{} | ||
} | ||
return item.(Entry[T]) | ||
} | ||
|
||
func (c *Cache[T]) Load(fileName string) (T, bool) { | ||
item, ok := c.entries.Load(fileName) | ||
if !ok { | ||
var zero T | ||
return zero, false | ||
} | ||
entry := item.(Entry[T]) | ||
return entry.Data, true | ||
} | ||
|
||
func (c *Cache[T]) IsStale(fileName string, entry Entry[T]) (bool, os.FileInfo, error) { | ||
fi, err := os.Stat(fileName) | ||
if err != nil { | ||
return true, fi, fmt.Errorf("failed to stat file %s: %w", fileName, err) | ||
} | ||
t := fi.ModTime().Unix() | ||
return entry.LastModified < t || entry.Size != fi.Size(), fi, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.