feat: implement RAG-based chatbot service with daily usage rate limiting and background index worker
All checks were successful
Build and Release / release (push) Successful in 1m27s

This commit is contained in:
2026-05-06 10:02:00 +07:00
parent 76c5f55cdd
commit a61cf085ce
13 changed files with 442 additions and 38 deletions

View File

@@ -0,0 +1,55 @@
package repositories
import (
"context"
"fmt"
"history-api/pkg/cache"
"history-api/pkg/constants"
"time"
)
type UsageRepository interface {
GetAIUsage(ctx context.Context, userID string) (int, error)
IncrementAIUsage(ctx context.Context, userID string) (int, error)
}
type usageRepository struct {
c cache.Cache
}
func NewUsageRepository(c cache.Cache) UsageRepository {
return &usageRepository{
c: c,
}
}
func (r *usageRepository) getUsageKey(userID string) string {
dateStr := time.Now().Format("20060102")
return fmt.Sprintf("usage:ai:%s:%s", userID, dateStr)
}
func (r *usageRepository) GetAIUsage(ctx context.Context, userID string) (int, error) {
key := r.getUsageKey(userID)
var count int
err := r.c.Get(ctx, key, &count)
if err != nil {
return 0, nil
}
return count, nil
}
func (r *usageRepository) IncrementAIUsage(ctx context.Context, userID string) (int, error) {
key := r.getUsageKey(userID)
rdb := r.c.GetRawClient()
count, err := rdb.Incr(ctx, key).Result()
if err != nil {
return 0, err
}
if count == 1 {
rdb.Expire(ctx, key, constants.UsageExpiration)
}
return int(count), nil
}