Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuelef committed Sep 24, 2023
1 parent 006353e commit d9cfee1
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 68 deletions.
75 changes: 75 additions & 0 deletions cache/memory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package cache

import (
"sync"
"time"
)

// Cache represents an in-memory cache.
type Cache[T any] struct {
mu sync.Mutex
items map[string]CacheItem[T]
}

// CacheItem represents an item stored in the cache.
type CacheItem[T any] struct {
Value T
Expiration time.Time
}

// NewCache creates a new instance of the cache.
func NewCache[T any]() *Cache[T] {
return &Cache[T]{
items: make(map[string]CacheItem[T]),
}
}

// Set adds an item to the cache with a specified key and expiration time.
func (c *Cache[T]) Set(key string, value T, expiration time.Time) {
c.mu.Lock()
defer c.mu.Unlock()

c.items[key] = CacheItem[T]{
Value: value,
Expiration: expiration,
}
}

// Get retrieves an item from the cache by its key.
func (c *Cache[T]) Get(key string) (T, bool) {
c.mu.Lock()
defer c.mu.Unlock()

item, found := c.items[key]
if !found {
var zero T
return zero, false
}

if item.Expiration.Before(time.Now()) {
// Item has expired, remove it from the cache
delete(c.items, key)
var zero T
return zero, false
}

return item.Value, true
}

func (c *Cache[T]) Reset() {
c.mu.Lock()
defer c.mu.Unlock()
c.items = make(map[string]CacheItem[T])
}

func (c *Cache[T]) GetAllKeys() []string {
c.mu.Lock()
defer c.mu.Unlock()

keys := make([]string, 0, len(c.items))
for key := range c.items {
keys = append(keys, key)
}

return keys
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/emanuelef/gh-repo-stats-server
go 1.21.1

require (
github.com/emanuelef/github-repo-activity-stats v0.0.0-20230923193702-4ef241a871d4
github.com/emanuelef/github-repo-activity-stats v0.0.0-20230924005336-0dbcf3a83836
github.com/gofiber/contrib/otelfiber v1.0.10
github.com/gofiber/fiber/v2 v2.49.2
github.com/joho/godotenv v1.5.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqy
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emanuelef/github-repo-activity-stats v0.0.0-20230923193702-4ef241a871d4 h1:nuVxuqfiLrLv9MwKeQ632cqAK7Az+Hq72jXfsVqpuNo=
github.com/emanuelef/github-repo-activity-stats v0.0.0-20230923193702-4ef241a871d4/go.mod h1:a4gtH6JdkmHxydIwACgM0k7AgIHGaRGTv8jBkO8rjv0=
github.com/emanuelef/github-repo-activity-stats v0.0.0-20230924005336-0dbcf3a83836 h1:fTiEn+i+w7q5ISjW5DMvQlmvfIuNtVgKFlcVc9z8iEE=
github.com/emanuelef/github-repo-activity-stats v0.0.0-20230924005336-0dbcf3a83836/go.mod h1:a4gtH6JdkmHxydIwACgM0k7AgIHGaRGTv8jBkO8rjv0=
github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk=
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand Down
83 changes: 18 additions & 65 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sync"
"time"

"github.com/emanuelef/gh-repo-stats-server/cache"
"github.com/emanuelef/gh-repo-stats-server/otel_instrumentation"
"github.com/emanuelef/github-repo-activity-stats/repostats"
_ "github.com/joho/godotenv/autoload"
Expand Down Expand Up @@ -44,63 +45,6 @@ func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}

// Cache represents an in-memory cache.
type Cache[T any] struct {
mu sync.Mutex
items map[string]CacheItem[T]
}

// CacheItem represents an item stored in the cache.
type CacheItem[T any] struct {
Value T
Expiration time.Time
}

// NewCache creates a new instance of the cache.
func NewCache[T any]() *Cache[T] {
return &Cache[T]{
items: make(map[string]CacheItem[T]),
}
}

// Set adds an item to the cache with a specified key and expiration time.
func (c *Cache[T]) Set(key string, value T, expiration time.Time) {
c.mu.Lock()
defer c.mu.Unlock()

c.items[key] = CacheItem[T]{
Value: value,
Expiration: expiration,
}
}

// Get retrieves an item from the cache by its key.
func (c *Cache[T]) Get(key string) (T, bool) {
c.mu.Lock()
defer c.mu.Unlock()

item, found := c.items[key]
if !found {
var zero T
return zero, false
}

if item.Expiration.Before(time.Now()) {
// Item has expired, remove it from the cache
delete(c.items, key)
var zero T
return zero, false
}

return item.Value, true
}

func (c *Cache[T]) Reset() {
c.mu.Lock()
defer c.mu.Unlock()
c.items = make(map[string]CacheItem[T])
}

func main() {
ctx := context.Background()
tp, exp, err := otel_instrumentation.InitializeGlobalTracerProvider(ctx)
Expand All @@ -115,8 +59,8 @@ func main() {
log.Fatalf("failed to initialize OpenTelemetry: %e", err)
}

cache := NewCache[*repostats.RepoStats]()
cacheStars := NewCache[[]repostats.StarsPerDay]()
cacheOverall := cache.NewCache[*repostats.RepoStats]()
cacheStars := cache.NewCache[[]repostats.StarsPerDay]()

tokenSource := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("PAT")},
Expand Down Expand Up @@ -149,7 +93,7 @@ func main() {
return err
}

if res, hit := cache.Get(repo); hit {
if res, hit := cacheOverall.Get(repo); hit {
return c.JSON(res)
}

Expand All @@ -161,10 +105,14 @@ func main() {

nextDay := time.Now().UTC().Truncate(24 * time.Hour).Add(24 * time.Hour)

cache.Set(repo, result, nextDay)
cacheOverall.Set(repo, result, nextDay)
return c.JSON(result)
})

app.Get("/allStarsKeys", func(c *fiber.Ctx) error {
return c.JSON(cacheStars.GetAllKeys())
})

app.Get("/allStars", func(c *fiber.Ctx) error {
param := c.Query("repo")
repo, err := url.QueryUnescape(param)
Expand All @@ -183,17 +131,22 @@ func main() {
}

updateChannel := make(chan int)

var allStars []repostats.StarsPerDay
var wg sync.WaitGroup

wg.Add(1) // Increment the WaitGroup counter

go func() {
defer wg.Done()
allStars, _ = client.GetAllStarsHistory(ctx, repo, result.CreatedAt, updateChannel)
}()

for progress := range updateChannel {
fmt.Printf("Progress: %d\n", progress)
}

wg.Wait()

nextDay := time.Now().UTC().Truncate(24 * time.Hour).Add(24 * time.Hour)

cacheStars.Set(repo, allStars, nextDay)
Expand All @@ -219,8 +172,8 @@ func main() {
"tSys": bToMb(m.Sys),
"tNumGC": m.NumGC,
"goroutines": runtime.NumGoroutine(),
"cachesize": len(cache.items),
"cacheStars": len(cacheStars.items),
"cachesize": len(cacheOverall.GetAllKeys()),
"cacheStars": len(cacheStars.GetAllKeys()),
}

// percent, _ := cpu.Percent(time.Second, true)
Expand All @@ -230,7 +183,7 @@ func main() {
})

app.Post("/cleanAllCache", func(c *fiber.Ctx) error {
cache.Reset()
cacheOverall.Reset()
cacheStars.Reset()
return c.Send(nil)
})
Expand Down

0 comments on commit d9cfee1

Please sign in to comment.