Skip to content

Commit

Permalink
added caching responses into worker;
Browse files Browse the repository at this point in the history
  • Loading branch information
jacekk committed Aug 18, 2019
1 parent 1f6b8f0 commit 9075375
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
/go.sum
/main

/cache/*.*
!/cache/.gitkeep
/storage/*.json
!/storage/*.dist.json
Empty file added cache/.gitkeep
Empty file.
14 changes: 13 additions & 1 deletion pkg/storage/cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package storage

import "github.com/pkg/errors"
import (
"path"

"github.com/jacekk/dead-simple-proxy-server/pkg/helpers"
"github.com/pkg/errors"
)

// GetCached -
func GetCached() (Items, error) {
Expand All @@ -18,3 +23,10 @@ func GetCached() (Items, error) {

return cached, nil
}

// SlugCachePath -
func SlugCachePath(slug string) string {
baseDir := helpers.GetProjectDir()

return path.Join(baseDir, "cache", slug+".txt")
}
23 changes: 22 additions & 1 deletion pkg/worker/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package worker

import (
"math/rand"
"net/http"
"os"
"time"

"github.com/jacekk/dead-simple-proxy-server/pkg/storage"
Expand Down Expand Up @@ -35,7 +37,26 @@ func refreshRandomCache(loggr *loggerImpl) error {
}

func refreshConfigItem(loggr *loggerImpl, item storage.Item) error {
loggr.Info("Refreshing '%s'", item.ID)
loggr.Info("Refreshing '%s' ...", item.ID)
resp, err := http.Get(item.URL)
if err != nil {
return errors.Wrap(err, "failed making get request")
}
defer resp.Body.Close()

cachePath := storage.SlugCachePath(item.ID)
cache, err := os.Create(cachePath)
if err != nil {
return errors.Wrap(err, "failed creating cache")
}
defer cache.Close()

err = resp.Write(cache)
if err != nil {
return errors.Wrap(err, "failed writing to cache")
}

loggr.Info("Refreshed '%s'", item.ID)

return nil
}

0 comments on commit 9075375

Please sign in to comment.