mirror of
https://github.com/LBRYFoundation/lbry.com.git
synced 2025-08-23 17:47:26 +00:00
caching cleanup
This commit is contained in:
parent
00fa2329ee
commit
5238e52575
7 changed files with 41 additions and 34 deletions
|
@ -173,7 +173,7 @@ class Controller
|
|||
return static::redirect('/' . strtolower($slug), 301);
|
||||
}
|
||||
if (View::exists('page/' . $slug)) {
|
||||
Response::enableHttpCache();
|
||||
Response::enablePublicImmutableCache();
|
||||
return ['page/' . $slug, []];
|
||||
} else {
|
||||
return NavActions::execute404();
|
||||
|
|
|
@ -16,8 +16,15 @@ class Session
|
|||
{
|
||||
ini_set('session.cookie_secure', IS_PRODUCTION); // send cookie over ssl only
|
||||
ini_set('session.cookie_httponly', true); // no js access to cookies
|
||||
|
||||
session_start();
|
||||
|
||||
/*
|
||||
* session_start automatically adds headers because lolphp, let's remove them and handle it ourselves
|
||||
*/
|
||||
header_remove('cache-control');
|
||||
header_remove('pragma');
|
||||
header_remove('expires');
|
||||
|
||||
if (!static::get('secure_and_httponly_set')) {
|
||||
session_regenerate_id(); // ensure that old cookies get new settings
|
||||
|
|
|
@ -36,16 +36,19 @@ class AcquisitionActions extends Actions
|
|||
|
||||
public static function executeVerify(string $token)
|
||||
{
|
||||
Response::disableHttpCache();
|
||||
return ['acquisition/verify', ['token' => $token]];
|
||||
}
|
||||
|
||||
public static function executeAutoVerify()
|
||||
{
|
||||
Response::disableHttpCache();
|
||||
return ['acquisition/auto-verify'];
|
||||
}
|
||||
|
||||
public static function executeYoutubeToken()
|
||||
{
|
||||
Response::disableHttpCache();
|
||||
$channelName = Request::encodeStringFromUser($_POST['desired_lbry_channel_name']);
|
||||
$immediateSync = (boolean)$_POST['immediate_sync'];
|
||||
|
||||
|
@ -65,6 +68,7 @@ class AcquisitionActions extends Actions
|
|||
|
||||
public static function executeYoutubeStatus(string $token)
|
||||
{
|
||||
Response::disableHttpCache();
|
||||
$data = LBRY::statusYoutube($token);
|
||||
|
||||
if (!$data['success']) {
|
||||
|
|
|
@ -28,27 +28,27 @@ class ContentActions extends Actions
|
|||
|
||||
public static function executeHome(): array
|
||||
{
|
||||
Response::enableHttpCache();
|
||||
Response::enablePublicImmutableCache();
|
||||
return ['page/home'];
|
||||
}
|
||||
|
||||
//
|
||||
public static function executeOrg(): array
|
||||
{
|
||||
Response::enableHttpCache();
|
||||
Response::enablePublicImmutableCache();
|
||||
return ['page/org'];
|
||||
}
|
||||
|
||||
public static function executeTv(): array
|
||||
{
|
||||
Response::enableHttpCache();
|
||||
Response::enablePublicImmutableCache();
|
||||
return ['page/tv'];
|
||||
}
|
||||
//
|
||||
|
||||
public static function executeNews(string $slug = null): array
|
||||
{
|
||||
Response::enableHttpCache();
|
||||
Response::enablePublicImmutableCache();
|
||||
|
||||
if (!$slug || $slug == static::SLUG_RSS) {
|
||||
$posts = array_filter(
|
||||
|
@ -90,7 +90,7 @@ class ContentActions extends Actions
|
|||
|
||||
public static function executeFaq(string $slug = null): array
|
||||
{
|
||||
Response::enableHttpCache();
|
||||
Response::enablePublicImmutableCache();
|
||||
|
||||
if (!$slug) {
|
||||
$allPosts = Post::find(static::VIEW_FOLDER_FAQ, Post::SORT_ORD_ASC);
|
||||
|
@ -139,7 +139,7 @@ class ContentActions extends Actions
|
|||
|
||||
public static function executeCreditReports(string $year = null, string $month = null): array
|
||||
{
|
||||
Response::enableHttpCache();
|
||||
Response::enablePublicImmutableCache();
|
||||
|
||||
$posts = Post::find(static::VIEW_FOLDER_CREDIT_REPORTS);
|
||||
|
||||
|
@ -150,7 +150,7 @@ class ContentActions extends Actions
|
|||
|
||||
public static function executeCreditReport(string $year = null, string $quarter = null): array
|
||||
{
|
||||
Response::enableHttpCache();
|
||||
Response::enablePublicImmutableCache();
|
||||
|
||||
try {
|
||||
$post = Post::load(static::SLUG_CREDIT_REPORTS . '/' . $year . '-Q' . $quarter);
|
||||
|
@ -172,7 +172,7 @@ class ContentActions extends Actions
|
|||
|
||||
public static function executeBounty(string $slug = null): array
|
||||
{
|
||||
Response::enableHttpCache();
|
||||
Response::enablePublicImmutableCache();
|
||||
|
||||
|
||||
|
||||
|
@ -276,7 +276,7 @@ class ContentActions extends Actions
|
|||
//
|
||||
// $zip->close();
|
||||
//
|
||||
// Response::enableHttpCache();
|
||||
// Response::enablePublicImmutableCache();
|
||||
// Response::setDownloadHttpHeaders($zipFileName, 'application/zip', filesize($zipPath));
|
||||
//
|
||||
// return ['internal/zip', [
|
||||
|
@ -320,7 +320,7 @@ class ContentActions extends Actions
|
|||
}
|
||||
public static function executePostCategoryFilter(string $category)
|
||||
{
|
||||
Response::enableHttpCache();
|
||||
Response::enablePublicImmutableCache();
|
||||
|
||||
$filter_post = [];
|
||||
|
||||
|
|
|
@ -51,6 +51,7 @@ class MailActions extends Actions
|
|||
|
||||
public static function executeUnsubscribe(string $email)
|
||||
{
|
||||
Response::disableHttpCache();
|
||||
$decodedEmail = Encoding::base64DecodeUrlsafe(urldecode($email));
|
||||
if (!$decodedEmail) {
|
||||
return ['mail/unsubscribe', ['error' => 'Invalid unsubscribe link']];
|
||||
|
@ -62,6 +63,7 @@ class MailActions extends Actions
|
|||
|
||||
public static function editEmailSettings(string $token)
|
||||
{
|
||||
Response::disableHttpCache();
|
||||
list($status, $headers, $response) = LBRY::emailStatus($token);
|
||||
if ($status == 403) {
|
||||
//Catch 403 to return elegant error message.
|
||||
|
|
|
@ -32,7 +32,7 @@ class i18nActions extends Actions
|
|||
$json = Transifex::getTranslationResourceFile($project, $resource, $language);
|
||||
|
||||
Response::setHeader(Response::HEADER_CROSS_ORIGIN, "*");
|
||||
Response::setHeader(Response::HEADER_ETAG, md5(json_encode($json)));
|
||||
Response::enablePublicMutableCache(md5(json_encode($json)));
|
||||
|
||||
return View::renderJson($json);
|
||||
}
|
||||
|
|
|
@ -6,7 +6,6 @@ class Response
|
|||
const HEADER_LOCATION = 'Location';
|
||||
|
||||
const HEADER_CACHE_CONTROL = 'Cache-Control';
|
||||
const HEADER_LAST_MODIFIED = 'Last-Modified';
|
||||
const HEADER_ETAG = 'Etag';
|
||||
|
||||
const HEADER_CONTENT_TYPE = 'Content-Type';
|
||||
|
@ -26,7 +25,9 @@ class Response
|
|||
],
|
||||
'css' => ['/css/all.css']
|
||||
];
|
||||
protected static $headers = [];
|
||||
protected static $headers = [
|
||||
'Cache-Control' => 'private, no-cache'
|
||||
];
|
||||
protected static $headersSent = false;
|
||||
protected static $content = '';
|
||||
protected static $contentSent = false;
|
||||
|
@ -192,30 +193,23 @@ class Response
|
|||
]));
|
||||
}
|
||||
|
||||
public static function enableHttpCache(int $seconds = 300)
|
||||
//public immutable cache = hard-caching (no server checks) until time limit passes
|
||||
public static function enablePublicImmutableCache(int $seconds = 300)
|
||||
{
|
||||
static::addCacheControlHeader('max-age', $seconds);
|
||||
static::setHeader('Pragma', 'public');
|
||||
static::setHeader(static::HEADER_CACHE_CONTROL, 'public, max-age=' . $seconds);
|
||||
}
|
||||
|
||||
public static function addCacheControlHeader(string $name, $value = null)
|
||||
//public mutable cache = soft-caching (requires at least one round trip for headers) as long as etag identifier matches
|
||||
public static function enablePublicMutableCache(string $etag)
|
||||
{
|
||||
$cacheControl = static::getHeader(static::HEADER_CACHE_CONTROL);
|
||||
$currentHeaders = [];
|
||||
if ($cacheControl) {
|
||||
foreach (preg_split('/\s*,\s*/', $cacheControl) as $tmp) {
|
||||
$tmp = explode('=', $tmp);
|
||||
$currentHeaders[$tmp[0]] = $tmp[1] ?? null;
|
||||
}
|
||||
}
|
||||
$currentHeaders[strtr(strtolower($name), '_', '-')] = $value;
|
||||
|
||||
$headers = [];
|
||||
foreach ($currentHeaders as $key => $currentVal) {
|
||||
$headers[] = $key . ($currentVal !== null ? '=' . $currentVal : '');
|
||||
static::setHeader(static::HEADER_CACHE_CONTROL, 'public, no-cache');
|
||||
static::setHeader(static::HEADER_ETAG, $etag);
|
||||
}
|
||||
|
||||
static::setHeader(static::HEADER_CACHE_CONTROL, implode(', ', $headers));
|
||||
//always reload and re-execute this resource, disable any local or intermediary caching
|
||||
public static function disableHttpCache()
|
||||
{
|
||||
static::setHeader(static::HEADER_CACHE_CONTROL, 'private, no-cache, no-store');
|
||||
}
|
||||
|
||||
public static function setHeader($name, $value)
|
||||
|
|
Loading…
Add table
Reference in a new issue