Skip to content

Commit

Permalink
for v1.2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauren Song committed Jul 31, 2018
1 parent 01ad3f3 commit 38b4949
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -32,7 +32,9 @@ Otherwise request that the server admin configure the cache root for the server.
* LSCPS automatically caches the following pages with a GET request (including AJAX GET): Home, Categories, Products, CMS, New products, Best sales, Suppliers, Manufacturers, Prices drop, Sitemap.
* User information can be cached privately via ESI blocks and auto purged when the information changes. Support for cart and account sign in are built in. Other third-party modules that contain private information can be easily added.
* Updates in the shop admin area automatically trigger a purge of any related pages in the cache.
* New client orders automatically trigger a purge of related product and catalog pages based on stock status or quantity (configurable).
* The cache can be manually flushed from within the PrestaShop admin.
* If a page contains products with specific prices, TTL will be auto adjusted based on special price effective dates.

### Testing the Module

Expand Down
58 changes: 52 additions & 6 deletions classes/Cache.php
Expand Up @@ -37,6 +37,7 @@ class LiteSpeedCacheCore
private $purgeTags;
private $config;
private $esiTtl;
private $specificPrices = array();

public function __construct(LiteSpeedCacheConfig $config)
{
Expand Down Expand Up @@ -333,19 +334,15 @@ private function getPurgeTagsByOrder($order)
}

if ($includeProd) {
$tags = $this->getPurgeTagsByProductOrder(
$product['product_id'],
$includeCats);
$tags = $this->getPurgeTagsByProductOrder($product['product_id'], $includeCats);
if (!empty($tags)) {
$pubtags = array_merge($pubtags, $tags);
}
}
}
if (!empty($pubtags)) {
if ($hasStockStatusChange) {
$pubtags = array_merge(
$pubtags,
$this->config->getDefaultPurgeTagsByProduct());
$pubtags = array_merge($pubtags, $this->config->getDefaultPurgeTagsByProduct());
}
$tags['pub'] = array_unique($pubtags);
}
Expand Down Expand Up @@ -522,6 +519,54 @@ public function purgeEntireStorage($from)
LSLog::log('Set header ' . $purgeHeader . ' (' . $from . ')', LSLog::LEVEL_FORCE);
}

public function checkSpecificPrices($specific_prices)
{
LSLog::log('specific price ' . var_export($specific_prices, 1));
if ($specific_prices['from'] == $specific_prices['to'] && $specific_prices['to'] == '0000-00-00 00:00:00') {
// no date range
return;
}
if (is_array($specific_prices) && isset($specific_prices['to'])) {
$this->specificPrices[] = array('from' => $specific_prices['from'],
'to' => $specific_prices['to']);
}
}

private function adjustTtlBySpecificPrices($ttl)
{
if (empty($this->specificPrices)) {
return $ttl;
}
$ttl0 = $ttl;

$now = time();
foreach ($this->specificPrices as $sp) {
if ($sp['from'] != '0000-00-00 00:00:00') { // from is set
$from = strtotime($sp['from']);
if ($from > $now) { // not start yet, go by from date
if (($from - $now) < $ttl) {
$ttl = $from - $now;
}
continue;
}
}
if ($sp['to'] != '0000-00-00 00:00:00') { // to is set
$to = strtotime($sp['to']);
if (($to > $now) && (($to - $now) < $ttl)) {
$ttl = $to - $now;
}
}
}
if (defined('_LITESPEED_DEBUG_') && _LITESPEED_DEBUG_ >= LSLog::LEVEL_SPECIFIC_PRICE) {
if ($ttl0 != $ttl) {
LSLog::log("TTL adjusted due to specific prices from $ttl0 to $ttl", LSLog::LEVEL_SETHEADER);
} else {
LSLog::log('Detected specific prices, but no TTL adjustment', LSLog::LEVEL_SPECIFIC_PRICE);
}
}
return $ttl;
}

