You are here

class i18n_string_textgroup_cached in Internationalization 7

Textgroup handler for i18n_string API which integrated persistent caching.

Hierarchy

Expanded class hierarchy of i18n_string_textgroup_cached

1 string reference to 'i18n_string_textgroup_cached'
i18n_field_i18n_string_info in i18n_field/i18n_field.i18n.inc
Implements hook_i18n_string_info().

File

i18n_string/i18n_string.inc, line 1365
API for internationalization strings

View source
class i18n_string_textgroup_cached extends i18n_string_textgroup_default {

  /**
   * Defines the timeout for the persistent caching.
   * @var int
   */
  public $caching_time = CACHE_TEMPORARY;

  /**
   * Extends the existing constructor with a cache handling.
   *
   * @param string $textgroup
   *   The name of this textgroup.
   */
  public function __construct($textgroup) {
    parent::__construct($textgroup);

    // Fetch persistent caches, the persistent caches contain only metadata.
    // Those metadata are processed by the related cache_get() methods.
    foreach (array(
      'cache_multiple',
      'strings',
    ) as $caches_type) {
      if (($cache = cache_get('i18n:string:tgroup:' . $this->textgroup . ':' . $caches_type)) && !empty($cache->data)) {
        $this->{$caches_type} = $cache->data;
      }
    }
  }

  /**
   * Class destructor.
   *
   * Updates the persistent caches for the next usage.
   * This function not only stores the data of the textgroup objects but also
   * of the string objects. That way we ensure that only cacheable string object
   * go into the persistent cache.
   */
  public function __destruct() {

    // Reduce size to cache by removing NULL values.
    $this->strings = array_filter($this->strings);
    $strings_to_cache = array();

    // Store the persistent caches. We just store the metadata the translations
    // are stored by the string object itself. However storing the metadata
    // reduces the number of DB queries executed during runtime.
    $cache_data = array();
    foreach ($this->strings as $context => $i18n_string_object) {
      $cache_data[$context] = $context;
      $strings_to_cache[$context] = $i18n_string_object;
    }
    cache_set('i18n:string:tgroup:' . $this->textgroup . ':strings', $cache_data, 'cache', $this->caching_time);
    $cache_data = array();
    foreach ($this->cache_multiple as $pattern => $strings) {
      foreach ($strings as $context => $i18n_string_object) {
        $cache_data[$pattern][$context] = $context;
        $strings_to_cache[$context] = $i18n_string_object;
      }
    }
    cache_set('i18n:string:tgroup:' . $this->textgroup . ':cache_multiple', $cache_data, 'cache', $this->caching_time);

    // Cache the string objects related to this textgroup.
    // Store only the public visible data into the persistent cache.
    foreach ($strings_to_cache as $i18n_string_object) {

      // If this isn't an object it's an unprocessed cache item and doesn't need
      // to be stored again.
      if (is_object($i18n_string_object)) {
        cache_set($i18n_string_object
          ->get_cid(), get_object_vars($i18n_string_object), 'cache', $this->caching_time);
      }
    }
  }

  /**
   * Reset cache, needed for tests.
   *
   * Takes care of the persistent caches.
   */
  public function cache_reset() {

    // Reset the persistent caches.
    cache_clear_all('i18n:string:tgroup:' . $this->textgroup, 'cache', TRUE);

    // Reset the complete string object cache too. This will affect string
    // objects of other textgroups as well.
    cache_clear_all('i18n:string:obj:', 'cache', TRUE);
    return parent::cache_reset();
  }

  /**
   * Get translation from cache.
   *
   * Extends the original handler with persistent caching.
   *
   * @param string $context
   *   The context to look out for.
   * @return i18n_string_object|NULL
   *   The string object if available or NULL otherwise.
   */
  protected function cache_get($context) {
    if (isset($this->strings[$context])) {

      // If the cache contains a string re-build i18n_string_object.
      if (is_string($this->strings[$context])) {
        $i8n_string_object = new i18n_string_object(array(
          'textgroup' => $this->textgroup,
        ));
        $i8n_string_object
          ->set_context($context);
        $this->strings[$context] = $i8n_string_object;
      }

      // Now run the original handling.
      return parent::cache_get($context);
    }
    return NULL;
  }

