Hot answers tagged

38 votes
Accepted

What are the cache options available to a Craft installation?

There are many different caching levels on a web server, some of them are Craft-specific, and some have nothing to do with Craft, but they often get confused. In addition to that, some of them you ...
  • 66.5k
37 votes
Accepted

Generating pending image transforms stuck in process

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, ...
  • 66.5k
32 votes

Can I disable template caching for local development?

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 ...
  • 23.6k
18 votes
Accepted

Adding active class to globally cached navigation

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 #} ...
  • 2,824
12 votes
Accepted

Caching not working on page with asynchronously loaded asset transforms

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 ...
  • 815
11 votes
Accepted

How can I Prevent CSRF Tokens from being Cached?

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 %} // ...
10 votes
Accepted

Varnish 4.x and Craft

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 ...
  • 7,248
10 votes
Accepted

Why does Craft set no-cache headers for all requests?

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: {...
  • 66.5k
10 votes
Accepted

Troubleshooting paginated content cached globally

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 ...
9 votes

How can I Prevent CSRF Tokens from being Cached?

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 %} ...
  • 23.6k
8 votes
Accepted

Long 'Deleting stale template caches'

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 ...
  • 66.5k
8 votes
Accepted

Is it possible to clear caches and re-index assets programattically?

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 ...
  • 2,957
8 votes
Accepted

Adding cache headers to Nginx configuation file breaks the /admin area

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. ...
8 votes
Accepted

Cacheing in PHP code for plugin

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 ...
8 votes
Accepted

How to disable template caching? devMode isn't doing it

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: {% ...
8 votes
Accepted

Are there any file-based static cache options for Craft CMS?

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 ...
8 votes
Accepted

How do I cache with the ElementAPI plugin?

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/...
  • 220
8 votes
Accepted

Caching queries not working as expected

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 ...
  • 11.1k
7 votes
Accepted

Does the cache tag behave differently for Mobile vs Tablet vs Desktop?

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 {...
  • 66.5k
7 votes
Accepted

Cache/ColdCache & checking for ungenerated image transforms

Set generateTransformsBeforePageLoad in your config/general.php file. 'generateTransformsBeforePageLoad' => true, Configures Craft to generate new image transforms right when getUrl() is called,...
7 votes
Accepted

Section Single Title Caching

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 votes
Accepted

Force browser to download latest CSS file

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"&...
  • 7,248
6 votes
Accepted

How to fix "ambiguous" column error generated when DeleteStaleTemplateCaches is run?

Give this a whirl, Mark: {% for image in craft.assets.relatedTo(tags).order('elements.dateCreated desc') %}
  • 66.5k
6 votes
Accepted

Deleting Stale Template Caches Failed

When a task actually fails (as opposed to getting stuck), Craft will log an error message explaining why. To fish it up, open craft/storage/runtime/logs/craft.log, and search for: Encountered an ...
6 votes
Accepted

{% cache %} always creates new cache data, never uses existing cached data

Update: This has been fixed for the next (post 2.4.2725) release. Turns out there is a bug in Craft where if you put cache tags around code that doesn't actually output anything, then duplicate keys ...
  • 66.5k
6 votes
Accepted

Busting Craft Cache

EDIT Craft 2.4 is now available, adding the deleteCachesByKey method. This means the following would be the best solution to this question: public function clearcache($key) { craft()->...
6 votes
Accepted

Resolving “Stuck” Background Tasks

The easiest way is to use a GUI MySQL client to connect to the Craft database and edit the table directly. Popular ones include Navicat and Sequel Pro, but there is a good list here of options for ...
  • 66.5k
6 votes

Resolving “Stuck” Background Tasks

If you want an even more user friendly way of doing it, install the Task Manager plugin by Bob Olde Hampsink, and you can do it from the Craft control panel.
  • 7,248
6 votes
Accepted

Good practice to "Clear Caches" before you "Backup Database"?

Is this helpful, or am I just being OCD? Completely OCD. :) The database backup class already excludes unnecessary tables like asset indexing data, sessions, template caches, etc. during a backup. ...
  • 66.5k
6 votes
Accepted

DeleteStaleTemplateCaches taking forever

This is a side-effect of Craft's amazing automagical cache system. :) Check out my answer on this question for an explanation of what happens. Regarding your question of how to fix this, part of the ...
  • 7,248

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