-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #992 from rsteube/generic-cache
generic cache
- Loading branch information
Showing
5 changed files
with
116 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,30 @@ | ||
// Package cache provides cache keys | ||
package cache | ||
|
||
import ( | ||
"crypto/sha1" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// Key provides a cache key. | ||
type Key func() (string, error) | ||
"runtime" | ||
"time" | ||
|
||
// String creates a CacheKey for given strings. | ||
func String(s ...string) Key { | ||
return func() (string, error) { | ||
return strings.Join(s, "\n"), nil | ||
} | ||
} | ||
"github.com/rsteube/carapace/internal/cache" | ||
"github.com/rsteube/carapace/pkg/cache/key" | ||
) | ||
|
||
// FileChecksum creates a CacheKey for given file. | ||
func FileChecksum(file string) Key { | ||
return func() (checksum string, err error) { | ||
var content []byte | ||
if content, err = os.ReadFile(file); err == nil { | ||
checksum = fmt.Sprintf("%x", sha1.Sum(content)) | ||
// Cache caches a function for given duration and keys. | ||
func Cache(timeout time.Duration, keys ...key.Key) func(f func() ([]byte, error)) ([]byte, error) { | ||
return func(f func() ([]byte, error)) ([]byte, error) { | ||
_, file, line, _ := runtime.Caller(1) | ||
cacheFile, err := cache.File(file, line, keys...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return | ||
} | ||
} | ||
|
||
// FileStats creates a CacheKey for given file. | ||
func FileStats(file string) Key { | ||
return func() (checksum string, err error) { | ||
var path string | ||
if path, err = filepath.Abs(file); err == nil { | ||
var info os.FileInfo | ||
if info, err = os.Stat(file); err == nil { | ||
return String(path, strconv.FormatInt(info.Size(), 10), info.ModTime().String())() | ||
content, err := cache.Load(cacheFile, timeout) | ||
if err != nil { | ||
content, err = f() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return content, cache.Write(cacheFile, content) | ||
} | ||
return | ||
return content, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Package cache provides cache keys | ||
package key | ||
|
||
import ( | ||
"crypto/sha1" | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// Key provides a cache key. | ||
type Key func() (string, error) | ||
|
||
// String creates a CacheKey for given strings. | ||
func String(s ...string) Key { | ||
return func() (string, error) { | ||
return strings.Join(s, "\n"), nil | ||
} | ||
} | ||
|
||
// FileChecksum creates a CacheKey for given file. | ||
func FileChecksum(file string) Key { | ||
return func() (checksum string, err error) { | ||
var content []byte | ||
if content, err = os.ReadFile(file); err == nil { | ||
checksum = fmt.Sprintf("%x", sha1.Sum(content)) | ||
} | ||
return | ||
} | ||
} | ||
|
||
// FileStats creates a CacheKey for given file. | ||
func FileStats(file string) Key { | ||
return func() (checksum string, err error) { | ||
var path string | ||
if path, err = filepath.Abs(file); err == nil { | ||
var info os.FileInfo | ||
if info, err = os.Stat(file); err == nil { | ||
return String(path, strconv.FormatInt(info.Size(), 10), info.ModTime().String())() | ||
} | ||
} | ||
return | ||
} | ||
} | ||
|
||
func FolderStats(folder string) Key { | ||
return func() (string, error) { | ||
sums := make([]string, 0) | ||
err := filepath.Walk(folder, func(path string, info fs.FileInfo, err error) error { | ||
if !info.IsDir() { | ||
sum, err := String(info.Name(), strconv.FormatInt(info.Size(), 10), info.ModTime().String())() | ||
if err != nil { | ||
return err | ||
} | ||
sums = append(sums, sum) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return "", err | ||
} | ||
return strings.Join(sums, "\n"), nil | ||
} | ||
} |