in pkg/ec2pricing/odpricing.go [87:117]
func LoadODCacheOrNew(ctx context.Context, pricingClient pricing.GetProductsAPIClient, region string, fullRefreshTTL time.Duration, directoryPath string) (*OnDemandPricing, error) {
expandedDirPath, err := homedir.Expand(directoryPath)
if err != nil {
return nil, fmt.Errorf("unable to load on-demand pricing cache directory %s: %w", expandedDirPath, err)
}
odPricing := &OnDemandPricing{
Region: region,
FullRefreshTTL: fullRefreshTTL,
DirectoryPath: expandedDirPath,
pricingClient: pricingClient,
cache: cache.New(fullRefreshTTL, fullRefreshTTL),
logger: log.New(io.Discard, "", 0),
}
if fullRefreshTTL <= 0 {
if err := odPricing.Clear(); err != nil {
return nil, fmt.Errorf("unable to clear od pricing cache due to ttl <= 0 %w", err)
}
return odPricing, nil
}
// Start the cache refresh job
go odPricing.odCacheRefreshJob(ctx)
odCache, err := loadODCacheFrom(fullRefreshTTL, region, expandedDirPath)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("an on-demand pricing cache file could not be loaded: %v", err)
}
if err != nil {
odCache = cache.New(0, 0)
}
odPricing.cache = odCache
return odPricing, nil
}