2026-01-29 15:54:22 +00:00
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
import 'package:crypto/crypto.dart';
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
|
import 'gemini_service.dart';
|
|
|
|
|
|
|
2026-04-11 23:13:18 +00:00
|
|
|
|
/// 画像ハッシュベースの AI 解析セッションキャッシュ
|
2026-01-29 15:54:22 +00:00
|
|
|
|
///
|
2026-04-11 23:13:18 +00:00
|
|
|
|
/// ## 設計方針
|
|
|
|
|
|
/// ディスクへの永続化(Hive)を行わず、アプリ起動中のみ有効なインメモリキャッシュを使用する。
|
2026-01-29 15:54:22 +00:00
|
|
|
|
///
|
2026-04-11 23:13:18 +00:00
|
|
|
|
/// ### なぜインメモリか
|
|
|
|
|
|
/// - カメラ撮影では毎回異なるファイルが生成されるため、実際にキャッシュがヒットする
|
|
|
|
|
|
/// ケースはごく限られる(同一セッション内でのドラフト二重処理など)
|
|
|
|
|
|
/// - 各ユーザーが自分の Gemini API キーを使うため、API コスト節約の恩恵が薄い
|
|
|
|
|
|
/// - 永続キャッシュは誤認識結果を最大30日間保持してしまうリスクがある
|
|
|
|
|
|
/// - 異なるエディション(獺祭23 vs 獺祭39 など)は異なる写真 = 異なるハッシュのため
|
|
|
|
|
|
/// 実質的にキャッシュの干渉は起きない
|
|
|
|
|
|
///
|
|
|
|
|
|
/// ### 効果が残るケース
|
|
|
|
|
|
/// - 同一セッション内で同じ画像ファイルを複数回解析しようとした場合の重複 API 防止
|
|
|
|
|
|
/// - アプリ再起動で常にクリアされるため、古い解析結果が残り続けることはない
|
2026-01-29 15:54:22 +00:00
|
|
|
|
class AnalysisCacheService {
|
2026-04-11 23:13:18 +00:00
|
|
|
|
// in-memory analysis cache: imageHash -> result
|
|
|
|
|
|
static final Map<String, SakeAnalysisResult> _cache = {};
|
2026-01-29 15:54:22 +00:00
|
|
|
|
|
2026-04-11 23:13:18 +00:00
|
|
|
|
// in-memory brand index: normalized brand name -> imageHash
|
|
|
|
|
|
static final Map<String, String> _brandIndex = {};
|
2026-01-29 15:54:22 +00:00
|
|
|
|
|
2026-04-11 23:13:18 +00:00
|
|
|
|
// ============================================================
|
|
|
|
|
|
// Hash computation
|
|
|
|
|
|
// ============================================================
|
2026-01-29 15:54:22 +00:00
|
|
|
|
|
2026-04-11 23:13:18 +00:00
|
|
|
|
/// 単一画像の SHA-256 ハッシュを計算
|
2026-01-29 15:54:22 +00:00
|
|
|
|
static Future<String> computeImageHash(String imagePath) async {
|
|
|
|
|
|
try {
|
|
|
|
|
|
final bytes = await File(imagePath).readAsBytes();
|
2026-04-11 23:13:18 +00:00
|
|
|
|
return sha256.convert(bytes).toString();
|
2026-01-29 15:54:22 +00:00
|
|
|
|
} catch (e) {
|
2026-04-11 22:25:24 +00:00
|
|
|
|
debugPrint('Hash computation failed: $e');
|
2026-04-11 23:13:18 +00:00
|
|
|
|
// ハッシュ計算失敗時はパスをそのまま使用(キャッシュなし扱い)
|
2026-01-29 15:54:22 +00:00
|
|
|
|
return imagePath;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-11 23:13:18 +00:00
|
|
|
|
/// 複数画像の結合ハッシュを計算(順序考慮)
|
2026-01-29 15:54:22 +00:00
|
|
|
|
static Future<String> computeCombinedHash(List<String> imagePaths) async {
|
|
|
|
|
|
if (imagePaths.isEmpty) return '';
|
|
|
|
|
|
if (imagePaths.length == 1) return computeImageHash(imagePaths.first);
|
|
|
|
|
|
|
|
|
|
|
|
final hashes = await Future.wait(
|
2026-04-11 23:13:18 +00:00
|
|
|
|
imagePaths.map(computeImageHash),
|
2026-01-29 15:54:22 +00:00
|
|
|
|
);
|
2026-04-11 23:13:18 +00:00
|
|
|
|
return sha256.convert(utf8.encode(hashes.join('_'))).toString();
|
2026-01-29 15:54:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-11 23:13:18 +00:00
|
|
|
|
// ============================================================
|
|
|
|
|
|
// Cache read / write
|
|
|
|
|
|
// ============================================================
|
2026-04-11 15:09:09 +00:00
|
|
|
|
|
2026-04-11 23:13:18 +00:00
|
|
|
|
/// キャッシュから取得(なければ null)
|
|
|
|
|
|
static Future<SakeAnalysisResult?> getCached(String imageHash) async {
|
|
|
|
|
|
final result = _cache[imageHash];
|
|
|
|
|
|
if (result != null) {
|
|
|
|
|
|
debugPrint('Cache HIT: ${result.name ?? 'Unknown'}');
|
2026-01-29 15:54:22 +00:00
|
|
|
|
}
|
2026-04-11 23:13:18 +00:00
|
|
|
|
return result;
|
2026-01-29 15:54:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// キャッシュに保存
|
2026-04-11 23:13:18 +00:00
|
|
|
|
static Future<void> saveCache(
|
|
|
|
|
|
String imageHash, SakeAnalysisResult result) async {
|
|
|
|
|
|
_cache[imageHash] = result;
|
|
|
|
|
|
debugPrint('Cache saved: ${result.name ?? 'Unknown'}');
|
2026-01-29 15:54:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-11 23:13:18 +00:00
|
|
|
|
/// キャッシュを全消去(開発メニュー用)
|
2026-01-29 15:54:22 +00:00
|
|
|
|
static Future<void> clearAll() async {
|
2026-04-11 23:13:18 +00:00
|
|
|
|
final count = _cache.length;
|
|
|
|
|
|
_cache.clear();
|
|
|
|
|
|
_brandIndex.clear();
|
2026-04-11 22:25:24 +00:00
|
|
|
|
debugPrint('Cache cleared ($count entries deleted)');
|
2026-01-29 15:54:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-11 23:13:18 +00:00
|
|
|
|
/// 現在のキャッシュ件数(開発メニュー表示用)
|
|
|
|
|
|
static Future<int> getCacheSize() async => _cache.length;
|
2026-01-29 15:54:22 +00:00
|
|
|
|
|
2026-04-11 23:13:18 +00:00
|
|
|
|
// ============================================================
|
|
|
|
|
|
// Brand index(銘柄名 → ハッシュ インメモリインデックス)
|
|
|
|
|
|
// ============================================================
|
2026-02-15 15:13:12 +00:00
|
|
|
|
|
|
|
|
|
|
/// 銘柄名をインデックスに登録
|
|
|
|
|
|
///
|
2026-04-11 15:09:09 +00:00
|
|
|
|
/// [forceUpdate] = true のとき(再解析など)は既存エントリを上書きする。
|
|
|
|
|
|
static Future<void> registerBrandIndex(
|
|
|
|
|
|
String? brandName,
|
|
|
|
|
|
String imageHash, {
|
|
|
|
|
|
bool forceUpdate = false,
|
|
|
|
|
|
}) async {
|
2026-02-15 15:13:12 +00:00
|
|
|
|
if (brandName == null || brandName.isEmpty) return;
|
|
|
|
|
|
|
2026-04-11 23:13:18 +00:00
|
|
|
|
final normalized = _normalizeBrandName(brandName);
|
|
|
|
|
|
final exists = _brandIndex.containsKey(normalized);
|
2026-02-15 15:13:12 +00:00
|
|
|
|
|
2026-04-11 23:13:18 +00:00
|
|
|
|
if (!exists || forceUpdate) {
|
|
|
|
|
|
_brandIndex[normalized] = imageHash;
|
|
|
|
|
|
debugPrint(
|
|
|
|
|
|
'Brand index ${exists ? "updated" : "registered"}: $brandName');
|
2026-02-15 15:13:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 銘柄名の正規化
|
|
|
|
|
|
///
|
2026-04-11 23:13:18 +00:00
|
|
|
|
/// 全角・半角スペースを除去し、ASCII 文字を小文字に統一する。
|
|
|
|
|
|
/// 日本語文字には大文字・小文字の区別がないため .toLowerCase() は ASCII のみに作用する。
|
2026-02-15 15:13:12 +00:00
|
|
|
|
static String _normalizeBrandName(String name) {
|
|
|
|
|
|
return name
|
2026-04-11 23:13:18 +00:00
|
|
|
|
.replaceAll(RegExp(r'\s+'), '')
|
|
|
|
|
|
.toLowerCase();
|
2026-02-15 15:13:12 +00:00
|
|
|
|
}
|
2026-01-29 15:54:22 +00:00
|
|
|
|
}
|