Caches AI enrichment results per distillery to save API calls: - New table: enrichment_cache (distillery, tags, hit_count) - New service: cache-enrichment.ts (get, save, increment, stats) - enrich-data.ts checks cache before AI query - Saves to cache after successful AI response - Returns cached: true/false flag for transparency Benefits: - 0 API cost for repeated distillery scans - Near-instant response for cached distilleries - Shared across all users
33 lines
1.2 KiB
SQL
33 lines
1.2 KiB
SQL
-- Enrichment Cache Table
|
|
-- Caches AI enrichment results per distillery to save API calls
|
|
|
|
CREATE TABLE IF NOT EXISTS enrichment_cache (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
distillery TEXT NOT NULL UNIQUE,
|
|
suggested_tags TEXT[],
|
|
suggested_custom_tags TEXT[],
|
|
search_string TEXT,
|
|
created_at TIMESTAMPTZ DEFAULT now(),
|
|
updated_at TIMESTAMPTZ DEFAULT now(),
|
|
hit_count INTEGER DEFAULT 0
|
|
);
|
|
|
|
-- Index for fast lookups
|
|
CREATE INDEX IF NOT EXISTS idx_enrichment_cache_distillery
|
|
ON enrichment_cache(distillery);
|
|
|
|
-- RLS: Allow all authenticated users to read (it's shared cache)
|
|
ALTER TABLE enrichment_cache ENABLE ROW LEVEL SECURITY;
|
|
|
|
DROP POLICY IF EXISTS "enrichment_cache_select" ON enrichment_cache;
|
|
CREATE POLICY "enrichment_cache_select" ON enrichment_cache
|
|
FOR SELECT TO authenticated USING (true);
|
|
|
|
DROP POLICY IF EXISTS "enrichment_cache_insert" ON enrichment_cache;
|
|
CREATE POLICY "enrichment_cache_insert" ON enrichment_cache
|
|
FOR INSERT TO authenticated WITH CHECK (true);
|
|
|
|
DROP POLICY IF EXISTS "enrichment_cache_update" ON enrichment_cache;
|
|
CREATE POLICY "enrichment_cache_update" ON enrichment_cache
|
|
FOR UPDATE TO authenticated USING (true);
|