Magento 2 cache solution needed - magento

Hi I'm looking for a cache solution that will allow us to see changes we make across our site much quicker. At the moment we have a cache that runs routinely every day at 1am. The issue I have is that if I want to make changes on the site such as catalog price rules, block changes and category updates, I don't see this until the following day.
Ideally I'd see these changes instantly. Thanks in advance for any tips.
Ronnie

To do this you have to programatically clean it and flush the cache
You can do this in magento 2, but you have to do it manually in your code
The way to do this is:
1. Inject these classed into you constructor dependency
private $_cacheTypeList;
private $_cacheFrontendPool;
public function __construct(
...
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
) {
...
$this->_cacheTypeList = $cacheTypeList;
$this->_cacheFrontendPool = $cacheFrontendPool;
}
2. Write this code inside of your class
$types = array('config','layout','block_html','collections','reflection','db_ddl','eav','config_integration','config_integration_api','full_page','translate','config_webservice');
foreach ($types as $type) {
$this->_cacheTypeList->cleanType($type);
}
foreach ($this->_cacheFrontendPool as $cacheFrontend) {
$cacheFrontend->getBackend()->clean();
}
Inside of the $types array you have all the type of cache you want to clean
Also magento 2 has areas so, you have cache tyes and frontend pool cache types as well.
This will refresh all the types you've entered inside you $types array.
Check magento documentation for all cache types that are available in here
to clean only the ones you need, instead of every single one of them

Related

Make the entire symfony app read-only

I need to set up a live demo of a Symfony app.
How can I make everything read-only? The users should be able to try all the features but not make any persistent change visible to others.
I could remove the INSERT and UPDATE privileges to the mysql user, but that would be an ugly error 500 when they try to save something...
Quick and dirty way to make your entire app Read-Only.
AppBundle/EventSubscriber/EntitySubscriber.php
namespace AppBundle\EventSubscriber;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\PreFlushEventArgs;
class EntitySubscriber implements EventSubscriber
{
public function getSubscribedEvents()
{
return [
'preFlush'
];
}
public function preFlush(PreFlushEventArgs $args)
{
$entityManager = $args->getEntityManager();
$entityManager->clear();
}
}
services.yml
app.entity_subscriber:
class: AppBundle\EventSubscriber\EntitySubscriber
tags:
- { name: doctrine.event_subscriber, connection: default }
I suppose that you've already made it. But if not:
Use dummy database. Copy it from original DB. Let them play. Drop it when you don't need it.
If you have no access to database creation and drop you can still do the trick. Just add temporary prefixes to table names in Doctrine entities. No need to rewrite the entire app, just a few lines. Run migrations to create new tables. Drop them whenever you want later.
Use virtual machine. Make a snapshot before the show. Roll back to the saved snapshot after the show.
These are more or less easy ways and they are platform independent.
Changing this based on the Symfony app level might have one of two disadvantages. You either do not save anything and thus your demo is not working so nice to show it to the customer. Or you have to do to much manipulations with the code and throw this huge work away right after the show.
Maybe you can use Session to do that or Memcache that you can implement in Symfony (Some examples are available on the web). Hope this will help.

Magento : Taking 35 Second to load 4 product's