  /**
   * Get strings from multiple cache.
   *
   * @param $context array
   *   String context as array with language property at the end.
   *
   * @return mixed
   *   Array of strings (may be empty) if we've got a cache hit.
   *   Null otherwise.
   */
  protected function multiple_cache_get($context) {

    // Ensure the values from the persistent cache are properly re-build.
    $cache_key = implode(':', $context);
    if (isset($this->cache_multiple[$cache_key])) {
      foreach ($this->cache_multiple[$cache_key] as $cached_context) {
        if (is_string($cached_context)) {
          $i8n_string_object = new i18n_string_object(array(
            'textgroup' => $this->textgroup,
          ));
          $i8n_string_object
            ->set_context($cached_context);
          $this->cache_multiple[$cache_key][$cached_context] = $i8n_string_object;
        }
      }
    }
    else {

      // Now we try more generic keys. For instance, if we are searching 'term:1:*'
      // we may try too 'term:*:*' and filter out the results.
      foreach ($context as $key => $value) {
        if ($value != '*') {
          $try = array_merge($context, array(
            $key => '*',
          ));
          $cached_results = $this
            ->multiple_cache_get($try);

          // Now filter the ones that actually match.
          if (!empty($cached_results)) {
            $cached_results = $this
              ->string_filter($cached_results, $context);
          }
          return $cached_results;
        }
      }
    }
    return parent::multiple_cache_get($context);
  }
  public function string_update($i18nstring, $options = array()) {

    // Flush persistent cache.
    cache_clear_all($i18nstring
      ->get_cid(), 'cache', TRUE);
    return parent::string_update($i18nstring, $options);
  }
  public function string_remove($i18nstring, $options = array()) {

    // Flush persistent cache.
    cache_clear_all($i18nstring
      ->get_cid(), 'cache', TRUE);
    return parent::string_remove($i18nstring, $options);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
i18n_string_textgroup_cached::$caching_time public property Defines the timeout for the persistent caching.
i18n_string_textgroup_cached::cache_get protected function Get translation from cache. Overrides i18n_string_textgroup_default::cache_get
i18n_string_textgroup_cached::cache_reset public function Reset cache, needed for tests. Overrides i18n_string_textgroup_default::cache_reset
i18n_string_textgroup_cached::multiple_cache_get protected function Get strings from multiple cache. Overrides i18n_string_textgroup_default::multiple_cache_get
i18n_string_textgroup_cached::string_remove public function Remove string object. Overrides i18n_string_textgroup_default::string_remove
i18n_string_textgroup_cached::string_update public function Update / create / remove string. Overrides i18n_string_textgroup_default::string_update
i18n_string_textgroup_cached::__construct public function Extends the existing constructor with a cache handling. Overrides i18n_string_textgroup_default::__construct
i18n_string_textgroup_cached::__destruct public function Class destructor.
i18n_string_textgroup_default::$cache_multiple protected property
i18n_string_textgroup_default::$debug public property
i18n_string_textgroup_default::$strings public property
i18n_string_textgroup_default::$textgroup public property
i18n_string_textgroup_default::build_string public function Build string object
i18n_string_textgroup_default::cache_set protected function Set string object into cache
i18n_string_textgroup_default::context_remove public function Remove source and translations for user defined string.
i18n_string_textgroup_default::context_translate public function Translate source string
i18n_string_textgroup_default::context_update public function Update / create translation source for user defined strings.
i18n_string_textgroup_default::load_source public static function Load string source from db
i18n_string_textgroup_default::load_strings public function Load multiple strings.
i18n_string_textgroup_default::load_translation public static function Load translation from db 1
i18n_string_textgroup_default::multiple_cache_set protected function Set multiple cache.
i18n_string_textgroup_default::multiple_combine protected static function Build combinations of an array of arrays respecting keys.
i18n_string_textgroup_default::multiple_translate public function Translate array of source strings
i18n_string_textgroup_default::multiple_translation_build protected function Get multiple translations with search conditions.
i18n_string_textgroup_default::multiple_translation_load protected function Load multiple translations from db
i18n_string_textgroup_default::multiple_translation_search public function Search multiple translations with key combinations.
i18n_string_textgroup_default::save_source protected static function Save source string (create / update)
i18n_string_textgroup_default::save_string protected function Save / update string object
i18n_string_textgroup_default::save_translation protected function Save translation to the db
i18n_string_textgroup_default::string_add protected function Add source string to the locale tables for translation.
i18n_string_textgroup_default::string_check protected static function Check if string is ok for translation
i18n_string_textgroup_default::string_filter protected static function Filter array of strings
i18n_string_textgroup_default::string_query protected static function Build query for i18n_string table
i18n_string_textgroup_default::string_translate protected function Translate string object
i18n_string_textgroup_default::update_check public function Recheck strings after update
i18n_string_textgroup_default::update_translation function Update string translation, only if source exists.