Skip to content

Commit

Permalink
Format Last-Modified timestamp in cache.Set() (#112)
Browse files Browse the repository at this point in the history
Remove locking from Cache struct. It is not needed.
  • Loading branch information
lucasrod16 authored Jan 19, 2025
1 parent 30c19e4 commit ee444de
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
13 changes: 4 additions & 9 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"io"
"log"
"sync"
"net/http"
"time"

"cloud.google.com/go/storage"
Expand All @@ -18,24 +18,19 @@ const (

type Cache struct {
data []byte
timestamp time.Time
mu sync.RWMutex
timestamp string
}

func New() *Cache {
return &Cache{}
}

func (c *Cache) Set(data []byte) {
c.mu.Lock()
defer c.mu.Unlock()
c.data = data
c.timestamp = time.Now().UTC()
c.timestamp = time.Now().UTC().Format(http.TimeFormat)
}

func (c *Cache) Get() ([]byte, time.Time) {
c.mu.RLock()
defer c.mu.RUnlock()
func (c *Cache) Get() ([]byte, string) {
return c.data, c.timestamp
}

Expand Down
3 changes: 2 additions & 1 deletion internal/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func TestCache(t *testing.T) {
data, timestamp := cache.Get()

require.Equal(t, tt.expected, data)
require.False(t, timestamp.IsZero(), "timestamp should not be empty")
require.NotEmpty(t, timestamp)
require.Contains(t, timestamp, "GMT")
})
}
}
2 changes: 1 addition & 1 deletion internal/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func GetRepos(c *cache.Cache) http.HandlerFunc {
}

w.Header().Set("Content-Type", "application/json")
w.Header().Set("Last-Modified", timestamp.Format(http.TimeFormat))
w.Header().Set("Last-Modified", timestamp)

if r.Method == http.MethodHead {
w.WriteHeader(http.StatusOK)
Expand Down

0 comments on commit ee444de

Please sign in to comment.