Skip to content

[V9.1˖] Configuring a Solr driver

Georges.L edited this page Mar 26, 2022 · 4 revisions

As of Phpfastcache 9.1 a new Solr driver support has been added 🎉

⚠️ Phpfastcache requires Solarium 6.x (or greater) to be installed as a composer dependency for the Solr driver.

The configuration is pretty simple, as always:

use Phpfastcache\Drivers\Solr\Driver as SolrDriver;
use Phpfastcache\Drivers\Solr\Config as SolrConfig;

/** @var SolrConfig $solrConfig */
$solrConfig = new SolrConfig();
$solrConfig->setCoreName('phpfastcache'); // Optional: Default value
$solrConfig->setPort(8983); // Optional: Default value
$solrConfig->setHost('127.0.0.1'); // Optional: Default value
$solrConfig->setPath('/'); // Optional: Default value
$solrConfig->setScheme('http'); // Optional: Default value



/**
 * Optional:
 *
 * You can change the mapping schema used by Phpfastcache.
 * The keys are the Phpfastcache internal index. All required.
 * The values are the name of your Solr schema.
 */
$solrConfig->setMappingSchema([
    SolrDriver::SOLR_DEFAULT_ID_FIELD => Driver::SOLR_DEFAULT_ID_FIELD,
    SolrDriver::SOLR_DISCRIMINATOR_FIELD => Driver::SOLR_DISCRIMINATOR_FIELD . '_s',
    ExtendedCacheItemPoolInterface::DRIVER_KEY_WRAPPER_INDEX => ExtendedCacheItemPoolInterface::DRIVER_KEY_WRAPPER_INDEX . '_s',
    ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX => ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX . '_s',
    ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX => ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX . '_s',
    ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX => ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX . '_s',
    ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX => ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX . '_s',
    TaggableCacheItemPoolInterface::DRIVER_TAGS_WRAPPER_INDEX => TaggableCacheItemPoolInterface::DRIVER_TAGS_WRAPPER_INDEX . '_ss',
]);

/**
 * Optional:
 *
 * You can change the PSR-14 event dispatcher service used (and required) by solarium, by your own one.
 */
// $solrConfig->setEventDispatcher($yourEventDispatcher);


$cacheInstance = CacheManager::getInstance('Solr', $solrConfig);

However keep in minds that Phpfastcache expects the following fields to be type-configured:

    SolrDriver::SOLR_DEFAULT_ID_FIELD => Expect type "string" field
    SolrDriver::SOLR_DISCRIMINATOR_FIELD => Expect type "string" field
    ExtendedCacheItemPoolInterface::DRIVER_KEY_WRAPPER_INDEX => Expect type "string" field
    ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX => Expect type "string" field
    ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX => Expect type "string" field
    ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX => Expect type "string" field
    ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX => Expect type "string" field
    TaggableCacheItemPoolInterface::DRIVER_TAGS_WRAPPER_INDEX => Expect type "strings" field

Please note the difference above between "string" and "strings" (array of strings).

However if you keep the default Solr dynamic fields, Phpfastcache will use them by default.

If you found a bug please feel free to open a new issue 😃

Clone this wiki locally