Simplify text embeddings cache to use only database cache
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:photos/core/cache/lru_map.dart';
|
||||
import 'package:photos/db/ml/db.dart';
|
||||
import 'package:photos/services/machine_learning/ml_computer.dart';
|
||||
import 'package:synchronized/synchronized.dart';
|
||||
@@ -9,7 +8,6 @@ import 'package:synchronized/synchronized.dart';
|
||||
class TextEmbeddingsCacheService {
|
||||
static final _logger = Logger('TextEmbeddingsCacheService');
|
||||
|
||||
LRUMap<String, List<double>> _memoryCache = LRUMap<String, List<double>>(50);
|
||||
final _cacheLock = Lock();
|
||||
|
||||
TextEmbeddingsCacheService._privateConstructor();
|
||||
@@ -17,36 +15,22 @@ class TextEmbeddingsCacheService {
|
||||
|
||||
Future<List<double>> getEmbedding(String query) async {
|
||||
return _cacheLock.synchronized(() async {
|
||||
// 1. Check memory cache
|
||||
final cachedResult = _memoryCache.get(query);
|
||||
if (cachedResult != null) {
|
||||
_logger.info('Text embedding cache hit (memory) for query');
|
||||
return cachedResult;
|
||||
}
|
||||
|
||||
// 2. Check database
|
||||
// 1. Check database cache
|
||||
final dbResult =
|
||||
await MLDataDB.instance.getRepeatedTextEmbeddingCache(query);
|
||||
if (dbResult != null) {
|
||||
_logger.info('Text embedding cache hit (database) for query');
|
||||
_memoryCache.put(query, dbResult);
|
||||
_logger.info('Text embedding cache hit for query');
|
||||
return dbResult;
|
||||
}
|
||||
|
||||
// 3. Compute new embedding
|
||||
// 2. Compute new embedding
|
||||
_logger.info('Computing new text embedding for query');
|
||||
final embedding = await MLComputer.instance.runClipText(query);
|
||||
|
||||
// 4. Store in both caches
|
||||
_memoryCache.put(query, embedding);
|
||||
// 3. Store in database cache
|
||||
await MLDataDB.instance.putRepeatedTextEmbeddingCache(query, embedding);
|
||||
|
||||
return embedding;
|
||||
});
|
||||
}
|
||||
|
||||
void clearMemoryCache() {
|
||||
_memoryCache = LRUMap<String, List<double>>(50);
|
||||
_logger.info('Cleared text embeddings memory cache');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user