Add an LRU cache for queries

This commit is contained in:
vishnukvmd 2023-12-18 12:54:54 +05:30
parent 073e2a06e6
commit d4ebb5c493

View file

@ -4,6 +4,7 @@ import "dart:math";
import "package:computer/computer.dart";
import "package:logging/logging.dart";
import "package:photos/core/cache/lru_map.dart";
import "package:photos/core/configuration.dart";
import "package:photos/core/event_bus.dart";
import "package:photos/db/files_db.dart";
@ -27,6 +28,7 @@ class SemanticSearchService {
static final SemanticSearchService instance =
SemanticSearchService._privateConstructor();
static final Computer _computer = Computer.shared();
static final LRUMap<String, List<double>> _queryCache = LRUMap(50);
static const kModelName = "clip";
static const kEmbeddingLength = 512;
@ -269,8 +271,13 @@ class SemanticSearchService {
Future<List<double>> _getTextEmbedding(String query) async {
_logger.info("Searching for " + query);
final cachedResult = _queryCache.get(query);
if (cachedResult != null) {
return cachedResult;
}
try {
final result = await _mlFramework.getTextEmbedding(query);
_queryCache.put(query, result);
return result;
} catch (e) {
_logger.severe("Could not get text embedding", e);