public function setCacheControlHeader()
{
if (headers_sent()) {
Expand All @@ -544,6 +589,7 @@ public function setCacheControlHeader()
$ttl = (($ccflag & LSC::CCBM_PRIVATE) == 0) ?
$this->config->get(Conf::CFG_PUBLIC_TTL) : $this->config->get(Conf::CFG_PRIVATE_TTL);
}
$ttl = $this->adjustTtlBySpecificPrices($ttl);

if (($ccflag & LSC::CCBM_PRIVATE) == 0) {
$cacheControlHeader .= 'public,max-age=' . $ttl;
Expand Down
2 changes: 2 additions & 0 deletions classes/Config.php
Expand Up @@ -537,6 +537,8 @@ public function getReservedHooks()
'actionCustomerLogoutAfter',
'actionAuthentication',
'actionCustomerAccountAdd',
/** specific price check **/
'actionProductSearchAfter',
/** public purge * */
/***** product *****/
'actionProductAdd',
Expand Down
1 change: 1 addition & 0 deletions classes/DebugLog.php
Expand Up @@ -39,6 +39,7 @@ class LiteSpeedCacheLog
const LEVEL_ESI_INCLUDE = 7;
const LEVEL_CACHE_ROUTE = 8;
const LEVEL_ENVCOOKIE_DETAIL = 9;
const LEVEL_SPECIFIC_PRICE = 9.5;
const LEVEL_ESI_OUTPUT = 9.5;
const LEVEL_SAVED_DATA = 10;
const LEVEL_HOOK_DETAIL = 10;
Expand Down
7 changes: 5 additions & 2 deletions classes/VaryCookie.php
Expand Up @@ -113,8 +113,11 @@ private function writeVary()
$this->status |= self::BM_UPDATE_FAILED;
return;
}
if (($this->status & self::BM_IS_GUEST) > 0 && ($this->status & self::BM_VARYVALUE_CHANGED) == 0) {
// no cookie set for guest mode
if (($this->status & self::BM_IS_GUEST) > 0
&& ($this->status & self::BM_VARYVALUE_CHANGED) == 0
&& LiteSpeedCache::isCacheable()) {
// no cookie set for guest mode and only if for cacheable response.
// for non-cacheable route, like ajax request, can set vary cookie
return;
}

Expand Down
2 changes: 1 addition & 1 deletion config.xml
Expand Up @@ -2,7 +2,7 @@
<module>
<name>litespeedcache</name>
<displayName><![CDATA[LiteSpeed Cache Plugin]]></displayName>
<version><![CDATA[1.2.3]]></version>
<version><![CDATA[1.2.4]]></version>
<description><![CDATA[Integrates with LiteSpeed Full Page Cache on LiteSpeed Server.]]></description>
<author><![CDATA[LiteSpeedTech]]></author>
<tab><![CDATA[administration]]></tab>
Expand Down
31 changes: 28 additions & 3 deletions litespeedcache.php
Expand Up @@ -63,7 +63,7 @@ public function __construct()
$this->name = 'litespeedcache'; // self::MODULE_NAME was rejected by validator
$this->tab = 'administration';
$this->author = 'LiteSpeedTech';
$this->version = '1.2.3'; // validator does not allow const here
$this->version = '1.2.4'; // validator does not allow const here
$this->need_instance = 0;
$this->module_key = '2a93f81de38cad872010f09589c279ba';

Expand Down Expand Up @@ -139,6 +139,14 @@ public function hookActionDispatcher($params)
{
$controllerType = $params['controller_type'];
$controllerClass = $params['controller_class'];

if (_LITESPEED_DEBUG_ > 0) {
$notprinted = array('AdminDashboardController', 'AdminGamificationController');
if (in_array($controllerClass, $notprinted)) {
LiteSpeedCacheLog::setDebugLevel(0); // disable logging for current request
}
}

$status = $this->checkDispatcher($controllerType, $controllerClass);

if (_LITESPEED_DEBUG_ >= LiteSpeedCacheLog::LEVEL_CACHE_ROUTE) {
Expand Down Expand Up @@ -167,6 +175,18 @@ public function hookDisplayOverrideTemplate($params)
$this->cache->initCacheTagsByController($params);
}

public function hookActionProductSearchAfter($params)
{
// Hook::exec('actionProductSearchAfter', $searchVariables);
if (self::isCacheable() && isset($params['products'])) {
foreach ($params['products'] as $p) {
if (!empty($p['specific_prices'])) {
$this->cache->checkSpecificPrices($p['specific_prices']);
}
}
}
}

public function hookFilterCategoryContent($params)
{
if (self::isCacheable()) {
Expand All @@ -178,8 +198,13 @@ public function hookFilterCategoryContent($params)

public function hookFilterProductContent($params)
{
if (self::isCacheable() && isset($params['object']['id'])) {
$this->cache->addCacheTags(LiteSpeedCacheConfig::TAG_PREFIX_PRODUCT . $params['object']['id']);
if (self::isCacheable()) {
if (isset($params['object']['id'])) {
$this->cache->addCacheTags(LiteSpeedCacheConfig::TAG_PREFIX_PRODUCT . $params['object']['id']);
}
if (!empty($params['object']['specific_prices'])) {
$this->cache->checkSpecificPrices($params['object']['specific_prices']);
}
}
}

Expand Down

0 comments on commit 38b4949

Please sign in to comment.