Drupal + page fragment caching - drupal

our drupal site has some user specific content. When we enable page caching the whole page is being cached. Isn't it possible to cache only fragments of the page. Or specify which fragments not to cache? Or even specify which pages not to cache. This way we can remove some stuff from the caches when logged in. Or don't use the cached versions when logged in.
I found this website already but it doesn't seem to work:
http://www.jpstacey.info/blog/2009/03/03/how-to-not-cache-a-particular-drupal-page
But this doesn't seem to work.
kind regards,
Daan

First, I second Ran Bar-Ziks suggestion of putting the 'not to be cached' data in a separate block and set that block to BLOCK_NO_CACHE, as it is the simplest solution.
If that is not possible, you can disable caching of specific pages, but contrary to the suggestion from the link you posted, I'd do this by preventing the page from getting cached in the first place (rather than removing the cache entry afterwards).
To do this, you can manipulate the global cache config setting temporarily on the page in question:
// Disable caching for this request cycle
$GLOBALS['conf']['cache'] = FALSE;
Where you put this code depends on the pages you want to exclude from caching:
If it is a custom node type coming from your own module, you'd put it in hook_view.
If you want to do this for a node type coming from other modules, you can put it in 'view' operation part on a hook_nodeapi() implementation.
** This would also also work for individual nodes, if you add a check for the node id before disabling the cache.
If you need to do this based on paths, you could put it in a hook_init() implementation, checking for the path (or path alias) to decide whether to disable caching or not.
It should be obvious that you need to clear caches first for any of these approaches to work, as they depend on preventing the page from being cached at all. They will not remove an already paged entry from the cache.

You can put the data that you don't want to be cached in block and use BLOCK_NO_CACHE to prevent this block from caching. It is very simple thing to do and explained in drupal.api: http://api.drupal.org/api/drupal/modules--block--block.module/constant/BLOCK_NO_CACHE/6

Yes! Drupal caches the menu by default, for example. That's usually on every page.
See the cache getters and setters (below). You can make your own by setting a unique cache ID.
http://api.drupal.org/api/drupal/includes--cache.inc/function/cache_get
http://api.drupal.org/api/drupal/includes--cache.inc/function/cache_set

Related

Is there short way to exclude layout page from ResponseCache

I want to cache page but the problem is that the button connect is
also cached because it's in the layout. Is there a way to exclude layout page from cache and .net core will generate only the layout again and the inner page from cache without cachetag
[ResponseCache(CacheProfileName = "Default")]
No. Response caching is literally caching the response, i.e. the entire HTML document. There is no concept of what is layout, a partial, etc. It's just an HTML document. You can use the cache/distributed-cache tag helper to cache portions of the view, but you cannot cache the entire response, if you only want to cache some portion (i.e. exclude the layout itself).
That said, you can certainly vary the cache on a particular header, whether a user is logged in or not, etc. So, depending on why you're wanting to not cache the layout, it might be possible to actually cache the whole response, including layout, and yet still present different versions.

Best way to let browsers refresh from cache on a live website?

It's about making changes in design (css-files and images) on a website which is already online and in use. I wonder what is the best-practice to make sure that visitors see the changes without clearing there browser's cache manually. Things that came in my mind:
change meta-tag - dismissed because I do not want the site to be ALWAYS loaded from the server
include the css-file with a parameter (like timestamp) after made a change
change the names of included images so that they are reloaded - means also change names in the files where images are included
?
What else could achieve the loading from server? Did I forget some advantages/disadvantages?
Possible duplicate of this post: How to control web page caching, across all browsers?
My favoured solution is to set a random number after you call the file e.g.
css/styles.ccs?628454548
images/sprite.gif?8356484894
You could use javascript/php or whatever to set those random numbers every time the page is called to the browser.

Drupal - nodes being loaded for no apparent reason

(Sorry this is rather a vague question. My attempts to be clearer [and indeed to be more code-oriented] have failed...) :-/
//
I've installed the Firebug for Drupal module, and I notice that it shows I'm apparently loading the same eight node objects on every page for no apparent reason. These are all of the same content type (the site uses many other content types).
It seems they are actually all the nodes of this one content type, excepting those produced as dummy content by the developer module.
I've flushed the cache multiple times.
Is there a way to work out where these nodes are being loaded from???
Install devel.module, add ddebug_backtrace(); inside the node_load() function. Reload the site. Now you should see 8 browsable backtraces which will tell you which function calls node_load().
At a guess, you probably have a block (from a view or module) which is querying those nodes on every request.
http://heydon.com.au/node/1044 has a short writeup on this behaviour. If so, the fix is to remove that block from the regions which are rendered, or configure it to only be displayed (and therefore rendered) on pages where you want it to be run.
Drupal caching should prevent those queries being run for anonymous users (depending on the caching and block settings, of course).
I've had this happen with 3rd party modules that were repeatedly calling node_load() needlessly. What I would suggest is for you to disable all 3rd party modules, retry you node loads and re-enable them one by one until you catch the misbehaving module.
Good-luck!

