Skip to content

Commit ab71aac

Browse files
committed
Merge pull request #195 from GordonLesti/develop
Cache the response content-type header
2 parents 429d540 + adbcf1f commit ab71aac

File tree

10 files changed

+158
-52
lines changed

10 files changed

+158
-52
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ env:
1414
- MAGE=magento-1-5-1-0
1515

1616
install:
17-
- cp -f .travis/composer.json composer.json
18-
- composer install --dev --prefer-dist
17+
- composer install --dev --prefer-dist --no-scripts
1918
- curl https://gordonlesti.com/"$MAGE".tar.gz | tar xz
2019
- mv $MAGE magento
2120
- cd magento

.travis/composer.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

app/code/community/Lesti/Fpc/Helper/Data.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,19 @@ public function getFullActionName()
243243
$request->getRequestedControllerName() . $delimiter .
244244
$request->getRequestedActionName();
245245
}
246+
247+
/**
248+
* @param Mage_Core_Controller_Response_Http $response
249+
* @return string
250+
*/
251+
public function getContentType(\Mage_Core_Controller_Response_Http $response)
252+
{
253+
foreach ($response->getHeaders() as $header) {
254+
if (isset($header['name']) && $header['name'] === 'Content-Type' && isset($header['value'])) {
255+
return $header['value'];
256+
}
257+
}
258+
259+
return 'text/html; charset=UTF-8';
260+
}
246261
}

app/code/community/Lesti/Fpc/Model/Fpc.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,25 @@ public function __construct()
6363
/**
6464
* Save data
6565
*
66-
* @param string $data
66+
* @param \Lesti_Fpc_Model_Fpc_CacheItem $item
6767
* @param string $id
6868
* @param array $tags
6969
* @param int $lifeTime
7070
* @return bool
7171
*/
72-
public function save($data, $id, $tags=array(), $lifeTime=null)
72+
public function save(Lesti_Fpc_Model_Fpc_CacheItem $item, $id, $tags=array(), $lifeTime=null)
7373
{
7474
if (!in_array(self::CACHE_TAG, $tags)) {
7575
$tags[] = self::CACHE_TAG;
7676
}
7777
if (is_null($lifeTime)) {
7878
$lifeTime = (int) $this->getFrontend()->getOption('lifetime');
7979
}
80+
$data = array(
81+
$item->getContent(),
82+
$item->getTime(),
83+
$item->getContentType(),
84+
);
8085
// edit cached object
8186
$cacheData = new Varien_Object();
8287
$cacheData->setCachedata($data);
@@ -93,6 +98,7 @@ public function save($data, $id, $tags=array(), $lifeTime=null)
9398
$lifeTime = $cacheData->getLifeTime();
9499

95100
$compressLevel = Mage::getStoreConfig(self::GZCOMPRESS_LEVEL_XML_PATH);
101+
$data = serialize($data);
96102
if ($compressLevel != -2) {
97103
$data = gzcompress($data, $compressLevel);
98104
}
@@ -107,17 +113,22 @@ public function save($data, $id, $tags=array(), $lifeTime=null)
107113

108114
/**
109115
* @param string $id
110-
* @return string
116+
* @return boolean|\Lesti_Fpc_Model_Fpc_CacheItem
111117
*/
112118
public function load($id)
113119
{
114120
$data = parent::load($id);
121+
if ($data === false) {
122+
return false;
123+
}
115124
$compressLevel = Mage::getStoreConfig(self::GZCOMPRESS_LEVEL_XML_PATH);
116125
if ($data !== false && $compressLevel != -2) {
117126
$data = gzuncompress($data);
118127
}
119128

120-
return $data;
129+
$data = unserialize($data);
130+
131+
return new Lesti_Fpc_Model_Fpc_CacheItem($data[0], $data[1], $data[2]);
121132
}
122133

123134
/**
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/**
4+
* Class Lesti_Fpc_Model_Fpc_CacheItem
5+
*/
6+
class Lesti_Fpc_Model_Fpc_CacheItem
7+
{
8+
/**
9+
* @var string
10+
*/
11+
private $_content;
12+
13+
/**
14+
* @var int
15+
*/
16+
private $_time;
17+
18+
/**
19+
* @var string
20+
*/
21+
private $_contentType;
22+
23+
/**
24+
* @param string $content
25+
* @param int $time
26+
* @param string $contentType
27+
*/
28+
public function __construct($content, $time, $contentType)
29+
{
30+
$this->_content = $content;
31+
$this->_time = $time;
32+
$this->_contentType = $contentType;
33+
}
34+
35+
/**
36+
* @return string
37+
*/
38+
public function getContent()
39+
{
40+
return $this->_content;
41+
}
42+
43+
/**
44+
* @return int
45+
*/
46+
public function getTime()
47+
{
48+
return $this->_time;
49+
}
50+
51+
/**
52+
* @return string
53+
*/
54+
public function getContentType()
55+
{
56+
return $this->_contentType;
57+
}
58+
}

app/code/community/Lesti/Fpc/Model/Observer.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public function controllerActionLayoutGenerateBlocksBefore($observer)
3737
Mage::helper('fpc')->canCacheRequest()) {
3838
$key = Mage::helper('fpc')->getKey();
3939
if ($object = $this->_getFpc()->load($key)) {
40-
$time = (int)substr($object, 0, 10);
41-
$body = substr($object, 10);
40+
$time = $object->getTime();
41+
$body = $object->getContent();
4242
$this->_cached = true;
4343
$session = Mage::getSingleton('customer/session');
4444
$lazyBlocks = Mage::helper('fpc/block')->getLazyBlocks();
@@ -71,9 +71,9 @@ public function controllerActionLayoutGenerateBlocksBefore($observer)
7171
$this->_replaceFormKey();
7272
$body = str_replace($this->_placeholder, $this->_html, $body);
7373
if (Mage::getStoreConfig(self::SHOW_AGE_XML_PATH)) {
74-
Mage::app()->getResponse()
75-
->setHeader('Age', time() - $time);
74+
Mage::app()->getResponse()->setHeader('Age', time() - $time);
7675
}
76+
Mage::app()->getResponse()->setHeader('Content-Type', $object->getContentType());
7777
$response = Mage::app()->getResponse();
7878
$response->setBody($body);
7979
Mage::dispatchEvent(
@@ -133,7 +133,11 @@ public function httpResponseSendBefore($observer)
133133
array('cache_tags' => $cacheTags)
134134
);
135135
$this->_cacheTags = $cacheTags->getValue();
136-
$this->_getFpc()->save(time() . $body, $key, $this->_cacheTags);
136+
$this->_getFpc()->save(
137+
new Lesti_Fpc_Model_Fpc_CacheItem($body, time(), Mage::helper('fpc')->getContentType($response)),
138+
$key,
139+
$this->_cacheTags
140+
);
137141
$this->_cached = true;
138142
$body = str_replace($this->_placeholder, $this->_html, $body);
139143
$observer->getEvent()->getResponse()->setBody($body);
@@ -269,4 +273,4 @@ protected function _insertDynamicBlocks(
269273
}
270274
}
271275
}
272-
}
276+
}

