Commit 7a58420c authored by idebr's avatar idebr Committed by bdragon
Browse files

Issue #2877947 by idebr, sathish.redcrackle, bonus, bdragon, tcmug,...

Issue #2877947 by idebr, sathish.redcrackle, bonus, bdragon, tcmug, damiankloip, guilopes: Apply Drupal coding standards (part 1)
parent 9a910f23
<?php
/**
* @file
* Install, update and uninstall hooks for the Memcache module.
*/
/**
* Implements hook_requirements().
*/
......
show_memcache_statistics: 0
\ No newline at end of file
show_memcache_statistics: 0
......@@ -8,4 +8,4 @@ core: 8.x
configure: memcache_admin.settings
dependancies:
- memcache
\ No newline at end of file
- memcache
......@@ -8,4 +8,4 @@ memcache_admin.reports:
title: Memcache statistics
description: View statistics for all configured memcache servers.
parent: system.admin_reports
route_name: memcache_admin.reports
\ No newline at end of file
route_name: memcache_admin.reports
......@@ -3,4 +3,4 @@ access memcache statistics:
access slab cachedump:
title: Access cachedump of memcache slab
restrict access: true
\ No newline at end of file
restrict access: true
......@@ -2,4 +2,4 @@ services:
memcache_admin.display_statistics:
class: Drupal\memcache_admin\EventSubscriber\MemcacheAdminSubscriber
tags:
- { name: event_subscriber }
\ No newline at end of file
- { name: event_subscriber }
<?php
/**
* @file
* Contains \Drupal\memcache_admin\Controller\MemcacheStatisticsController.
*/
namespace Drupal\memcache_admin\Controller;
use Drupal\Core\Controller\ControllerBase;
......@@ -21,6 +16,7 @@ class MemcacheStatisticsController extends ControllerBase {
* Callback for the Memcache Stats page.
*
* @param string $bin
* The bin name.
*
* @return string
* The page output.
......@@ -40,14 +36,14 @@ class MemcacheStatisticsController extends ControllerBase {
$additional_message = $this->t(
'@enable the memcache module',
[
'@enable' => l(t('enable'), 'admin/modules', ['fragment' => 'edit-modules-performance-and-scalability'])
'@enable' => l(t('enable'), 'admin/modules', ['fragment' => 'edit-modules-performance-and-scalability']),
]
);
if (module_exists('memcache')) {
$additional_message = $this->t(
'visit the Drupal admin @status page',
[
'@status' => l(t('status report'), 'admin/reports/status')
'@status' => l(t('status report'), 'admin/reports/status'),
]
);
}
......@@ -178,8 +174,11 @@ class MemcacheStatisticsController extends ControllerBase {
* Callback for the Memcache Stats page.
*
* @param string $cluster
* The Memcache cluster name.
* @param string $server
* The Memcache server name.
* @param string $type
* The type of statistics to retrieve when using the Memcache extension.
*
* @return string
* The page output.
......@@ -287,7 +286,7 @@ class MemcacheStatisticsController extends ControllerBase {
'@average' => number_format($average, 2),
'@sets' => number_format($sets, 2),
'@set' => number_format($stats['cmd_set']),
'@total' => number_format($stats['cmd_set'] + $stats['cmd_get'])
'@total' => number_format($stats['cmd_set'] + $stats['cmd_get']),
]
);
}
......@@ -317,7 +316,7 @@ class MemcacheStatisticsController extends ControllerBase {
'@percent_hit' => ($stats['cmd_get'] > 0 ? number_format($stats['get_hits'] / $stats['cmd_get'] * 100, 2) : '0.00'),
'@miss' => number_format($stats['get_misses']),
'@percent_miss' => ($stats['cmd_get'] > 0 ? number_format($stats['get_misses'] / $stats['cmd_get'] * 100, 2) : '0.00'),
'@total' => number_format($stats['cmd_get'])
'@total' => number_format($stats['cmd_get']),
]
);
}
......@@ -341,7 +340,7 @@ class MemcacheStatisticsController extends ControllerBase {
'@incr increments, @decr decrements',
[
'@incr' => number_format($stats['incr_hits'] + $stats['incr_misses']),
'@decr' => number_format($stats['decr_hits'] + $stats['decr_misses'])
'@decr' => number_format($stats['decr_hits'] + $stats['decr_misses']),
]
);
}
......@@ -361,7 +360,7 @@ class MemcacheStatisticsController extends ControllerBase {
[
'@to' => format_size((int) $stats['bytes_read']),
'@from' => format_size((int) $stats['bytes_written']),
'@written' => number_format($written, 2)
'@written' => number_format($written, 2),
]
);
}
......@@ -388,7 +387,7 @@ class MemcacheStatisticsController extends ControllerBase {
'@get' => number_format($get, 2),
'@set' => number_format($set, 2),
'@read' => format_size(number_format($read, 2)),
'@write' => format_size(number_format($write, 2))
'@write' => format_size(number_format($write, 2)),
]
);
}
......@@ -408,7 +407,7 @@ class MemcacheStatisticsController extends ControllerBase {
[
'@available' => format_size($stats['limit_maxbytes'] - $stats['bytes']),
'@percent' => number_format($percent, 2),
'@total' => format_size($stats['limit_maxbytes'])
'@total' => format_size($stats['limit_maxbytes']),
]
);
}
......@@ -552,4 +551,5 @@ class MemcacheStatisticsController extends ControllerBase {
return $output;
}
}
<?php
/**
* Memcache Admin event subscriber.
*/
namespace Drupal\memcache_admin\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
......@@ -31,29 +27,33 @@ class MemcacheAdminSubscriber implements EventSubscriberInterface {
public function display_statistics(FilterResponseEvent $event) {
$user = \Drupal::currentUser();
// Removed exclusion critiria, untested. Will likely need to add some of
// Removed exclusion criteria, untested. Will likely need to add some of
// these back in.
// @codingStandardsIgnoreStart
// strstr($_SERVER['PHP_SELF'], '/update.php')
// substr($_GET['q'], 0, strlen('batch')) == 'batch'
// strstr($_GET['q'], 'autocomplete')
// substr($_GET['q'], 0, strlen('system/files')) == 'system/files'
// in_array($_GET['q'], ['upload/js', 'admin/content/node-settings/rebuild'])
// @codingStandardsIgnoreEnd
// @todo validate these checks
if ($user->id() == 0) {
// suppress for the above criteria.
// Suppress for the above criteria.
}
else {
$response = $event->getResponse();
// Don't call theme() during shutdown if the registry has been rebuilt (such
// as when enabling/disabling modules on admin/build/modules) as things break.
// Don't call theme() during shutdown if the registry has been rebuilt
// (such as when enabling/disabling modules on admin/build/modules) as
// things break.
// Instead, simply exit without displaying admin statistics for this page
// load. See http://drupal.org/node/616282 for discussion.
// @todo make sure this is not still a requirement.
// @codingStandardsIgnoreStart
// if (!function_exists('theme_get_registry') || !theme_get_registry()) {
// return;
// }
// }.
// @codingStandardsIgnoreEnd
// Try not to break non-HTML pages.
if ($response instanceof HTMLResponse) {
......@@ -132,4 +132,5 @@ class MemcacheAdminSubscriber implements EventSubscriberInterface {
return $a / ($a + $b) * 100;
}
}
}
<?php
/**
* @file
* Contains \Drupal\memcache_admin\Form\MemcacheAdminSettingsForm.
*/
namespace Drupal\memcache_admin\Form;
use Drupal\Core\Form\ConfigFormBase;
......@@ -53,4 +48,5 @@ class MemcacheAdminSettingsForm extends ConfigFormBase {
parent::submitForm($form, $form_state);
}
}
<?php
/**
* @file
* Contains \Drupal\memcache\TimestampCacheTagsChecksum.
*/
namespace Drupal\memcache\Cache;
use Drupal\Core\Cache\CacheTagsChecksumInterface;
......@@ -12,14 +7,14 @@ use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\memcache\Invalidator\TimestampInvalidatorInterface;
/**
* Cache tags invalidations checksum implementation that uses timestamp invalidation.
* Cache tags invalidations checksum implementation by timestamp invalidation.
*/
class TimestampCacheTagsChecksum implements CacheTagsChecksumInterface, CacheTagsInvalidatorInterface {
/**
* The timestamp invalidator object.
*
* @var TimestampInvalidatorInterface
* @var \Drupal\memcache\Invalidator\TimestampInvalidatorInterface
*/
protected $invalidator;
......@@ -42,7 +37,8 @@ class TimestampCacheTagsChecksum implements CacheTagsChecksumInterface, CacheTag
/**
* Constructs a TimestampCacheTagsChecksum object.
*
* @param TimestampInvalidatorInterface $invalidator
* @param \Drupal\memcache\Invalidator\TimestampInvalidatorInterface $invalidator
* The timestamp invalidator object.
*/
public function __construct(TimestampInvalidatorInterface $invalidator) {
$this->invalidator = $invalidator;
......@@ -67,7 +63,7 @@ class TimestampCacheTagsChecksum implements CacheTagsChecksumInterface, CacheTag
* {@inheritdoc}
*/
public function getCurrentChecksum(array $tags) {
// @todo Revist the invalidatedTags hack.
// @todo Revisit the invalidatedTags hack.
// Remove tags that were already invalidated during this request from the
// static caches so that another invalidation can occur later in the same
// request. Without that, written cache items would not be invalidated
......@@ -135,8 +131,8 @@ class TimestampCacheTagsChecksum implements CacheTagsChecksumInterface, CacheTag
}
}
// The checksum is equal to the *most recent* invalidation of an applicable tag.
// If the item is untagged, the checksum is always 0.
// The checksum is equal to the *most recent* invalidation of an applicable
// tag. If the item is untagged, the checksum is always 0.
return max([0] + array_intersect_key($this->tagCache, array_flip($tags)));
}
......
......@@ -2,8 +2,9 @@
namespace Drupal\memcache\Connection;
use Drupal\memcache\Connection\MemcacheConnectionInterface;
/**
* Class MemcacheConnection.
*/
class MemcacheConnection implements MemcacheConnectionInterface {
/**
......@@ -21,7 +22,7 @@ class MemcacheConnection implements MemcacheConnectionInterface {
}
/**
* @{@inheritdoc}
* {@inheritdoc}
*/
public function addServer($server_path, $persistent = FALSE) {
list($host, $port) = explode(':', $server_path);
......@@ -57,10 +58,16 @@ class MemcacheConnection implements MemcacheConnectionInterface {
* Connects to a memcache server.
*
* @param string $host
* The server path without port.
* @param int $port
* The server port.
* @param bool $persistent
* Whether this server connection is persistent or not.
*
* @return bool|mixed
* @return \Memcache|bool
* A Memcache object for a successful persistent connection. TRUE for a
* successful non-persistent connection. FALSE when the server fails to
* connect.
*/
protected function connect($host, $port, $persistent) {
if ($persistent) {
......
......@@ -2,6 +2,9 @@
namespace Drupal\memcache\Connection;
/**
* Defines the Memcache connection interface.
*/
interface MemcacheConnectionInterface {
/**
......
......@@ -2,9 +2,11 @@
namespace Drupal\memcache\Connection;
use Drupal\memcache\Connection\MemcacheConnectionInterface;
use Drupal\memcache\MemcacheSettings;
/**
* Class MemcachedConnection.
*/
class MemcachedConnection implements MemcacheConnectionInterface {
/**
......@@ -50,7 +52,7 @@ class MemcachedConnection implements MemcacheConnectionInterface {
list($host, $port) = explode(':', $server_path);
if ($host == 'unix') {
// Memcached expects just the path to the socket without the protocol
// Memcached expects just the path to the socket without the protocol.
$host = substr($server_path, 7);
// Port is always 0 for unix sockets.
$port = 0;
......
......@@ -21,13 +21,13 @@ abstract class DriverBase implements DrupalMemcacheInterface {
/**
* The memcache object.
*
* @var mixed
* @var \Memcache|\Memcached
* E.g. \Memcache|\Memcached
*/
protected $memcache;
/**
* The hash algorithm to pass to hash(). Defaults to 'sha1'
* The hash algorithm to pass to hash(). Defaults to 'sha1'.
*
* @var string
*/
......@@ -51,11 +51,11 @@ abstract class DriverBase implements DrupalMemcacheInterface {
];
/**
* Constructs a DrupalMemcacheBase object.
* Constructs a DriverBase object.
*
* @param \Drupal\memcache\MemcacheSettings
* @param \Drupal\memcache\MemcacheSettings $settings
* The memcache config object.
* @param \Memcached|\Memcache $connection
* @param \Memcached|\Memcache $memcache
* An existing memcache connection object.
* @param string $bin
* The class instance specific cache bin to use.
......@@ -71,7 +71,6 @@ abstract class DriverBase implements DrupalMemcacheInterface {
$this->prefix = $prefix . ':';
}
if ($bin) {
$this->prefix .= $bin . ':';
}
......@@ -154,7 +153,7 @@ abstract class DriverBase implements DrupalMemcacheInterface {
// The stats_type can be over-loaded with an integer slab id, if doing a
// cachedump. We know we're doing a cachedump if $slab is non-zero.
$slab = (int) $stats_type;
$slab = (int) $stats_type;
$stats = [];
foreach ($this->get_bins() as $bin => $target) {
......@@ -290,9 +289,10 @@ abstract class DriverBase implements DrupalMemcacheInterface {
$user_access_checked = &$drupal_static_fast['user_access_checked'];
// Confirm DRUPAL_BOOTSTRAP_VARIABLES has been reached. We don't use
// drupal_get_bootstrap_phase() as it's buggy. We can use variable_get() here
// because _drupal_bootstrap_variables() includes module.inc immediately
// after it calls variable_initialize().
// drupal_get_bootstrap_phase() as it's buggy. We can use variable_get()
// here because _drupal_bootstrap_variables() includes module.inc
// immediately after it calls variable_initialize().
// @codingStandardsIgnoreStart
// if (!isset($variable_checked) && function_exists('module_list')) {
// $variable_checked = variable_get('show_memcache_statistics', FALSE);
// }
......@@ -302,7 +302,9 @@ abstract class DriverBase implements DrupalMemcacheInterface {
// // that the user has access to view them.
// $user_access_checked = user_access('access memcache statistics');
// }
// Return whether or not statistics are enabled and the user can access them.
// @codingStandardsIgnoreEnd
// Return whether or not statistics are enabled and the user can access
// them.
if ((!isset($variable_checked) || $variable_checked) && (!isset($user_access_checked) || $user_access_checked)) {
Timer::start('dmemcache');
return TRUE;
......@@ -349,4 +351,5 @@ abstract class DriverBase implements DrupalMemcacheInterface {
}
}
}
}
......@@ -20,37 +20,51 @@ class MemcacheDriverFactory {
protected $settings;
/**
* The connection class reference.
*
* @var string
*/
protected $connectionClass;
/**
* The driver class reference.
*
* @var string
*/
protected $driverClass;
/**
* Whether to connect to memcache using a persistent connection.
*
* @var bool
*/
protected $persistent;
/**
* An array of Memcache connections keyed by bin.
*
* @var \Drupal\memcache\Connection\MemcacheConnectionInterface[]
*/
protected $connections = [];
/**
* An array of configured servers.
*
* @var array
*/
protected $servers = [];
/**
* @var array
* An array of configured bins.
*
* @var string[]
*/
protected $bins = [];
/**
* @var array
* An array of failed connections to configured servers keyed by server name.
*
* @var bool[]
*/
protected $failedConnectionCache = [];
......@@ -58,6 +72,7 @@ class MemcacheDriverFactory {
* Constructs a MemcacheDriverFactory object.
*
* @param \Drupal\memcache\MemcacheSettings $settings
* The settings object.
*/
public function __construct(MemcacheSettings $settings) {
$this->settings = $settings;
......@@ -70,11 +85,10 @@ class MemcacheDriverFactory {
*
* @param string $bin
* The bin which is to be used.
*
* @param bool $flush
* Rebuild the bin/server/cache mapping.
*
* @return \Drupal\memcache\DrupalMemcacheInterface | bool
* @return \Drupal\memcache\DrupalMemcacheInterface|bool
* A Memcache object.
*/
public function get($bin = NULL, $flush = FALSE) {
......@@ -167,7 +181,7 @@ class MemcacheDriverFactory {
$this->driverClass = MemcacheDriver::class;
}
// Values from settings.php
// Values from settings.php.
$this->servers = $this->settings->get('servers', ['127.0.0.1:11211' => 'default']);
$this->bins = $this->settings->get('bins', ['default' => 'default']);
......
......@@ -59,7 +59,8 @@ class MemcachedDriver extends DriverBase {
if (PHP_MAJOR_VERSION === 7) {
$results = $this->memcache->getMulti($full_keys, \Memcached::GET_PRESERVE_ORDER);
} else {
}
else {
$cas_tokens = NULL;
$results = $this->memcache->getMulti($full_keys, $cas_tokens, \Memcached::GET_PRESERVE_ORDER);
}
......
<?php
/**
* @file
* Contains \Drupal\memcache\DrupalMemcacheInterface.
*/
namespace Drupal\memcache;
/**
......@@ -97,7 +92,7 @@ interface DrupalMemcacheInterface {
/**
* Immediately invalidates all existing items.
*
* flush doesn't actually free any resources, it only marks all the
* Flush doesn't actually free any resources, it only marks all the
* items as expired, so occupied memory will be overwritten by new items.
*
* @return bool
......
......@@ -10,6 +10,8 @@ use Drupal\memcache\Driver\MemcacheDriverFactory;
class MemcacheTimestampInvalidator extends TimestampInvalidatorBase {
/**
* A Memcache object.
*
* @var \Drupal\memcache\DrupalMemcacheInterface
*/
protected $memcache;
......@@ -17,9 +19,12 @@ class MemcacheTimestampInvalidator extends TimestampInvalidatorBase {
/**
* MemcacheTimestampInvalidator constructor.
*
* @param MemcacheDriverFactory $memcache_factory
* @param string $bin Memcache bin to store the timestamps in.
* @param float $tolerance Allowed clock skew between servers, in decimal seconds.
* @param \Drupal\memcache\Driver\MemcacheDriverFactory $memcache_factory
* Factory class for creation of Memcache objects.
* @param string $bin
* Memcache bin to store the timestamps in.
* @param float $tolerance
* Allowed clock skew between servers, in decimal seconds.
*/
public function __construct(MemcacheDriverFactory $memcache_factory, $bin, $tolerance = 0.001) {
parent::__construct($tolerance);
......@@ -43,7 +48,7 @@ class MemcacheTimestampInvalidator extends TimestampInvalidatorBase {
/**
* {@inheritdoc}
*/
public function getLastInvalidationTimestamps($tags) {
public function getLastInvalidationTimestamps(array $tags) {
return $this->memcache->getMulti($tags);
}
......@@ -53,4 +58,5 @@ class MemcacheTimestampInvalidator extends TimestampInvalidatorBase {
protected function writeTimestamp($tag, $timestamp) {
return $this->memcache->set($tag, $timestamp);
}
}
......@@ -3,7 +3,7 @@
namespace Drupal\memcache\Invalidator;
/**
* Class TimestampInvalidatorBase
* Class TimestampInvalidatorBase.
*
* Base class for timestamp-based tag invalidation.
*
......@@ -12,30 +12,34 @@ namespace Drupal\memcache\Invalidator;
abstract class TimestampInvalidatorBase implements TimestampInvalidatorInterface {
/**
* @var float Allowed timestamp slop.
* Allowed timestamp slop.
*
* @var float
*/
protected $tolerance;
/**
* TimestampInvalidatorBase constructor.
*
* @param float $tolerance Allowed clock skew between servers, in decimal seconds.
* @param float $tolerance
* Allowed clock skew between servers, in decimal seconds.
*/
function __construct($tolerance = 0.001) {
public function __construct($tolerance = 0.001) {
$this->tolerance = $tolerance;
}
/**
* Mark a tag as outdated.
*
* @param $tag string Tag to mark as outdated.
* @param string $tag
* Tag to mark as outdated.
*
* @return float New timestamp for tag.
* @return float
* New timestamp for tag.
*/
protected function markAsOutdated($tag) {
$now = $this->getCurrentTimestamp($this->tolerance);
$current = $this
->getLastInvalidationTimestamp($tag);
$current = $this->getLastInvalidationTimestamp($tag);
if ($now > $current) {
$this->writeTimestamp($tag, $now);
return $now;
......@@ -50,8 +54,8 @@ abstract class TimestampInvalidatorBase implements TimestampInvalidatorInterface
*/
public function getCurrentTimestamp($offset = 0.0) {
// @todo Eventually we might want to use a time service instead of microtime().
// Unfortunately, TimeInterface needs a request object and we don't have that