I have created a extension that filters product collection based on a attribute.
Below is the controllers, block and view template code.
Controller
$url = Mage::getUrl('no-route');
if(Mage::app()->getRequest()->getParam('ajax')){
echo $this->getLayout()->createBlock('catalogextensions/bestsellers_home_list')
->setTemplate('catalog/landings/bestseller.phtml')
->toHtml();
}
else{
$this->loadLayout();
$this->getLayout()->getBlock('head')->setTitle('Besesellers');
$this->renderLayout();
}
Block product collection function
$storeId = Mage::app()->getStore()->getId();
$products = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter(array( array( 'attribute'=>'top_seller', 'eq' => '1' )));
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($products);
$products->getSelect()->limit(4,$this->get_cur_page());
return $products ;
Product collection is iterated on view.phtml template.
Now, it is taking 35 seconds to get the output, below are the statistics of execution that i was able to get by making use of PHP's microtime() function
For
Block(product collection ) it taking around 0.01 second
Template Rendering its taking around 0.12 second
But for controller function it is taking around 35 second
I am not able to find what to check, because controller function just create a Block at runtime.
*Note:I am making use of a Paid full page cache Extension "Mirasvit FPC".
how can i find it what is taking time
Server configuration is
30GB Ram with 4 vCPU.
Application server : Nginx + php5-fpm.
Version: Magento CE 1.8.0.1
Thanks.
You use a bit deprecated way to call the filters. I don't know if that can really influate the time of loading but using addFieldToFilter() would limit the calls to database and can be usefull. You can use :
Magento: Filter products by Status
filtering product collection on "is_salable"
The block is maybe not cached correctly. I mean the products are load each time the page is called.
Make sure your flat product is set to on and all attributes needed for your filters are set as filter.
If a listing comes from EAv tables it might consume lots of resources thus slowing down.
Additionally apply optimization tricks.
First, are you sure you need to add all attributes to select?
You can replace the
addAttributeToSelect('*')
with
addAttributeToSelect(array('attribute_code', 'attribute_code'))
Next, what exactly is that
$this->get_cur_page()
doing? Rest of code shouldn't make disastrous performance issues.
PS
This should have been a comment, but I'm unable to comment yet.

How do I get Magento to serve up the cached version of the page when I have unique URL parameters?

It's a simple question with no answer in search(google/bing/stackoverflow). The answer of course could be complicated.
I've read a couple articles on FPC within Magento, and have yet to really nail down where I need to add or create code so that when certain URL parameters are sent it serves up cached version of the page and not try and re-cache with the URL parameters.
http://www.kingletas.com/2012/09/how-does-magento-full-page-cache-works.html
So for example, when you go to http://www.example.com/shoes it loads the correct cached version. however, with google analytics and any other type of 3rd party reporting, especially with unique identifiers, it will reload the page as if it wasn't cached. So http://www.example.com/shoes?utm_key=A1537BD94EF07 would create a new cached version of that page and so on.
I would like to be able to exclude certain URL parameters and not all. Mainly any parameter I am using for tracking of customers.
As far as code, I have not come up with anything, due to the fact of the complexity of FPC and not having a dev site currently setup to test on.
Any leads as to where I can add this exception would be helpful, thanks!
EDIT: I would like to add that I am working with the Enterprise Edition. And using the Redis for cache.
I developed my own extension on the fix.
In short the get parameters are used in the cache ID. So in order to bypass this, I created an extension that changed the following:
/app/code/core/Enterprise/PageCache/Model/Processor/Category.php
Two functions where changed
protected function _getQueryParams()
AND
public function getPageIdWithoutApp(Enterprise_PageCache_Model_Processor $processor)
/app/code/core/Enterprise/PageCache/Model/Processor/Default.php
One function was changed
public function getPageIdWithoutApp(Enterprise_PageCache_Model_Processor $processor)
Once changed, it no longer created the cache ID with my specified tracking parameters.
example:
public function getPageIdWithoutApp(Enterprise_PageCache_Model_Processor $processor)
{
$queryParams = $_GET;
ksort($queryParams);
/**
* unset known tracking codes
*/
unset($queryParams["trk_msg"]);
unset($queryParams["trk_contact"]);
unset($queryParams["utm_source"]);
unset($queryParams["utm_medium"]);
unset($queryParams["utm_term"]);
unset($queryParams["utm_campaign"]);
unset($queryParams["utm_content"]);
/** End Edit */
$queryParamsHash = md5(serialize($queryParams));
return $processor->getRequestId() . '_' . $queryParamsHash;
}

How to implement magento cache hole punching for shopping cart block

