feat(cache): implement cache for cuit info

This commit is contained in:
2025-04-06 21:54:26 -03:00
parent 48ad83bea5
commit ee1a597c9e

41
cache/cache.go vendored Normal file
View File

@@ -0,0 +1,41 @@
package cache
import (
"errors"
"fmt"
"os"
"time"
"github.com/gabdlr/api-cuit-go/utils"
)
const cacheDir = "./.cache"
func cacheIsOld(fPath string) bool {
fInfo, _ := os.Stat(fPath)
fCreationDate := fInfo.ModTime()
oneYearAgo := (time.Now()).AddDate(-1, 0, 0)
isOld := fCreationDate.Before(oneYearAgo)
if isOld {
os.Remove(fPath)
}
return isOld
}
func Search(cuit string) ([]byte, error) {
cuit = utils.StandardizeCuit(cuit)
fPath := fmt.Sprintf("%s/%s.json", cacheDir, cuit)
f, err := os.ReadFile(fPath)
if err == nil {
if cacheIsOld(fPath) {
return []byte{0}, errors.New("cached file expired")
}
return f, nil
}
return []byte{0}, err
}
func Save(cuit string, cuitInfo []byte) {
cuit = utils.StandardizeCuit(cuit)
os.WriteFile(fmt.Sprintf("%s/%s.json", cacheDir, cuit), cuitInfo, 0644)
}