before refactor index store to complex file-line pattern

This commit is contained in:
2026-03-12 16:13:44 +06:00
parent 491ccbea89
commit 8ba956d8c5
21 changed files with 7804 additions and 57 deletions

View File

@@ -87,13 +87,71 @@ func (c *RecordCache) Delete(key string) {
delete(c.cache, key)
}
// Clear очищает кэш
// Clear очищает кэш полностью.
func (c *RecordCache) Clear() {
c.mutex.Lock()
defer c.mutex.Unlock()
c.cache = make(map[string]*CacheEntry)
}
// ClearEntriesContainingIDs удаляет из кэша только те записи, в данных которых
// встречается хотя бы один из переданных id. Если ids пуст — ничего не делает.
func (c *RecordCache) ClearEntriesContainingIDs(ids []any) {
if len(ids) == 0 {
return
}
c.mutex.Lock()
defer c.mutex.Unlock()
for key, entry := range c.cache {
if entry.Data == nil {
continue
}
records, ok := entry.Data.([]any)
if !ok {
continue
}
for _, rec := range records {
m, ok := rec.(map[string]any)
if !ok {
continue
}
recID := m["id"]
for _, id := range ids {
if idsEqual(recID, id) {
delete(c.cache, key)
goto nextKey
}
}
}
nextKey:
}
}
func idsEqual(a, b any) bool {
if a == b {
return true
}
// JSON unmarshals numbers as float64; сравниваем численно
if an, ok := toFloat64(a); ok {
if bn, ok := toFloat64(b); ok {
return an == bn
}
}
return false
}
func toFloat64(v any) (float64, bool) {
switch x := v.(type) {
case float64:
return x, true
case int:
return float64(x), true
case int64:
return float64(x), true
}
return 0, false
}
// Stop останавливает кэш
func (c *RecordCache) Stop() {
close(c.stopChan)