Improve caching, move mutex to the cache implementation
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/uptrace/bun"
|
||||
@@ -57,7 +56,6 @@ func NewConnLogs(ctx context.Context, db *bun.DB, cache Cache, logger *zap.Logge
|
||||
|
||||
type ConnLogs struct {
|
||||
db *bun.DB
|
||||
mu sync.RWMutex
|
||||
cache Cache
|
||||
lastTS time.Time
|
||||
}
|
||||
@@ -65,9 +63,6 @@ type ConnLogs struct {
|
||||
func (l *ConnLogs) fillCache(ctx context.Context) error {
|
||||
var entity []ConnLog
|
||||
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
|
||||
query := l.db.NewSelect().Model(&entity).
|
||||
Order("ts").
|
||||
Group("user_id", "ip_addr").
|
||||
@@ -82,22 +77,8 @@ func (l *ConnLogs) fillCache(ctx context.Context) error {
|
||||
return fmt.Errorf("select: %w", err)
|
||||
}
|
||||
|
||||
loop:
|
||||
for i := range entity {
|
||||
item := &entity[i]
|
||||
|
||||
if ips := l.cache.Get(item.UserID); len(ips) == 0 {
|
||||
l.cache.Append(item.UserID, item.IP)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, s := range l.cache.Get(item.UserID) {
|
||||
if s == item.IP {
|
||||
continue loop
|
||||
}
|
||||
}
|
||||
|
||||
l.cache.Append(item.UserID, item.IP)
|
||||
l.cache.Append(entity[i].UserID, entity[i].IP)
|
||||
}
|
||||
|
||||
if len(entity) > 0 {
|
||||
|
||||
Reference in New Issue
Block a user