Hot answers tagged

38

Generally you’ll just want to cache areas of your templates that are going to trigger a lot of database queries, or fetch a lot of data, since those are going to have the biggest hit on your site’s performance. Matrix field loops that have relationship fields inside them (Entries/Assets/etc.) are a good example, since each Matrix block is going to come with ...


37

More of a "How do I debug stuck tasks" answer, but it should help you. Open up your craft_tasks table. You'll see one task in with a 'status' of 'running' and if you refresh the table several times, the 'currentStep' value for that task is no longer incrementing. That's your stuck task. Set that task's status to 'pending', and reload any page in the ...


32

As of Craft 2.4, it is now possible to use the enableTemplateCaching config value. 'enableTemplateCaching' => false, Simply add that to your local environment in config/general.php, and it'll disable the cache tags entirely.


32

There are many different levels of caching on a web server, some of them are Craft-specific and some that have nothing to do with Craft, but they often get confused. In addition to that, some of them you have control over and some of them you get for free. PHP Accelerators These are web server extensions that are installed to provide byte-level PHP ...


30

When working with multiple environments you likely want to disable caching for all but one environment, because Craft saves the absolute url to entries/assets in the cache. I achieved this by setting a custom variable in general.php 'craft.dev' => array( 'cache' => true, 'environmentVariables' => array(...), ) '192.168.178.20' => ...


18

I can't actually reproduce your problem but here's a couple of things to check and try: Clear your browsers cache! Did you re-upload through Assets with the same filename and click replace? Purge all the caches via the Clear Caches button on the settings page Update Asset Indexes on that same page Failing those, manually delete the contents of the /craft/...


18