Im using Magento EE version 1.12 with Full page cache enabled
a) my product detail page is cached
b) as a result my shopping cart in this page doesn't show dynamic item count
c) so i am not able to show valid cart item count in my product detail page
steps i followed
1) I created a block and called from header.phtml
2) trying to make that topcart.phtml block not to be cached
As im a newbie in magento , i got some links for cache hole punching
I followed below links but no success
my file structure
app- code - local - Enterprise - PageCache ->etc - cache.xml
and PageCache - model -container - TopCart.php
code as shown below
help link one
help link two
link three
i created files
cache.xml and cart.php container file
<page_html_topcart>
<block>page/html_topcart</block>
<name>topcart</name>
<placeholder>PAGE_HTML_HEADER_CART</placeholder>
<container>Enterprise_PageCache_Model_Container_TopCart</container>
<cache_lifetime>36400</cache_lifetime>
</page_html_topcart>
this is my topcart.php container file looks like
protected function _getIdentifier()
{
$cacheId = $this->_getCookieValue(Enterprise_PageCache_Model_Cookie::COOKIE_CUSTOMER, '')
. '_'
. $this->_getCookieValue(Enterprise_PageCache_Model_Cookie::COOKIE_CUSTOMER_LOGGED_IN, '');
return $cacheId;
}
protected function _getCacheId()
{
return 'CONTAINER_TOPCART_' . md5($this->_placeholder->getAttribute('cache_id') . $this->_getIdentifier());
}
protected function _renderBlock()
{
$block = $this->_getPlaceHolderBlock(); //('page/html_header_cart');
Mage::dispatchEvent('render_block', array('block' => $block, 'placeholder' => $this->_placeholder));
return $block->toHtml();
}
kindly help me out with useful links and step
I faced the same issue. So, i think, the problem is, that we don't have cachable template in this case. So if you cache it the way you did (and as I did, too), you end up in a base64 encoded list of links in your cache file. To validate that, I uncompressed the files in var/full_page_cache - and here we go: the cart count is cached and won't be changed even if your cart changes, and it would be not replaceable on server side (at least not in a clean way).
The reason for this behaviour is a simple one: For FPC, you render templates only, passing some values. But the template only renders a list in that special case, accessing only one block method (getLinks). In your layout xml files, you will find some calls of "addLink", which feeds that block, that's why all the results become base64 encoded and end up in your cache file. They are not accessable by your container.
But I think, there is a way to fix that. Just collect the links you want to be rendered and create a custom template and a custom block for that. You'll now be able to cache it in a proper way.

Yii: Piggybacking on Session GC For Cart Cleanup?

I am building a shopping cart using the Yii framework. I have created a cart model to store the items the user adds to the cart, and I'm keeping track of the products that guest shoppers are adding to the cart by using a session_id field that stores the current session.
However, if the shopper abandons the cart or the session simply times out before they proceed to the checkout I find that I have a bunch of records in the cart table that need to be cleaned up.
I was thinking that the best way to do this would be to piggy back on the garbage collection process that Yii uses to clean up the session table, but I'm not sure how to do this, or even if this is the best way.
Am I on the right track here?
If so, how do I go about piggybacking on Yii's garbage collection?
I don't know much about PHP's session garbage collection, so I don't know if this is a better way to go than a cron job. The little I do know I just learned from Professor Google, and it makes me think relying on the session garbage collection may not be as reliable as you want:
How do I expire a PHP session after 30 minutes?
But it could work, I suppose. Kind of clever, actually, if it does. And in this case, you would need to override the gcSession() method in the CDbHttpSession class in the Yii core (assuming, as you say, you are using the database session storage). You can override this method very easily, actually, in your config.php file.
First, create your new MyCustomHttpSession class which extends CDbHttpSession (drop it in your /components folder probably). Be sure to add your new custom Cart garbage collection to the gcSession() function!
class MyCustomHttpSession extends CDbHttpSession
{
public function gcSession($maxLifetime) {
/**** ADD YOUR CUSTOM LOGIC HERE ****/
$sql="DELETE FROM {$this->sessionTableName} WHERE expire<".time();
$this->getDbConnection()->createCommand($sql)->execute();
return true;
}
}
Then, tell Yii to use your new MyCustomHttpSession class in the components configuration array:
'components'=>array(
'session'=>array(
'class' => 'application.components.MyCustomHttpSession',
'connectionID' => 'db',
'timeout'=>14400, // 4 hour session time
),
),
I did not test this, but it should work just fine. Good luck!

Resources