Skip to content

Commit

Permalink
Merge pull request #12 from pararang/feat/caching-with-goroutines
Browse files Browse the repository at this point in the history
feat: caching scraping result with goroutines
  • Loading branch information
pararang authored Aug 7, 2021
2 parents 74be0f1 + e731041 commit d52b030
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
32 changes: 25 additions & 7 deletions service/scraper/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,12 @@ func (s *scraper) GetProvinceAvailability(provinceID int) ([]model.HospitalSumma
data = append(data, *hospital)
})

err = redis.SetScrapedAvailableHospitals(url, data)
if err != nil {
log.Println("ERROR: Error set to redis")
}
go func() {
err = redis.SetScrapedAvailableHospitals(url, data)
if err != nil {
log.Println("ERROR: Error set to redis")
}
}()

return data, nil
}
Expand All @@ -134,14 +136,23 @@ func (s *scraper) GetHospitalDetail(hospitalCode string) (model.HospitalDetail,
}

var data model.HospitalDetail
var provinceID = hospitalCode[0:2]
var url = fmt.Sprintf("https://yankes.kemkes.go.id/app/siranap/tempat_tidur?kode_rs=%s&jenis=1&propinsi=%sprop&kabkota=", hospitalCode, provinceID)

provinceID := hospitalCode[0:2]

domHTML, err := s.readPage(fmt.Sprintf("https://yankes.kemkes.go.id/app/siranap/tempat_tidur?kode_rs=%s&jenis=1&propinsi=%sprop&kabkota=", hospitalCode, provinceID))
domHTML, err := s.readPage(url)
if err != nil {
return data, err
}

cachedData, _ := redis.GetScrapedDetailHospital(url)
if len(cachedData) > 0 {
err := json.Unmarshal([]byte(cachedData), &data)
if err == nil {
log.Printf("INFO: Return data from cached data")
return data, err
}
}

titleSelector := domHTML.Find("p[class=mb-0]").First()

data.Address = strings.TrimSpace(titleSelector.Find("small").First().Text())
Expand Down Expand Up @@ -189,6 +200,13 @@ func (s *scraper) GetHospitalDetail(hospitalCode string) (model.HospitalDetail,

data.Room = rooms

go func() {
err = redis.SetScrapedDetailHospital(url, data)
if err != nil {
log.Println("ERROR: Error set to redis")
}
}()

return data, nil
}

Expand Down
24 changes: 24 additions & 0 deletions storage/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ import (
type Redis interface {
Get(key string) (string, error)
Set(key string, value string) error
SetEx(key string, value string) error

SetScrapedAvailableHospitals(hashURL, hashHTML string, hospitals []model.HospitalSummary) error
GetScrapedAvailableHospitals(key string) (string, error)
SetScrapedDetailHospital(hashURL, hashHTML string, hospital model.HospitalDetail) error
GetScrapedDetailHospital(key string) (string, error)
}

type redis struct {
Expand Down Expand Up @@ -72,3 +76,23 @@ func buildKeyAvailableHospital(url string) string {
const prefix = "available_hospitals"
return prefix + "." + utils.GetMD5String(url)
}

// SetScrapedAvailableHospitals sets the scraped available hospitals
func (r redis) SetScrapedDetailHospital(url string, hospitals model.HospitalDetail) error {
var key = buildKeyDetailHospital(url)
var value = utils.JSONString(hospitals)
var expireTime = time.Duration(5 * 60 * time.Second) // TODO: set to env var

return r.SetEx(key, value, expireTime)
}

// GetScrapedAvailableHospitals sets the scraped available hospitals
func (r redis) GetScrapedDetailHospital(url string) (string, error) {
return r.Get(buildKeyDetailHospital(url))
}

// buildKeyAvailableHospital returns the key for the scraped available hospitals
func buildKeyDetailHospital(url string) string {
const prefix = "detail_hospital"
return prefix + "." + utils.GetMD5String(url)
}

1 comment on commit d52b030

@vercel
Copy link

@vercel vercel bot commented on d52b030 Aug 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.