Merge pull request #247 from lbryio/clear-cache

Add route to clear cache
This commit is contained in:
Jeremy Kauffman 2017-10-31 15:39:37 -04:00 committed by GitHub
commit e9df217483
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 8 deletions

View file

@ -2,6 +2,8 @@
class Controller
{
const CACHE_CLEAR_PATH = '/clear-cache';
protected static $queuedFunctions = [];
public static function dispatch($uri)
@ -109,6 +111,7 @@ class Controller
$router->post('/postcommit', 'OpsActions::executePostCommit');
$router->post('/log-upload', 'OpsActions::executeLogUpload');
$router->get(static::CACHE_CLEAR_PATH, 'OpsActions::executeClearCache');
$router->any('/list/subscribe', 'MailActions::executeSubscribe');
$router->any('/list/subscribed', 'MailActions::executeSubscribed');

View file

@ -2,6 +2,20 @@
class OpsActions extends Actions
{
public static function executeClearCache(): array
{
if (!ini_get('apc.enabled') || !function_exists('apc_clear_cache'))
{
return View::renderJson(['success' => false, 'error' => 'Cache not enabled']);
}
apc_clear_cache();
apc_clear_cache('user');
apc_clear_cache('opcode');
return View::renderJson(['success' => true]);
}
public static function executePostCommit(): array
{
$payload = Request::getParam('payload');

View file

@ -21,3 +21,6 @@ Shell::exec('git fetch && git reset --hard origin/master');
View::compileCss();
View::gzipAssets();
// clear cache
Curl::get('localhost'.Controller::CACHE_CLEAR_PATH);

View file

@ -352,7 +352,7 @@ class Response
return $statusTexts[$code] ?? null;
}
protected static function normalizeHeaderName($name)
protected static function normalizeHeaderName($name): string
{
return preg_replace_callback(
'/\-(.)/',

View file

@ -19,7 +19,7 @@ class View
const CSS_DIR = self::WEB_DIR . '/css';
const JS_DIR = self::WEB_DIR . '/js';
public static function render($template, array $vars = [])
public static function render($template, array $vars = []): string
{
if (static::isMarkdown($template))
{
@ -78,7 +78,7 @@ class View
}
}
public static function markdownToHtml($path)
public static function markdownToHtml($path): string
{
return ParsedownExtra::instance()->text(trim(file_get_contents($path)));
}
@ -154,7 +154,7 @@ class View
}
}
protected static function interpolateTokens($html)
protected static function interpolateTokens($html): string
{
return preg_replace_callback('/{{[\w\.]+}}/is', function ($m)
{
@ -162,12 +162,12 @@ class View
}, $html);
}
protected static function escapeOnce($value)
protected static function escapeOnce($value): string
{
return preg_replace('/&([a-z]+|(#\d+)|(#x[\da-f]+));/i', '&$1;', htmlspecialchars((string)$value, ENT_QUOTES, 'utf-8'));
}
protected static function attributesToHtml($attributes)
protected static function attributesToHtml($attributes): string
{
return implode('', array_map(function ($k, $v)
{
@ -175,13 +175,19 @@ class View
}, array_keys($attributes), array_values($attributes)));
}
public static function renderTag($tag, $attributes = [])
public static function renderTag($tag, $attributes = []): string
{
return $tag ? sprintf('<%s%s />', $tag, static::attributesToHtml($attributes)) : '';
}
public static function renderContentTag($tag, $content = null, $attributes = [])
public static function renderContentTag($tag, $content = null, $attributes = []): string
{
return $tag ? sprintf('<%s%s>%s</%s>', $tag, static::attributesToHtml($attributes), $content, $tag) : '';
}
public static function renderJson($data): array
{
Response::setHeader(Response::HEADER_CONTENT_TYPE, 'application/json');
return ['internal/json', ['json' => $data, '_no_layout' => true]];
}
}

View file

@ -0,0 +1 @@
<?php echo json_encode($json ?? [], JSON_PRETTY_PRINT) ?>