change module name 22 очистка кэша при вставке и измении и удалении
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package linedb
|
package linedb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -94,6 +95,37 @@ func (c *RecordCache) Clear() {
|
|||||||
c.cache = make(map[string]*CacheEntry)
|
c.cache = make(map[string]*CacheEntry)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ClearCollection удаляет записи кэша, относящиеся к коллекции collectionName:
|
||||||
|
// ключи ReadByFilter ("collection:filter" и "collection_part:filter") и SetByRecord ("id:collection", "id:collection_part").
|
||||||
|
func (c *RecordCache) ClearCollection(collectionName string) {
|
||||||
|
if collectionName == "" {
|
||||||
|
c.Clear()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.mutex.Lock()
|
||||||
|
defer c.mutex.Unlock()
|
||||||
|
for key := range c.cache {
|
||||||
|
if cacheKeyBelongsToCollection(key, collectionName) {
|
||||||
|
delete(c.cache, key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func cacheKeyBelongsToCollection(key, collectionName string) bool {
|
||||||
|
if strings.HasPrefix(key, collectionName+":") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(key, collectionName+"_") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
idx := strings.LastIndex(key, ":")
|
||||||
|
if idx < 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
suf := key[idx+1:]
|
||||||
|
return suf == collectionName || strings.HasPrefix(suf, collectionName+"_")
|
||||||
|
}
|
||||||
|
|
||||||
// ClearEntriesContainingIDs удаляет из кэша только те записи, в данных которых
|
// ClearEntriesContainingIDs удаляет из кэша только те записи, в данных которых
|
||||||
// встречается хотя бы один из переданных id. Если ids пуст — ничего не делает.
|
// встречается хотя бы один из переданных id. Если ids пуст — ничего не делает.
|
||||||
func (c *RecordCache) ClearEntriesContainingIDs(ids []any) {
|
func (c *RecordCache) ClearEntriesContainingIDs(ids []any) {
|
||||||
|
|||||||
@@ -392,12 +392,7 @@ func (db *LineDb) Insert(data any, collectionName string, options LineDbAdapterO
|
|||||||
return fmt.Errorf("failed to write data: %w", err)
|
return fmt.Errorf("failed to write data: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Обновляем кэш
|
db.clearCacheForCollection(collectionName)
|
||||||
if db.cacheExternal != nil {
|
|
||||||
for _, item := range resultDataArray {
|
|
||||||
db.cacheExternal.UpdateCacheAfterInsert(item, collectionName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -530,10 +525,7 @@ func (db *LineDb) Update(data any, collectionName string, filter any, options Li
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if used {
|
if used {
|
||||||
if db.cacheExternal != nil {
|
db.clearCacheForCollection(collectionName)
|
||||||
ids := extractIDsFromRecords(result)
|
|
||||||
db.cacheExternal.ClearEntriesContainingIDs(ids)
|
|
||||||
}
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -552,10 +544,7 @@ func (db *LineDb) Update(data any, collectionName string, filter any, options Li
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if db.cacheExternal != nil {
|
db.clearCacheForCollection(collectionName)
|
||||||
ids := extractIDsFromRecords(result)
|
|
||||||
db.cacheExternal.ClearEntriesContainingIDs(ids)
|
|
||||||
}
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -596,10 +585,7 @@ func (db *LineDb) Delete(data any, collectionName string, options LineDbAdapterO
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if used {
|
if used {
|
||||||
if db.cacheExternal != nil {
|
db.clearCacheForCollection(collectionName)
|
||||||
ids := extractIDsFromRecords(result)
|
|
||||||
db.cacheExternal.ClearEntriesContainingIDs(ids)
|
|
||||||
}
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -618,10 +604,7 @@ func (db *LineDb) Delete(data any, collectionName string, options LineDbAdapterO
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if db.cacheExternal != nil {
|
db.clearCacheForCollection(collectionName)
|
||||||
ids := extractIDsFromRecords(result)
|
|
||||||
db.cacheExternal.ClearEntriesContainingIDs(ids)
|
|
||||||
}
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -758,15 +741,21 @@ func (db *LineDb) ClearCache(collectionName string, options LineDbAdapterOptions
|
|||||||
if collectionName == "" {
|
if collectionName == "" {
|
||||||
db.cacheExternal.Clear()
|
db.cacheExternal.Clear()
|
||||||
} else {
|
} else {
|
||||||
// Очищаем только записи для конкретной коллекции
|
db.cacheExternal.ClearCollection(collectionName)
|
||||||
// Это упрощенная реализация
|
|
||||||
db.cacheExternal.Clear()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// clearCacheForCollection сбрасывает кэш запросов по логической коллекции (включая ключи партиций base_*).
|
||||||
|
func (db *LineDb) clearCacheForCollection(collectionName string) {
|
||||||
|
if db.cacheExternal == nil || collectionName == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
db.cacheExternal.ClearCollection(collectionName)
|
||||||
|
}
|
||||||
|
|
||||||
// Close закрывает базу данных
|
// Close закрывает базу данных
|
||||||
func (db *LineDb) Close() {
|
func (db *LineDb) Close() {
|
||||||
db.mutex.Lock()
|
db.mutex.Lock()
|
||||||
@@ -1561,24 +1550,6 @@ func (db *LineDb) compareIDs(a, b any) bool {
|
|||||||
return a == b
|
return a == b
|
||||||
}
|
}
|
||||||
|
|
||||||
// extractIDsFromRecords извлекает id из списка записей (map[string]any).
|
|
||||||
func extractIDsFromRecords(records []any) []any {
|
|
||||||
if len(records) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
ids := make([]any, 0, len(records))
|
|
||||||
for _, rec := range records {
|
|
||||||
m, ok := rec.(map[string]any)
|
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if id, exists := m["id"]; exists && id != nil {
|
|
||||||
ids = append(ids, id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ids
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *LineDb) generateCacheKey(filter any, collectionName string) string {
|
func (db *LineDb) generateCacheKey(filter any, collectionName string) string {
|
||||||
// Упрощенная реализация генерации ключа кэша
|
// Упрощенная реализация генерации ключа кэша
|
||||||
return fmt.Sprintf("%s:%v", collectionName, filter)
|
return fmt.Sprintf("%s:%v", collectionName, filter)
|
||||||
@@ -1677,6 +1648,7 @@ func (db *LineDb) updatePartitioned(data any, collectionName string, filter any,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db.clearCacheForCollection(collectionName)
|
||||||
return allResults, nil
|
return allResults, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1710,6 +1682,7 @@ func (db *LineDb) deletePartitioned(data any, collectionName string, options Lin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db.clearCacheForCollection(collectionName)
|
||||||
return allResults, nil
|
return allResults, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user