Permalink
Browse files

Make sure query string is part of the cache identifier

  • Loading branch information...
flack committed Feb 2, 2019
1 parent 5c73c2e commit 5b105f964fca4236f55b8a0aece709856b02a1eb
@@ -175,16 +175,16 @@ public function dynamic_load($url)
$oldcontext = midcom_core_context::get();
$context = midcom_core_context::enter($url, $oldcontext->get_key(MIDCOM_CONTEXT_ROOTTOPIC));
$cached = $this->cache->content->check_dl_hit($context->id);
$request = $this->request->duplicate([], null, []);
$request->attributes->set('context', $context);
$cached = $this->cache->content->check_dl_hit($request);
if ($cached !== false) {
echo $cached;
midcom_core_context::leave();
return;
}
$request = $this->request->duplicate([], null, []);
$request->attributes->set('context', $context);
try {
$response = $this->httpkernel->handle($request, HttpKernelInterface::SUB_REQUEST, false);
} catch (midcom_error $e) {
@@ -203,7 +203,7 @@ public function dynamic_load($url)
echo $dl_cache_data;
/* Cache DL the content */
$this->cache->content->store_dl_content($context->id, $dl_cache_data);
$this->cache->content->store_dl_content($context->id, $dl_cache_data, $request);
midcom_core_context::leave();
}
@@ -73,7 +73,7 @@ public function process_serveattachmentguid(Request $request, $guid)
}
midcom::get()->cache->content->cache_control_headers($response);
// Store metadata in cache so _check_hit() can help us
midcom::get()->cache->content->write_meta_cache('A-' . $etag, $etag);
midcom::get()->cache->content->write_meta_cache('A-' . $etag, $etag, $request);
return $response;
}
@@ -227,7 +227,7 @@ private function _check_hit(Request $request)
}
// Check that we have cache for the identifier
$request_id = $this->generate_request_identifier(0);
$request_id = $this->generate_request_identifier($request);
// Load metadata for the content identifier connected to current request
$content_id = $this->_meta_cache->fetch($request_id);
if ($content_id === false) {
@@ -321,15 +321,16 @@ public function on_response(FilterResponseEvent $event)
return;
}
$content_id = 'C-' . $etag;
$this->write_meta_cache($content_id, $etag);
$this->write_meta_cache($content_id, $etag, $request);
$this->_data_cache->save($content_id, $cache_data);
}
/**
* Generate a valid cache identifier for a context of the current request
*/
private function generate_request_identifier($context)
private function generate_request_identifier(Request $request)
{
$context = $request->attributes->get('context')->id;
// Cache the request identifier so that it doesn't change between start and end of request
static $identifier_cache = [];
if (isset($identifier_cache[$context])) {
@@ -365,8 +366,7 @@ private function generate_request_identifier($context)
break;
}
$identifier_source .= ';URL=' . midcom_core_context::get()->get_key(MIDCOM_CONTEXT_URI);
$identifier_source .= ';URL=' . $request->getRequestUri();
debug_add("Generating context {$context} request-identifier from: {$identifier_source}");
$identifier_cache[$context] = 'R-' . md5($identifier_source);
@@ -474,9 +474,9 @@ public function no_cache(Response $response = null)
*
* @see no_cache()
*/
public function uncached()
public function uncached($uncached = true)
{
$this->_uncached = true;
$this->_uncached = $uncached;
}
/**
@@ -619,7 +619,7 @@ public function register($guid)
* Writes meta-cache entry from context data using given content id
* Used to be part of on_request, but needed by serve-attachment method in midcom_core_urlmethods as well
*/
public function write_meta_cache($content_id, $etag)
public function write_meta_cache($content_id, $etag, Request $request)
{
if ( $this->_uncached
|| $this->_no_cache) {
@@ -634,8 +634,7 @@ public function write_meta_cache($content_id, $etag)
}
// Construct cache identifier
$context = midcom_core_context::get()->id;
$request_id = $this->generate_request_identifier($context);
$request_id = $this->generate_request_identifier($request);
$entries = [
$request_id => $content_id,
@@ -649,6 +648,7 @@ public function write_meta_cache($content_id, $etag)
$this->_meta_cache->saveMultiple($entries, $lifetime);
// Cache where the object have been
$context = midcom_core_context::get()->id;
$this->store_context_guid_map($context, $content_id, $request_id);
}
@@ -680,12 +680,12 @@ private function store_context_guid_map($context, $content_id, $request_id)
$this->_meta_cache->saveMultiple($to_save);
}
public function check_dl_hit($context)
public function check_dl_hit(Request $request)
{
if ($this->_no_cache) {
return false;
}
$dl_request_id = 'DL' . $this->generate_request_identifier($context);
$dl_request_id = 'DL' . $this->generate_request_identifier($request);
$dl_content_id = $this->_meta_cache->fetch($dl_request_id);
if ($dl_content_id === false) {
return false;
@@ -694,13 +694,13 @@ public function check_dl_hit($context)
return $this->_data_cache->fetch($dl_content_id);
}
public function store_dl_content($context, $dl_cache_data)
public function store_dl_content($context, $dl_cache_data, Request $request)
{
if ( $this->_no_cache
|| $this->_uncached) {
return;
}
$dl_request_id = 'DL' . $this->generate_request_identifier($context);
$dl_request_id = 'DL' . $this->generate_request_identifier($request);
$dl_content_id = 'DLC-' . md5($dl_cache_data);
if ($this->_expires !== null) {
@@ -0,0 +1,54 @@
<?php
/**
* @package openpsa.test
* @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
* @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License
*/
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
/**
* OpenPSA testcase
*
* @package openpsa.test
*/
class midcom_services_cache_module_contentTest extends openpsa_testcase
{
public function test_on_request()
{
$module = new midcom_services_cache_module_content;
$module->initialize();
$module->uncached(false);
$request = Request::create('/');
$ctx = midcom_core_context::enter('/');
$request->attributes->set('context', $ctx);
$event = new GetResponseEvent($GLOBALS['kernel'], $request, KernelInterface::MASTER_REQUEST);
$module->on_request($event);
$this->assertFalse($event->hasResponse(), 'Response should not be cached yet');
// write response to cache
$response = Response::create('test');
$filter_event = new FilterResponseEvent($GLOBALS['kernel'], $request, KernelInterface::MASTER_REQUEST, $response);
$module->on_response($filter_event);
$module->on_request($event);
$this->assertTrue($event->hasResponse(), 'Response should be cached');
midcom_core_context::leave();
// same url, but with GET params this time
$request = Request::create('/', 'GET', ['test' => 'test']);
$ctx = midcom_core_context::enter('/');
$request->attributes->set('context', $ctx);
$event = new GetResponseEvent($GLOBALS['kernel'], $request, KernelInterface::MASTER_REQUEST);
$module->on_request($event);
$this->assertFalse($event->hasResponse(), 'Response should not be cached yet');
}
}

0 comments on commit 5b105f9

Please sign in to comment.