Drupal 6 - Disable Views cache for specific View

I have lots of views which don't change very often, and enjoy the benefit of caching. But I have 1 view which is used to display a random quote in a block in my sidebar, which I do not want cached. Is there a way to disable caching for this particular view? Specifically, I want to see a different quote every time I refresh the page.
UPDATE: I have Caching Mode set to Normal in my site's Performance settings, and I've tried going into the View and setting Caching = None (as well as in Block settings: Caching = Do not cache), and going into the Views tools page, and selecting "Disable views data caching", and it works when I'm logged in, but as soon as I logout, the quote stays the same after refreshing.
UPDATE: I'm beginning to think that if you have enabled page caching in Drupal, then all other cache settings are ignored (i.e. View and Block caching). Can anyone confirm this?
In the Views UI under the Basic settings of a particular display you have an option called Caching make that as none. And your view won't be cached. So you get random
quote everytime. :)
EDIT : Oops how did I miss what you were telling :(
Use Cache Exclude module to disable caching on the particular page. If your random block is on many pages, you may need to dig deeper to find an alternate solutions. All the best ;)
Suggestion: Randomize on the client side. Load 'em all up into a javascript array and write a quick function to select the random quote on page load. Unless there are hundreds of possible quotes it shouldn't weigh the page down too much, & you could exclude this one little sidebar feature from consideration when working on your caching strategy.
In fact even if there are hundreds of random quotes, you could use a combination of the two approaches. Grab 50 random lines w/ your module and them let javascript pick from there. To an end user it would be nearly identical.
The block may be being cached. Did you try to http://drupal.org/project/blockcache_alter ?
You can also directly change a block's cache setting in the database with something like:
update blocks set cache=-1 where bid=<blockbid>;
Setting this cache entry to -1 means the block will not be cached.
In addition, setting cache to 1 will cache the block per role, 2 will cache the block per user, 4 will cache the block per page, and 8 will cache the block exactly once (the same for all users, pages, etc).

How to serve different cached versions of a page depending on a cookie in Drupal?

The task is relatively straightforward:
A Drupal website displays a list of articles with thumbnails. Some visitors would like to view it without images by clicking on a button/link and have that preference saved.
e.g. http://patterntap.com/collections/index/
The problem is all visitors are anonymous and given certain traffic, page cache is enabled.
My idea was to use some simple JavaScript to set a cookie, refresh the page and depending on the cookie values (or its presence/absence) display or hide the images.
Except Drupal serves cached pages quite early and the only quick way to modify the cached version that I could find is by hacking includes/bootstrap.inc and add a custom class to the body classes then hide the images with css.
A very wrong approach, I know. But I wonder if there is a way to save different versions of a page and serve the correct version?
Edit:
need to keep the same uri
the js to show/hide the images without reload and set the cookie is already in place
hook_boot() is not really called for cached pages, so can't do it via custom module
.htaccess mods?
Edit/solution:
In the end went with Rimian's suggestion. But it is possible to accomplish the task using our own cache.inc implementation as seen in the Mobile Tools module. Specifically, by extending cache.inc and updating settings.php to include
$conf['page_cache_fastpath'] = FALSE;
$conf['cache_inc'] = 'path/to/my/module/my_module_cache.inc';
So let me get this right. You wanna hide some images on a cached page if the user chooses to?
Why don't you write some jQuery or javascript and load that into your cached page with all the rest of the document?
Then, the client/browser would decide to run your script and hide images depending on some parameters you passed along with the request to that page or in the cookie? The script gets cached and only runs when you call it.
If you were hacking the bootstrap for something like that you'd really need to be rethinking what you were doing. Crazy! :)
Also take a look at cache_get and cache_set:
http://api.drupal.org/api/drupal/includes--cache.inc/6
I'm not sure I 100% understand what you are trying to do but here are my thoughts. One of your root problems is that you are trying to access what is essentially different content at the same uri.
If this is truly what you want to do, then Rimian's suggestion of checking out chache_get and chache_set may be worthwhile.
Personally, it seems cleaner to me to have your "with thumbnails" and "without thumbnails" be accessed via different uri's. Depending on exactly what you are wanting to accomplish, a GET variable my be an even better way to go. With either of these two options you would hide or show your thumbnails at the theme layer. Pages with different paths or get variables would get cached separately.
If you want the visitor to be able to switch views without a page reload, then jQuery and a cookie would probably suite your needs. This wouldn't require a page reload and switching back and forth would be quite simple.

Resources