app/code/community/Lesti/Fpc/Test/Model/Fpc.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected function setUp()
3333
*/
3434
public function saveLoadClean()
3535
{
36-
$data = 'fpc_data';
36+
$data = new \Lesti_Fpc_Model_Fpc_CacheItem('fpc_data', time(), 'text/html');
3737
$id = 'fpc_id';
3838
$tag = 'tag1';
3939

@@ -61,4 +61,4 @@ public function saveLoadClean()
6161
sleep(3);
6262
$this->assertFalse($this->_fpc->load($id));
6363
}
64-
}
64+
}

app/code/community/Lesti/Fpc/Test/Model/Observer/Clean.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ protected function tearDown()
3838
*/
3939
public function testCoreCleanCache()
4040
{
41-
$this->_fpc->save('fpc_old_data', 'fpc_old_id', array('fpc'), 1);
41+
$data = new \Lesti_Fpc_Model_Fpc_CacheItem('fpc_old_data', time(), 'text/html');
42+
$this->_fpc->save($data, 'fpc_old_id', array('fpc'), 1);
4243
sleep(2);
4344
$this->_cleanObserver->coreCleanCache();
4445
$this->assertFalse($this->_fpc->remove('fpc_old_id'));
@@ -49,14 +50,15 @@ public function testCoreCleanCache()
4950
*/
5051
public function testControllerActionPredispatchAdminhtmlCacheMassRefresh()
5152
{
52-
$this->_fpc->save('test_data', 'test_id');
53+
$data = new \Lesti_Fpc_Model_Fpc_CacheItem('test_data', time(), 'text/html');
54+
$this->_fpc->save($data, 'test_id');
5355
Mage::app()->getRequest()->setParam('types', array('core'));
5456
$this->_cleanObserver->
5557
controllerActionPredispatchAdminhtmlCacheMassRefresh();
56-
$this->assertEquals('test_data', $this->_fpc->load('test_id'));
58+
$this->assertEquals($data, $this->_fpc->load('test_id'));
5759

5860
$this->_fpc->clean();
59-
$this->_fpc->save('test_data', 'test_id');
61+
$this->_fpc->save($data, 'test_id');
6062
Mage::app()->getRequest()->setParam(
6163
'types',
6264
array('core', Lesti_Fpc_Model_Observer_Clean::CACHE_TYPE)
@@ -66,14 +68,14 @@ public function testControllerActionPredispatchAdminhtmlCacheMassRefresh()
6668
$this->assertFalse($this->_fpc->load('test_id'));
6769

6870
$this->_fpc->clean();
69-
$this->_fpc->save('test_data', 'test_id');
71+
$this->_fpc->save($data, 'test_id');
7072
Mage::app()->getRequest()->setParam(
7173
'types',
7274
array('core', Lesti_Fpc_Model_Observer_Clean::CACHE_TYPE)
7375
);
7476
Mage::app()->getCacheInstance()->banUse('fpc');
7577
$this->_cleanObserver->
7678
controllerActionPredispatchAdminhtmlCacheMassRefresh();
77-
$this->assertEquals('test_data', $this->_fpc->load('test_id'));
79+
$this->assertEquals($data, $this->_fpc->load('test_id'));
7880
}
7981
}

0 commit comments

Comments
 (0)