Yes, you can! The first thing you need to do is to add the entire cached content into a single variable: {% set navigation %} {% cache globally for 3 years %} {# YOUR CACHED CONTENT #} {% endcache %} {% endset %} Then you need to add some dummy placeholder tags on your <li> tag and your <a> tag which then can be used by |...


17

When working with multiple environments you most likely want to disable caching for all but one environment, because Craft saves the absolute URL to entries/assets in the cache. I achieved this by setting a custom variable in general.php 'craft.dev' => array( 'cache' => true, 'environmentVariables' => array(...), ) '192.168.178.20' => ...


12

Seems to be the case that Craft won't attempt to resolve these /cpresources/transforms/ URLs until the asset is actually requested, which can mess with a setup like PictureFill where there is no guarantee that the transformed assets will ever be loaded. Solution in my case was to to set 'generateTransformsBeforePageLoad' => true, in your Craft config ...


12

Are you using PHP 5.5? If so, opCache is enabled by default and that’s probably causing your issue.


10

I'm by no means a Varnish pro, but I'm currently working on a project where we're running Varnish 4.0 in front of our nginx servers. I've uploaded an example of our VCL 4.0 file in this gist. It is based on the Varnish 4.0 template made by Mattias Geniar with some adjustments for Craft. It is NOT an exact port of the 3.x file you're referencing, so it's ...


10

I ran into this today. I thought craft.request.path used to include the pagination page number but it doesn't appear to anymore. Maybe it was always like this though. Regardless, this is how I'm now handling it: {% cache globally using key craft.request.path ~ '/p' ~ craft.request.getPageNum %} ... {% endcache %}


10

You can do something like this: // First, set all your cached content to a variable: {% set myContent %} {% cache %} {# YOUR CACHED CONTENT #} %%MY_CSRF%% {% endcache %} {% endset %} // Then, run a replace on your cached content to insert the new CSRF {{ myContent|replace({'%%MY_CSRF%%': getCsrfInput()}) }}


9

Craft doesn't explicitly set any caching headers in the response, but you can control exactly how you want your caching headers to behave from your templates with the {% header %} tag. For example: {# Tell the browser to cache this page for 30 days #} {% set expiry = now|date_modify('+30 days') %} {% header "Cache-Control: max-age=" ~ expiry.timestamp %} {...


9

The trick is to inject the CSRF field using JavaScript. Omit the standard getCsrfInput Twig tag. Give your form(s) the class of csrf. Add this Twig code outside of the cached area: {% includejs %} $(function() { $('form.csrf').prepend('<input type="hidden" name="{{ craft.config.get('csrfTokenName') }}" value="{{ craft.request.getCsrfToken }}"...


8

For those interested: Disabling opCache in Mamp: Go to: /Applications/MAMP/bin/php/php5.5.3/conf/php.ini. OP Cache is at the bottom of the file. Comment out the following lines with a semicolon ;: [OPcache] ;zend_extension="/Applications/MAMP/bin/php/php5.5.3/lib/php/extensions/no-debug-non-zts-20121212/opcache.so" ; opcache.memory_consumption=128 ; ...


8

The {% cache %}, by default will cache it's contents on a per URL and per locale basis. A lot of times when we see the behavior you're seeing, that's not the desired behavior and you should be using the {% cache globally %} parameter, where Craft stores the cached segment only on a per locale basis and drastically reduces the number of rows in the database ...


8

The {% cache %} tag will detect if there are any ungenerated image transforms within it. If there are, it will hold off on caching the template until the next request, so that the temporary image URL won’t get cached.


8

Craft rewrites the location of those files using a query string, which is what's causing nginx to lose track of them. Using the try_files directive should force nginx to find and serve them properly. location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { try_files $uri /index.php?$query_string; expires 7d; add_header Pragma "public"; add_header Cache-...


8

You want to take a look at the CacheService class which is the service that allows you to programmatically interact with the internal cache. This is an example of how to set some cache that expires in 3600 seconds (one hour). Set the expiration to 0 if you never want it to expire. craft()->cache->set('some_key', 'some_value', 3600); Just checkout ...


8

It is true that devMode does not turn off caching. What I do is define a flag in general.php: 'cacheThings' => true, // true by default, but usually false for dev and then in my templates: {% cache if craft.config.cacheThings %} update Since version 2.4 there is a new config value: enableTemplateCaching, so all you need to do now is set this false in ...


8

Someone has recently developed a standalone static HTML caching plugin for Craft CMS which does similar to what you're after: HTML Cache plugin for Craft CMS This Craft plugin will generate static HTML files for your website. No need for Reddis/Varnish setups anymore! Improves the speed drastically: 300-1500MS to 2-50MS (depending on server setup) if ...


8

The ElementAPI plugin itself doesn't seem to offer a caching solution, but there is a fork from a guy which has implemented a caching option cache and cacheTime: https://github.com/theolampert/ElementAPI Update: They have introduced a new cache option in the official ElementAPI plugin (version 1.5).


8

The {% cache %} tag only caches the HTML output. That's it. In your example above, nothing is cached, and categoryProducts will be an empty set. One thing you could do is to cache each entry individually: {% set categoryProducts = craft.entries.section('products').relatedTo(category) %} {% set categoryProducts = shuffle(categoryProducts) %} {% for entry ...


7

I guess it depends. So, behind the scenes, Craft can have different cache drivers. You might use the db or file cache, or something like Redis or Memcache. Took a peek at ClearCachesTool, and it looks like it's doing the things you mentioned: craft()->db->createCommand()->truncateTable('assettransformindex'); craft()->db->createCommand()->...


7

Craft makes no distinction whether it's caching for mobile, tablet, desktop, etc., so it'll need to be taken care of on the template end. You can easily do that with the "using key" parameter of the {% cache %} tag, though. {% if craft.request.isMobileBrowser %} {% set cacheKeyType = 'mobile' %} {% else %} {% set cacheKeyType = 'desktop' %} {% ...


7

Set generateTransformsBeforePageLoad in your config/general.php file. 'generateTransformsBeforePageLoad' => true, Configures Craft to generate new image transforms right when getUrl() is called, rather than when the browser first requests the image.


7

For anyone stuck on this -- when changing a Section's name you have to goto the entry under Entries and hit save. This will update the mysql row / title.


7

There are several plugins that can do this. With my plugin Stamp, you can add a timestamp to the filename or path in the following formats: <script src="/assets/build/js/1399647655/scripts.js"></script> <script src="/assets/build/js/scripts.1399647655.js"></script> <script src="/assets/build/js/scripts.js?ts=1399647655"></...


6

Sure... we've seen Craft installs with several hundred thousands entries/users/assets/etc. running in the wild, but there really isn't a simple answer to this question. As you pointed out, performance can be heavily dependent on the server and environment that Craft is running on, but that goes for any web application. Performance on the front-end is also ...


Only top voted, non community-wiki answers of a minimum length are eligible