You are here

class MobileDetectCache in Mobile Detect 7

Defines a Mobile Detect cache implementation.

To use this functionality, the following needs to be added to settings.php

$conf['cache_backends'][] = 'sites/all/modules/mobile_detect/mobile_detect_caching/mobile_detect_caching.inc';
$conf['cache_class_cache_page'] = 'MobileDetectCache';
$conf['mobile_detect_library'] = 'sites/all/libraries/Mobile_Detect/Mobile_Detect.php';

Hierarchy

Expanded class hierarchy of MobileDetectCache

File

mobile_detect_caching/mobile_detect_caching.inc, line 19
Cache backend include for the mobile_detect_caching module.

View source
class MobileDetectCache extends DrupalDatabaseCache implements DrupalCacheInterface {

  /**
   * {@inheritdoc}
   */
  function __construct($bin) {
    include_once DRUPAL_ROOT . '/' . variable_get('mobile_detect_library', 'sites/all/libraries/Mobile_Detect/Mobile_Detect.php');
    parent::__construct($bin);
  }

  /**
   * {@inheritdoc}
   */
  function set($cid, $data, $expire = CACHE_PERMANENT) {
    $cid = $this
      ->mobile_detect_caching_alter_cid() . $cid;
    parent::set($cid, $data, $expire);
  }

  /**
   * {@inheritdoc}
   */
  function getMultiple(&$cids) {
    if (is_array($cids)) {
      foreach ($cids as $key => $cid) {
        $cids[$key] = $this
          ->mobile_detect_caching_alter_cid() . $cid;
      }
    }
    return parent::getMultiple($cids);
  }

  /**
   * Determines a prefix for the page cache $cid based on devide type.
   *
   * @return string
   *   The $cid prefix
   */
  protected function mobile_detect_caching_alter_cid() {
    $device = '';
    if (class_exists('Mobile_Detect')) {
      try {
        $detect = new Mobile_Detect();
        if ($detect
          ->isMobile() && $detect
          ->isTablet() == FALSE) {
          $device = 'mobile:';
        }
        elseif ($detect
          ->isTablet()) {
          $device = 'tablet:';
        }
      } catch (Exception $e) {

        // nop
      }
    }
    return $device;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalDatabaseCache::$bin protected property
DrupalDatabaseCache::clear function Implements DrupalCacheInterface::clear(). Overrides DrupalCacheInterface::clear 1
DrupalDatabaseCache::garbageCollection protected function Garbage collection for get() and getMultiple().
DrupalDatabaseCache::get function Implements DrupalCacheInterface::get(). Overrides DrupalCacheInterface::get 1
DrupalDatabaseCache::isEmpty function Implements DrupalCacheInterface::isEmpty(). Overrides DrupalCacheInterface::isEmpty 1
DrupalDatabaseCache::isValidBin function Checks if $this->bin represents a valid cache table.
DrupalDatabaseCache::prepareItem protected function Prepares a cached item.
MobileDetectCache::getMultiple function Implements DrupalCacheInterface::getMultiple(). Overrides DrupalDatabaseCache::getMultiple
MobileDetectCache::mobile_detect_caching_alter_cid protected function Determines a prefix for the page cache $cid based on devide type.
MobileDetectCache::set function Implements DrupalCacheInterface::set(). Overrides DrupalDatabaseCache::set
MobileDetectCache::__construct function Constructs a DrupalDatabaseCache object. Overrides DrupalDatabaseCache::__construct