symfony/ai-cache-store
Symfony AI Cache Store integrates a cache-backed vector store with Symfony AI Store, enabling lightweight storage and retrieval of embeddings using Symfony Cache. Ideal for development, testing, and small deployments where simplicity matters.
Install Dependencies:
composer require symfony/ai symfony/ai-cache-store predis/predis
(Use fruitcake/laravel-cache if not using Redis.)
Configure Cache Driver in .env:
CACHE_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
Bind the CacheStore in AppServiceProvider:
use Symfony\AI\Store\CacheStore;
use Symfony\Component\Cache\Adapter\RedisAdapter;
public function register()
{
$this->app->singleton(CacheStore::class, function ($app) {
$cache = new RedisAdapter();
return new CacheStore($cache);
});
}
First Usage Example (in a controller or command):
use Symfony\AI\Store\CacheStore;
$store = app(CacheStore::class);
// Insert a vector
$store->insert('user_123', [0.1, 0.2, 0.3]);
// Query vectors (exact match)
$results = $store->query([0.15, 0.25, 0.35]);
// Filtered query (requires v0.4.0+)
$results = $store->query([0.1, 0.2, 0.3], ['metadata' => ['user_id' => '123']]);
Verify with Tinker:
php artisan tinker
$store = app(CacheStore::class);
$store->insert('test_key', [0.5, 0.5, 0.5]);
$store->query([0.5, 0.5, 0.5]); // Should return the inserted vector
$store->insert('document_456', [0.7, 0.8, 0.9], [
'metadata' => ['author' => 'John Doe', 'tags' => ['ai', 'ml']]
]);
$results = $store->query([0.6, 0.7, 0.8]);
// Returns an array of keys matching the vector (if using exact-match logic).
$results = $store->query([0.1, 0.2, 0.3], [
'metadata' => ['tags' => ['ai']]
]);
$store->insertMany([
'doc_1' => [0.1, 0.2, 0.3],
'doc_2' => [0.4, 0.5, 0.6],
]);
$store->remove(['doc_1', 'doc_2']);
use Symfony\AI\Embedding\EmbeddingInterface;
// Assume $embeddingService generates embeddings
$embedding = $embeddingService->createFromText("Hello world");
// Store the embedding
$store->insert('text_hello_world', $embedding->getEmbedding());
// Later, retrieve and use in a query
$similarEmbeddings = $store->query($embedding->getEmbedding());
Facade for Cleaner Usage:
// app/Facades/VectorStore.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class VectorStore extends Facade {
protected static function getFacadeAccessor() {
return 'ai.cache_store';
}
}
Bind in AppServiceProvider:
$this->app->bind('ai.cache_store', function ($app) {
$cache = $app['cache']->driver();
return new \Symfony\AI\Store\CacheStore($cache);
});
Now use:
use App\Facades\VectorStore;
VectorStore::insert('key', [0.1, 0.2, 0.3]);
Queue Jobs for Async Operations:
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Symfony\AI\Store\CacheStore;
class StoreEmbeddingJob implements ShouldQueue {
use Queueable;
public function handle(CacheStore $store) {
$store->insert('job_key', $this->embedding);
}
}
Cache Tagging for Invalidation:
// Insert with tags
$store->insert('key', [0.1, 0.2, 0.3], ['tags' => ['user_123']]);
// Clear by tag (requires custom implementation)
$cache = $store->getCache();
$cache->deleteItemsMatchingTag('user_123');
TTL Management:
$store->insert('key', [0.1, 0.2, 0.3], [], 3600); // 1-hour TTL
Batch Processing:
$batch = [];
foreach ($documents as $doc) {
$batch['doc_' . $doc->id] = $doc->embedding;
}
$store->insertMany($batch);
Cache Driver Selection:
Unit Testing:
use Symfony\AI\Store\CacheStore;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
public function testInsertAndQuery() {
$cache = new ArrayAdapter();
$store = new CacheStore($cache);
$store->insert('test_key', [0.1, 0.2, 0.3]);
$results = $store->query([0.1, 0.2, 0.3]);
$this->assertContains('test_key', $results);
}
Integration Testing with Redis:
use Illuminate\Foundation\Testing\RefreshDatabase;
public function testRedisIntegration() {
$store = app(CacheStore::class);
$store->insert('redis_key', [0.4, 0.5, 0.6]);
$results = $store->query([0.4, 0.5, 0.6]);
$this->assertCount(1, $results);
}
Serialization Issues:
json_encode()/json_decode():
$serialized = json_encode([0.1, 0.2, 0.3]);
$store->insert('key', $serialized);
Cache Eviction:
CACHE_DRIVER=redis
REDIS_PERSISTENCE=appendonly
Exact-Match Queries Only:
How can I help you explore Laravel packages today?