Skip to content

Commit

Permalink
memcached library support (szepeviktor#46)
Browse files Browse the repository at this point in the history
PHP supports two memcached libraries: memcache and (the poorly named)
memcached.  w3tc only supports the memcache library extension.  Because
the memcache library is still not supported under  php7 this only leaves
the memcached library as the only option to get memcached daemon
support.  This update checks for and makes use of the memcached
extension library if available, and thus, providing php7 support for a
memcached server.
  • Loading branch information
amiga-500 authored and nigrosimone committed Sep 12, 2016
1 parent 3b1c6e4 commit a993431
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
9 changes: 9 additions & 0 deletions inc/lightbox/self_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@
<?php endif; ?>
</li>

<li>
<?php _e('Memcached extension:', 'w3-total-cache'); ?>
<?php if (class_exists('Memcached')): ?>
<code><?php _e('Installed', 'w3-total-cache'); ?></code>
<?php else: ?>
<code><?php _e('Not installed', 'w3-total-cache'); ?></code>
<?php endif; ?>
</li>

<li>
<?php _e('Redis Extension','w3-total-cache'); ?>
<?php if(class_exists('Redis')):?>
Expand Down
51 changes: 39 additions & 12 deletions lib/W3/Cache/Memcached.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,35 @@ class W3_Cache_Memcached extends W3_Cache_Base {
function __construct($config) {
parent::__construct($config);

$this->_memcache = new Memcache();
$isMemcached = class_exists('Memcached')?true:false;
$persistant = isset($config['persistant']) ? (boolean) $config['persistant'] : false;

if (!empty($config['servers'])) {
$persistant = isset($config['persistant']) ? (boolean) $config['persistant'] : false;
if ($isMemcached)
{
$this->_memcache = new Memcached($persistant?"persist":"");
$this->_memcache->setOption(Memcached::OPT_COMPRESSION, false);
}
else
$this->_memcache = new Memcache();

if (!empty($config['servers'])) {
foreach ((array) $config['servers'] as $server) {
if (substr($server, 0, 5) == 'unix:')
$this->_memcache->addServer(trim($server), 0, $persistant);
$this->_memcache->addServer(trim($server), 0, $isMemcached?0:$persistant);
else {
list($ip, $port) = explode(':', $server);
$this->_memcache->addServer(trim($ip), (integer) trim($port), $persistant);
$this->_memcache->addServer(trim($ip), (integer) trim($port), $isMemcached?0:$persistant);
}
}
} else {
return false;
}

if (!empty($config['compress_threshold'])) {
$this->_memcache->setCompressThreshold((integer) $config['compress_threshold']);
if ($isMemcached)
ini_set("memcached.compression_threshold", $config['compress_threshold']);
else
$this->_memcache->setCompressThreshold((integer) $config['compress_threshold']);
}

return true;
Expand Down Expand Up @@ -85,9 +95,13 @@ function set($key, $var, $expire = 0, $group = '') {
$key = $this->get_item_key($key);

$var['key_version'] = $this->_get_key_version($group);

if (class_exists('Memcached'))
$res = @$this->_memcache->set($key . '_' . $this->_blog_id, $var, $expire);
else
$res = @$this->_memcache->set($key . '_' . $this->_blog_id, $var, false, $expire);

return @$this->_memcache->set($key . '_' . $this->_blog_id, $var,
false, $expire);
return $res;
}

/**
Expand Down Expand Up @@ -124,7 +138,12 @@ function get_with_old($key, $group = '') {
$expires_at = isset($v['expires_at']) ? $v['expires_at'] : null;
if ($expires_at == null || time() > $expires_at) {
$v['expires_at'] = time() + 30;
@$this->_memcache->set($key . '_' . $this->_blog_id, $v, false, 0);

if (class_exists('Memcached'))
@$this->_memcache->set($key . '_' . $this->_blog_id, $v, 0);
else
@$this->_memcache->set($key . '_' . $this->_blog_id, $v, false, 0);

$has_old_data = true;

return array(null, $has_old_data);
Expand Down Expand Up @@ -161,7 +180,12 @@ function delete($key, $group = '') {
$v = @$this->_memcache->get($key . '_' . $this->_blog_id);
if (is_array($v)) {
$v['key_version'] = 0;
@$this->_memcache->set($key . '_' . $this->_blog_id, $v, false, 0);

if (class_exists('Memcached'))
@$this->_memcache->set($key . '_' . $this->_blog_id, $v, 0);
else
@$this->_memcache->set($key . '_' . $this->_blog_id, $v, false, 0);

return true;
}
}
Expand Down Expand Up @@ -196,7 +220,7 @@ function flush($group = '') {
* @return bool
*/
public function available() {
return class_exists('Memcache');
return class_exists('Memcache') || class_exists('Memcached');
}

/**
Expand All @@ -223,6 +247,9 @@ private function _get_key_version($group = '') {
* @return boolean
*/
private function _set_key_version($v, $group = '') {
@$this->_memcache->set($this->_get_key_version_key($group), $v, false, 0);
if (class_exists('Memcached'))
@$this->_memcache->set($this->_get_key_version_key($group), $v, 0);
else
@$this->_memcache->set($this->_get_key_version_key($group), $v, false, 0);
}
}
2 changes: 1 addition & 1 deletion lib/W3/UI/GeneralAdminView.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function view() {
$check_xcache = function_exists('xcache_set');
$check_wincache = function_exists('wincache_ucache_set');
$check_curl = function_exists('curl_init');
$check_memcached = class_exists('Memcache');
$check_memcached = class_exists('Memcache') || class_exists('Memcached');
$check_redis = class_exists('Redis');
$check_ftp = function_exists('ftp_connect');
$check_tidy = class_exists('tidy');
Expand Down

0 comments on commit a993431

Please sign in to comment.