func LoadSpotCacheOrNew()

in pkg/ec2pricing/spotpricing.go [57:88]


func LoadSpotCacheOrNew(ctx context.Context, ec2Client ec2.DescribeSpotPriceHistoryAPIClient, region string, fullRefreshTTL time.Duration, directoryPath string, days int) (*SpotPricing, error) {
	expandedDirPath, err := homedir.Expand(directoryPath)
	if err != nil {
		return nil, fmt.Errorf("unable to load spot pricing cache directory %s: %w", expandedDirPath, err)
	}
	spotPricing := &SpotPricing{
		Region:         region,
		FullRefreshTTL: fullRefreshTTL,
		DirectoryPath:  expandedDirPath,
		ec2Client:      ec2Client,
		cache:          cache.New(fullRefreshTTL, fullRefreshTTL),
		logger:         log.New(io.Discard, "", 0),
	}
	if fullRefreshTTL <= 0 {
		if err := spotPricing.Clear(); err != nil {
			return nil, err
		}
		return spotPricing, nil
	}
	gob.Register([]*spotPricingEntry{})
	// Start the cache refresh job
	go spotPricing.spotCacheRefreshJob(ctx, days)
	spotCache, err := loadSpotCacheFrom(fullRefreshTTL, region, expandedDirPath)
	if err != nil && !os.IsNotExist(err) {
		return nil, fmt.Errorf("a spot pricing cache file could not be loaded: %w", err)
	}
	if err != nil {
		spotCache = cache.New(0, 0)
	}
	spotPricing.cache = spotCache
	return spotPricing, nil
}