Skip to content

Commit

Permalink
Apply changes from PHP CS Fixer with PSR2
Browse files Browse the repository at this point in the history
  • Loading branch information
colinmollenhour committed Jan 18, 2023
1 parent d87ca37 commit dd4fe9b
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 249 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.basedir
vendor
composer.lock
/.php-cs-fixer.cache
331 changes: 160 additions & 171 deletions Cm/Cache/Backend/Redis.php

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"colinmollenhour/credis": "^1.14"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.4",
"phpunit/phpunit": "^9",
"zf1s/zend-cache": "~1.15"
},
Expand All @@ -22,6 +23,7 @@
]
},
"scripts": {
"test": "vendor/bin/phpunit tests"
"test": "vendor/bin/phpunit tests",
"php-cs-fixer": "php-cs-fixer --rules=@PSR2"
}
}
119 changes: 65 additions & 54 deletions stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
$db = 0;
$limit = 20;
array_shift($argv);
while($arg = array_shift($argv)) {
switch($arg) {
case '--db': $db = intval(array_shift($argv)); break;
case '--server': $server = array_shift($argv); break;
case '--limit': $limit = array_shift($argv); break;
default: die("Unrecognized argument '$arg'.\nUsage: path/to/stats.php [--server tcp://127.0.0.1:6378] [--db 0] [--limit 20]\n");
}
while ($arg = array_shift($argv)) {
switch($arg) {
case '--db': $db = intval(array_shift($argv));
break;
case '--server': $server = array_shift($argv);
break;
case '--limit': $limit = array_shift($argv);
break;
default: die("Unrecognized argument '$arg'.\nUsage: path/to/stats.php [--server tcp://127.0.0.1:6378] [--db 0] [--limit 20]\n");
}
}

require __DIR__.'/lib/Credis/Client.php';
Expand All @@ -23,59 +26,67 @@
$client->select($db);

$tagStats = array();
foreach($client->sMembers(Cm_Cache_Backend_Redis::SET_TAGS) as $tag) {
if (preg_match('/^\w{3}_MAGE$/', $tag)) continue;
$ids = $client->sMembers(Cm_Cache_Backend_Redis::PREFIX_TAG_IDS . $tag);
$tagSizes = array();
$missing = 0;
foreach ($ids as $id) {
$data = $client->hGet(Cm_Cache_Backend_Redis::PREFIX_KEY.$id, Cm_Cache_Backend_Redis::FIELD_DATA);
$size = strlen($data);
if ($size) $tagSizes[] = $size;
else $missing++;
}
if ($tagSizes) {
$tagStats[$tag] = array(
'count' => count($tagSizes),
'min' => min($tagSizes),
'max' => max($tagSizes),
'avg size' => array_sum($tagSizes) / count($tagSizes),
'total size' => array_sum($tagSizes),
'missing' => $missing,
);
}
foreach ($client->sMembers(Cm_Cache_Backend_Redis::SET_TAGS) as $tag) {
if (preg_match('/^\w{3}_MAGE$/', $tag)) {
continue;
}
$ids = $client->sMembers(Cm_Cache_Backend_Redis::PREFIX_TAG_IDS . $tag);
$tagSizes = array();
$missing = 0;
foreach ($ids as $id) {
$data = $client->hGet(Cm_Cache_Backend_Redis::PREFIX_KEY.$id, Cm_Cache_Backend_Redis::FIELD_DATA);
$size = strlen($data);
if ($size) {
$tagSizes[] = $size;
} else {
$missing++;
}
}
if ($tagSizes) {
$tagStats[$tag] = array(
'count' => count($tagSizes),
'min' => min($tagSizes),
'max' => max($tagSizes),
'avg size' => array_sum($tagSizes) / count($tagSizes),
'total size' => array_sum($tagSizes),
'missing' => $missing,
);
}
}

function _format_bytes($a_bytes)
{
if ($a_bytes < 1024) {
return $a_bytes .' B';
} elseif ($a_bytes < 1048576) {
return round($a_bytes / 1024, 4) .' KB';
} else {
return round($a_bytes / 1048576, 4) . ' MB';
}
if ($a_bytes < 1024) {
return $a_bytes .' B';
} elseif ($a_bytes < 1048576) {
return round($a_bytes / 1024, 4) .' KB';
} else {
return round($a_bytes / 1048576, 4) . ' MB';
}
}

function printStats($data, $key, $limit) {
echo "Top $limit tags by ".ucwords($key)."\n";
echo "------------------------------------------------------------------------------------\n";
$sort = array();
foreach ($data as $tag => $stats) {
$sort[$tag] = $stats[$key];
}
array_multisort($sort, SORT_DESC, $data);
$i = 0;
$fmt = "%-40s| %-8s| %-15s| %-15s\n";
printf($fmt, 'Tag', 'Count', 'Avg Size', 'Total Size');
foreach ($data as $tag => $stats) {
$tag = substr($tag, 4);
if (++$i > $limit) break;
$avg = _format_bytes($stats['avg size']);
$total = _format_bytes($stats['total size']);
printf($fmt, $tag, $stats['count'], $avg, $total);
}
echo "\n";
function printStats($data, $key, $limit)
{
echo "Top $limit tags by ".ucwords($key)."\n";
echo "------------------------------------------------------------------------------------\n";
$sort = array();
foreach ($data as $tag => $stats) {
$sort[$tag] = $stats[$key];
}
array_multisort($sort, SORT_DESC, $data);
$i = 0;
$fmt = "%-40s| %-8s| %-15s| %-15s\n";
printf($fmt, 'Tag', 'Count', 'Avg Size', 'Total Size');
foreach ($data as $tag => $stats) {
$tag = substr($tag, 4);
if (++$i > $limit) {
break;
}
$avg = _format_bytes($stats['avg size']);
$total = _format_bytes($stats['total size']);
printf($fmt, $tag, $stats['count'], $avg, $total);
}
echo "\n";
}

// Top 20 by total size
Expand Down
7 changes: 2 additions & 5 deletions tests/CommonBackendTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

abstract class CommonBackendTest extends TestCase {

abstract class CommonBackendTest extends TestCase
{
protected Cm_Cache_Backend_Redis $_instance;
protected $_className;

Expand Down Expand Up @@ -197,7 +197,4 @@ public function testCleanModeNotMatchingTags3(): void
$this->assertTrue($this->_instance->test('bar2') > 999999);
$this->assertFalse($this->_instance->test('bar3'));
}

}


7 changes: 2 additions & 5 deletions tests/CommonExtendedBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
require_once 'vendor/autoload.php';
require_once 'CommonBackendTest.php';

abstract class CommonExtendedBackendTest extends CommonBackendTest {

abstract class CommonExtendedBackendTest extends CommonBackendTest
{
private $_capabilities;

public function __construct($name = null, array $data = array(), $dataName = '')
Expand Down Expand Up @@ -178,7 +178,4 @@ public function testGetCapabilities(): void
$this->assertTrue(isset($res['infinite_lifetime']));
$this->assertTrue(isset($res['get_list']));
}

}


4 changes: 2 additions & 2 deletions tests/RedisBackendAutoExpiryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
* @copyright Copyright (c) 2012 Colin Mollenhour (http://colin.mollenhour.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class RedisBackendAutoExpiryTest extends RedisBackendTest {

class RedisBackendAutoExpiryTest extends RedisBackendTest
{
protected $autoExpireLifetime = 3600;

protected $autoExpireRefreshOnLoad = 1;
Expand Down
7 changes: 3 additions & 4 deletions tests/RedisBackendStandaloneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
* @copyright Copyright (c) 2012 Colin Mollenhour (http://colin.mollenhour.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class RedisBackendStandaloneTest extends RedisBackendTest {

protected $forceStandalone = TRUE;

class RedisBackendStandaloneTest extends RedisBackendTest
{
protected $forceStandalone = true;
}
14 changes: 7 additions & 7 deletions tests/RedisBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
* @copyright Copyright (c) 2012 Colin Mollenhour (http://colin.mollenhour.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class RedisBackendTest extends CommonExtendedBackendTest {

class RedisBackendTest extends CommonExtendedBackendTest
{
const LUA_MAX_C_STACK = 1000;

protected $forceStandalone = FALSE;
protected $forceStandalone = false;

protected $autoExpireLifetime = 0;

Expand All @@ -56,11 +56,11 @@ public function setUp($notag = false): void
'server' => getenv('REDIS_SERVER') ?: 'localhost',
'port' => getenv('REDIS_PORT') ?: '6379',
'database' => '1',
'notMatchingTags' => TRUE,
'notMatchingTags' => true,
'force_standalone' => $this->forceStandalone,
'compress_threshold' => 100,
'compression_lib' => 'gzip',
'use_lua' => TRUE,
'use_lua' => true,
'lua_max_c_stack' => self::LUA_MAX_C_STACK,
'auto_expire_lifetime' => $this->autoExpireLifetime,
'auto_expire_refresh_on_load' => $this->autoExpireRefreshOnLoad,
Expand Down Expand Up @@ -91,8 +91,8 @@ public function testCompression(): void
public function testExpiredCleanup(): void
{
$this->assertTrue($this->_instance->clean());
$this->assertTrue($this->_instance->save('BLAH','foo', array('TAG1', 'TAG2'), 1));
$this->assertTrue($this->_instance->save('BLAH','bar', array('TAG1', 'TAG3'), 1));
$this->assertTrue($this->_instance->save('BLAH', 'foo', array('TAG1', 'TAG2'), 1));
$this->assertTrue($this->_instance->save('BLAH', 'bar', array('TAG1', 'TAG3'), 1));
$ids = $this->_instance->getIdsMatchingAnyTags(array('TAG1','TAG2','TAG3'));
sort($ids);
$this->assertEquals(array('bar','foo'), $ids);
Expand Down

0 comments on commit dd4fe9b

Please sign in to comment.