You are here

public function i18n_string_textgroup_cached::__destruct in Internationalization 7

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.

File

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

Class

i18n_string_textgroup_cached
Textgroup handler for i18n_string API which integrated persistent caching.

Code

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);
    }
  }
}