<commit_before><?php
/**
 * @file
 * Drupal cache layer for the CultureFeed Search Service.
 */

use \CultuurNet\Search\Guzzle\Service;
use \CultuurNet\Auth\ConsumerCredentials;

/**
 * DrupalCultureFeedSearchService_Cache
 */
class DrupalCultureFeedSearchService_Cache extends DrupalCultureFeedSearchService {

  /**
   * @var Integer
   */
  protected $loggedInUserId;

  /**
   * @var \CultuurNet\Search\Guzzle\Service
   */
  protected $realSearchService;

  /**
   * Consumer.
   * @var \CultuurNet\Auth\ConsumerCredentials
   */
  protected $consumer;

  /**
   * Constructor.
   *
   * @param Service $realSearchService
   * @param ConsumerCredentials $consumerCredentials
   * @param Integer $loggedInUserId
   */
  public function __construct(Service $realSearchService, $consumerCredentials, $loggedInUserId) {
    $this->loggedInUserId = $loggedInUserId;
    $this->consumerCredentials = $consumerCredentials;
    $this->realSearchService = $realSearchService;
  }

  public function getRealSearchService() {
    return $this->realSearchService();
  }

  protected function getCachePrefix() {
    return 'culturefeed:results:';
  }

  protected function getCacheSuffix() {
    // Don't cache per user. Search is the same for every user. (for now)
    return '';
  }

  protected function getCacheCid($cid) {
    return self::getCachePrefix() . $cid . self::getCacheSuffix();
  }

  protected function cacheSet($cid, $data, $expires = CACHE_PERMANENT) {
    $cid = $this->getCacheCid($cid);
    cache_set($cid, $data, 'cache_culturefeed_search', $expires);
  }

  protected function cacheGet($cid) {
    $cid = $this->getCacheCid($cid);
    return cache_get($cid, 'cache_culturefeed_search');
  }

  protected function cacheClear($cid = NULL, $wildcard = FALSE) {
    cache_clear_all($cid, 'cache_culturefeed_search', $wildcard);
  }

  /**
   * @return ConsumerCredentials
   */
  protected function getConsumer() {
    $this->consumer;
  }

  /**
   * @see \CultuurNet\Search\Service::search().
   */
  public function search(Array $parameters = array()) {

    DrupalCultureFeedSearchService::addLanguageParameter($parameters);

    $cid = 'search:' . md5(serialize($parameters));
    if ($cache = $this->cacheGet($cid)) {
      $result = $cache->data;
      // Restore xml element.
      if ($result) {
        $xmlElement = new SimpleXMLElement($result->getXml(), 0, FALSE, \CultureFeed_Cdb_Default::CDB_SCHEME_URL);
        $result->setXmlElement($xmlElement);
      }
      return $result;
    }

    $result = $this->realSearchService->search($parameters);

    // Translate categories.
    DrupalCultureFeedSearchService::translateCategories($result);

    // Clear xml element because serialize doesn't work on simple xml.
    $result->setXmlElement(NULL);

    $this->cacheSet($cid, $result, REQUEST_TIME + CULTUREFEED_SEARCH_CACHE_EXPIRES);

    return $result;

  }

  /**
   * @see \CultuurNet\Search\Service::search().<commit_msg>Set back xml element after putting it in cache<commit_after><?php
/**
 * @file
 * Drupal cache layer for the CultureFeed Search Service.
 */

use \CultuurNet\Search\Guzzle\Service;
use \CultuurNet\Auth\ConsumerCredentials;

/**
 * DrupalCultureFeedSearchService_Cache
 */
class DrupalCultureFeedSearchService_Cache extends DrupalCultureFeedSearchService {

  /**
   * @var Integer
   */
  protected $loggedInUserId;

  /**
   * @var \CultuurNet\Search\Guzzle\Service
   */
  protected $realSearchService;

  /**
   * Consumer.
   * @var \CultuurNet\Auth\ConsumerCredentials
   */
  protected $consumer;

  /**
   * Constructor.
   *
   * @param Service $realSearchService
   * @param ConsumerCredentials $consumerCredentials
   * @param Integer $loggedInUserId
   */
  public function __construct(Service $realSearchService, $consumerCredentials, $loggedInUserId) {
    $this->loggedInUserId = $loggedInUserId;
    $this->consumerCredentials = $consumerCredentials;
    $this->realSearchService = $realSearchService;
  }

  public function getRealSearchService() {
    return $this->realSearchService();
  }

  protected function getCachePrefix() {
    return 'culturefeed:results:';
  }

  protected function getCacheSuffix() {
    // Don't cache per user. Search is the same for every user. (for now)
    return '';
  }

  protected function getCacheCid($cid) {
    return self::getCachePrefix() . $cid . self::getCacheSuffix();
  }

  protected function cacheSet($cid, $data, $expires = CACHE_PERMANENT) {
    $cid = $this->getCacheCid($cid);
    cache_set($cid, $data, 'cache_culturefeed_search', $expires);
  }

  protected function cacheGet($cid) {
    $cid = $this->getCacheCid($cid);
    return cache_get($cid, 'cache_culturefeed_search');
  }

  protected function cacheClear($cid = NULL, $wildcard = FALSE) {
    cache_clear_all($cid, 'cache_culturefeed_search', $wildcard);
  }

  /**
   * @return ConsumerCredentials
   */
  protected function getConsumer() {
    $this->consumer;
  }

  /**
   * @see \CultuurNet\Search\Service::search().
   */
  public function search(Array $parameters = array()) {

    DrupalCultureFeedSearchService::addLanguageParameter($parameters);

    $cid = 'search:' . md5(serialize($parameters));
    if ($cache = $this->cacheGet($cid)) {
      $result = $cache->data;
      // Restore xml element.
      if ($result) {
        $xmlElement = new SimpleXMLElement($result->getXml(), 0, FALSE, \CultureFeed_Cdb_Default::CDB_SCHEME_URL);
        $result->setXmlElement($xmlElement);
      }
      return $result;
    }

    $result = $this->realSearchService->search($parameters);

    // Translate categories.
    DrupalCultureFeedSearchService::translateCategories($result);

    // Clear xml element because serialize doesn't work on simple xml.
    $xmlElement = $result->getXmlElement();
    $result->setXmlElement(NULL);

    $this->cacheSet($cid, $result, REQUEST_TIME + CULTUREFEED_SEARCH_CACHE_EXPIRES);
    $result->setXmlElement($xmlElement);

    return $result;

  }

  /**
   * @see \CultuurNet\Search\Service::search().
{'fraction_non_alphanumeric': 0.1325481088254811, 'fraction_numerical': 0.0006635700066357001, 'mean_word_length': 3.781126090404441, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 13}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2371350', 'n_tokens_mistral': 1888, 'n_tokens_neox': 1881, 'n_words': 364}