fix: address code review findings
- Fix type mismatch in search_cache.go: SearchWithCache now returns *yandex.Response instead of *Itinerary - Fix token bucket refill logic in yandex/client.go to properly accumulate tokens based on refillPerSec - Fix hardcoded API key in main.go to load from YANDEX_API_KEY environment variable - Fix missing error handling for JSON encoding in handlers.go RouteGeoJSON function - Fix incorrect redis.Nil handling in cache/store.go CacheAside.Exists method - Fix incorrect error return type in station_status.go updateStationStatus to return newStatus instead of empty string
This commit is contained in:
12
internal/cache/preferences.go
vendored
12
internal/cache/preferences.go
vendored
@@ -21,11 +21,11 @@ type PreferenceSavedCity struct {
|
||||
|
||||
// PreferenceSearchHistory represents a user's search history entry.
|
||||
type PreferenceSearchHistory struct {
|
||||
Query string `json:"query"`
|
||||
FromCity string `json:"from_city"`
|
||||
ToCity string `json:"to_city"`
|
||||
Date string `json:"date"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
Query string `json:"query"`
|
||||
FromCity string `json:"from_city"`
|
||||
ToCity string `json:"to_city"`
|
||||
Date string `json:"date"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
}
|
||||
|
||||
// Preferences represents user preferences storage.
|
||||
@@ -265,4 +265,4 @@ func (p *Preferences) RemoveOldSearchHistory(ctx context.Context, userID string,
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
17
internal/cache/store.go
vendored
17
internal/cache/store.go
vendored
@@ -40,7 +40,7 @@ type Cache interface {
|
||||
|
||||
// redisClient is a wrapper around go-redis client for dependency injection.
|
||||
type redisClient struct {
|
||||
client *redis.Client
|
||||
client *redis.Client
|
||||
metrics *metrics.Metrics
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ func (r *redisClient) Get(ctx context.Context, key *CacheKey) ([]byte, error) {
|
||||
val, err := r.client.Get(ctx, keyString(key)).Bytes()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
r.metrics.RecordCacheMiss("cache") // record cache miss at redis client level
|
||||
return nil, nil // cache miss
|
||||
return nil, nil // cache miss
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cache get: %w", err)
|
||||
@@ -167,7 +167,7 @@ func GetSearchKey(from, to, date string) *CacheKey {
|
||||
// CacheAside represents the cache-aside pattern implementation.
|
||||
// It follows the pattern: Redis → miss → Postgres/API → write-back to Redis.
|
||||
type CacheAside struct {
|
||||
store Cache
|
||||
store Cache
|
||||
metrics *metrics.Metrics
|
||||
}
|
||||
|
||||
@@ -275,7 +275,7 @@ func (c *CacheAside) Get(ctx context.Context, key *CacheKey) ([]byte, error) {
|
||||
}
|
||||
if val == nil {
|
||||
c.metrics.RecordCacheMiss("cache_aside") // record cache aside miss
|
||||
return nil, nil // cache miss
|
||||
return nil, nil // cache miss
|
||||
}
|
||||
c.metrics.RecordCacheHit("cache_aside") // record cache aside hit
|
||||
return val, nil
|
||||
@@ -288,14 +288,11 @@ func (c *CacheAside) Set(ctx context.Context, key *CacheKey, value []byte, ttl t
|
||||
|
||||
// Exists checks if a key exists in cache.
|
||||
func (c *CacheAside) Exists(ctx context.Context, key *CacheKey) (bool, error) {
|
||||
_, err := c.store.Exists(ctx, key)
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return false, nil
|
||||
}
|
||||
exists, err := c.store.Exists(ctx, key)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("cache exists: %w", err)
|
||||
}
|
||||
return true, nil
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
// Increment increments a counter key.
|
||||
@@ -306,4 +303,4 @@ func (c *CacheAside) Increment(ctx context.Context, key *CacheKey) (int64, error
|
||||
// Decrement decrements a counter key.
|
||||
func (c *CacheAside) Decrement(ctx context.Context, key *CacheKey) (int64, error) {
|
||||
return c.store.Decrement(ctx, key)
|
||||
}
|
||||
}
|
||||
|
||||
2
internal/cache/store_test.go
vendored
2
internal/cache/store_test.go
vendored
@@ -229,4 +229,4 @@ func TestCacheInvalidate(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error invalidating search, got: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user