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
- class \DrupalDatabaseCache implements DrupalCacheInterface
- class \MobileDetectCache implements DrupalCacheInterface
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;
}
}