Skip to content

Commit

Permalink
Implement the new cache when fetching packages data
Browse files Browse the repository at this point in the history
  • Loading branch information
semanser committed Dec 17, 2024
1 parent ae50c73 commit 8c94ff1
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions pkg/sources/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package sources
import (
"context"
"fmt"
"time"

"github.com/depshubhq/depshub/pkg/sources/crates"
"github.com/depshubhq/depshub/pkg/sources/go"
"github.com/depshubhq/depshub/pkg/sources/npm"
"github.com/depshubhq/depshub/pkg/types"
Expand All @@ -28,11 +30,17 @@ func (f fetcher) Fetch(uniqueDependencies []types.Dependency) (types.PackagesInf
// Launch goroutines for concurrent fetching
npmSource := npm.NpmSource{}
goSource := gosource.GoSource{}
cratesSource := crates.CratesSource{}
background := context.Background()
activeRequests := 0

// Use a semaphore to limit concurrent requests
sem := make(chan struct{}, MaxConcurrent)
c, err := NewFileCache("dependencies")

if err != nil {
return nil, err
}

for _, dep := range uniqueDependencies {
activeRequests++
Expand All @@ -46,11 +54,29 @@ func (f fetcher) Fetch(uniqueDependencies []types.Dependency) (types.PackagesInf
var packageInfo types.Package
var err error

switch dep.Manager {
case types.Npm:
packageInfo, err = npmSource.FetchPackageData(background, dep.Name)
case types.Go:
packageInfo, err = goSource.FetchPackageData(dep.Name, dep.Version)
key := fmt.Sprintf("%d-%s", dep.Manager, dep.Name)

exists, err := c.Get(key, &packageInfo)

if err != nil {
fmt.Printf("Error getting cache: %s\n", err)
}

if !exists {
switch dep.Manager {
case types.Npm:
packageInfo, err = npmSource.FetchPackageData(background, dep.Name)
case types.Go:
packageInfo, err = goSource.FetchPackageData(dep.Name, dep.Version)
case types.Cargo:
packageInfo, err = cratesSource.FetchPackageData(background, dep.Name)
}

if err != nil {
fmt.Printf("Error fetching package data: %s\n", err)
} else {
c.Set(key, packageInfo, 24*time.Hour)
}
}

resultChan <- packageResult{
Expand Down

0 comments on commit 8c94ff1

Please sign in to comment.