don't cache some HTML - php

I would like to know if I can exclude some of the HTML from being cached. I am using MediaWiki software. So any MediaWiki solution or any other PHP solution will work as well.
My Mediawiki pages are cached and I am implementing the site notice feature which expires after few days. But when pages are cached, it doesn't honor my expiration date and being displayed all the time. so I want to exclude that part of the code from being cached. I am implementing it as a MediaWiki extension.
Thanks

MediaWiki caching works in many layers. There are a number of server side caches, apart from the caching in the client. (As you might have noticed, MW i notoriously slow, unless you implement at least some of the caching functionalities.)
First of all you will want to figure out which sitenotices are cached. As I'm sure you are aware, there is more than one place where you can set the sitenotice:
MediaWiki:Anonnotice
MediaWiki:Sitenotice
$wgSiteNotice in LocalSettings.php
Through a few different extensions
Do they all stay on the page for too long?
Secondly, you can try and figure out where your sitenotice is cached:
Is there any difference if you are logged in/out? Some parts of the interface can be harder cached for anonymous users.
Does the message disappear if you clear you browsers cache?
Try and disable Varnish (or Squid), if you are using any of them.
Temporarily disable $wgEnableParserCache to see what difference it makes.
Set $wgCachePages = false; in your LocalSettings.php, to try and disallow client side caching

Related

Reduce Expires Header SEO

I am using Wordpress for my Content management system.
After a while I am trying to make it better performance wise and the tools I am using to analyse my website is Gtmetrix.
I have done a lot of performance optimization but there is one problem I can't solve, actually I don't even know when its being generated.
The problem is related to Expires Header for the following files:
connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.3
platform.twitter.com/widgets.js
platform.stumbleupon.com/1/widgets.js
apis.google.com/js/platform.js
www.facebook.com/impression.php/f38617d6c3bf89e/lid=115&payload=%7B%22source%22%3A%22jssdk%22%7D
I am also using Woocommerce.
Can you help me to remove this files from my website or at least add expire header to them? which I think is not possible since this file are being loaded from somewhere else right?!
You cannot change the headers sent by websites you don't control.
Facebook, Twitter, StumbleUpon and Google all control their own headers and have their own reasons for the expiration time and other cache settings that they send through.
You can only make these changes on a domain that you control. In this case your options are:
Consider alternative ways to connect to these social networks. For example, you could code your own custom scripts, or find a Wordpress plugin that will run locally on your page (even with this, you may not totally be able to remove reliance on these external scripts, depending on the exact functionality you want)
Don't get hung up trying to get good results for everything GTMetrix tells you. Don't get me wrong, GTMetrix's suggestions are great and you can learn a lot from them, but it's rarely possible to do absolutely everything 100% of the time. Do your best, do what's under your control, and you'll definitely notice your site's speed (and hopefully ranking) increase.
For what it's worth, installing a local caching plugin like W3 Total Cache generally deals with several of GTMetrix's metrics in one go!

Is it problematic to cache files for too long?

I discovered https://developers.google.com/speed/pagespeed/ the other day and have improved my website's page speed from ~75 to ~95 now.
One of the last few things it recommends is that I:
Leverage browser caching: Setting an expiry date or a maximum age in the HTTP headers
for static resources instructs the browser to load previously downloaded resources
from local disk rather than over the network.
The cache time for my main javascript and css files is set to 2 days, Google suggests I set it to at least 1 week. They also suggest that I do the same for html and php files.
What would happen to my users if I decided to make a large website change and they had just cached my website yesterday (for 1 week)? Would they not see the changes on my website until 1 week later?
Also, since my website contains a control panel and has some dynamically generated PHP pages, is there any reason for caching any of it? Wouldn't my server still be churning through php script and generating new content every time they logged into their account?
You probably doesn't want to cache your HTML and PHP in visitors browsers. However you might want to cache that in a layer you have more control over, like PHP opcode caching with APC and a reverse proxy like Varnish.
For the static assets, like your JavaScript and CSS files, it should be safe to cache them a year or more. If you make a change to them you can just update their URL to say mystyles.css?v=123 and browsers will think it's a whole different file from mystyles.css?v=122 or even just mystyles.css.

Caching website by Codeigniter or 3rd party caching web application?

I'm very new to the concept "caching", so excuse me if my question is too simple.
So,I'm using Codeigniter(PHP framework) and it supports page caching, simply by doing this $this->output->cache(n)//n: number of minutes to remain cached
(I think) Codeigniter's caching will store any requested page in a cache file, and get the page when needed immediately.
Also there's a 3rd part web application called Vanish Cache, it sits between Apache and the client, then it will cache requested pages and re-send them again when needed, isn't that the same thing Codeigniter does, or is it different from that?
Wouldn't it be a waste to cache each page twice, by Codeigniter and Vanish?
assuming they do the exact same thing(cache pages and re-send them to the user),which one is more efficient for dynamic(database driver) websites?
On the surface, they do the same thing, however there are appropriate uses for different levels of cache.
A cache like Varnish, that sits between the web server and the application, provides very high performance. You use it for static content like CSS, static pages and dynamic content that changes very rarely.
An application cache provides a less performant but far more flexibile option. Usually you can cache by time, but also by application/request variables like "current user". This allows you to provide an state-dependant cache with a lot more fine control. For example, you could cache an object's detail page by it's last modified time in the database.

Caching, CDN - are they the same for PHP Yii site? How to use it for a dynamic php site?

I have a dynamic php (Yii framework based) site. User has to login to do anything on the site. I am trying to understand how caching and CDN work; and I am a bit confused.
Caching (memcache):
My site has a good amount of css, js, and images. I've been given to understand that enabling caching ("memcache"?) will GREATLY speed up my site. But this has me confused. How does caching help? I mean, how can you cache something that's coming out of DB for each user separately? For instance, user-1 logs-in, he sees his control panel. User-2 logs-in, user 2 will see their control panel.
How do I determine what to cache? Plus, how do I enable caching (memcaching)?
CDN:
I have been told to use a content delivery network like CloudFlare. It is suppose to automatically cache my site. So, when my user-1 logs in, what will it cache? Will it cache only the homepage CSS, JS, and homepage images? Because everything else requires login? What happens when user logs-out? I mean, do "sessions" interfere with working of a CDN?
Does serving up images via CDN reduce significant load on my server? I don't have much cash for getting a clustered-server configuration. So, I just want my (shared) server to be able to devote all its resources in processing PHP code. So, how much load can I save by using "caching" (something like memcache) and/or "CDN" (something like CloudFlare)?
Finally,
What would be general strategy to implement in this scenario for caching, cdn, and basic performance optimization? do I need to make any changes to my php-code to enable CDN like CloudFlare and to enable/implement/configure caching? What can I do that would take least amount of developer/coding time and will make my site run much much faster?
Oh wait, some of my pages like "about us" page etc. are going to be static html too. But they won't get as many hits. Except for maybe the iFrame page that will be used for my Facebook Page.
I actually work for CloudFlare & thought I would hop in to address some of the concerns.
"do I need to make any changes to my php-code to enable CDN like
CloudFlare and to enable/implement/configure caching? What can I do
that would take least amount of developer/coding time and will make my
site run much much faster?"
No, nothing like a need to re-write urls, etc. We automatically cache static content by file extension. This does require changing your DNS to point to us, however.
Does serving up images via CDN reduce significant load on my server?
Yes, and it should also help most visitors access the site faster and save you a fair amount on bandwidth.
"Oh wait, some of my pages like "about us" page etc. are going to be
static html too."
CloudFlare doesn't cache HTML by default. You use PageRules to setup more advanced caching options for things like static HTML.
Caching helps because instead of performing disk io for each user the data is stored in the memory, ie memcached. This provides a SIGNIFICANT increase in performance.
Memcache is generally used for cacheing data ie query results.
http://pureform.wordpress.com/2008/05/21/using-memcache-with-mysql-and-php/
There are lots of tutorials.
I have only ever used amazon s3 which is is not quite a cdn. It is more of a storage platform but still it helps to take the load off of my own servers when serving media.
I would put all of your static resources on a CDN so your own server would not have to serve these. It would not require any modifcation to your php code. This includes JS, and CSS.
For your static pages (your about page) I'd make sure that php isn't processing that since there is no reason for it. Your web server should serve it directly.
Cacheing will require changes to your code. For cacheing a normal flow is:
1) user makes a request
2) check if data is in cache
3) if it is not in cache do the DB query and put it in cache
4) if it is in cache retrieve it
5) return data.
You can cache anything that requires disk io and you should see a speed up.
Memcached works by storing database information (usually from a remote server or even a database engine on the same server) in a flat file format in the filesystem of the web server. Accessing a flat file directly to retrieve data stored in a regulated format is much much muuuuuch faster than accessing that data from a remote query each time. This is typically useful when you have data that can be safely stored for certain periods of time as it is not subject to regular changes.
The way this works is that if you want to store a user's account information in a cache to speed up loading pages where that user is logged in. You would load the information and cache it locally. On any subsequent requests for that data, it will load in a fraction of the time it normally would take to load that information from the database itself. Obviously you will need to make sure that you update/recache that information if the user changes it while logged in, but you will greatly reduce the time it takes to serve up pages if you implement a caching system that can minimize the time spent waiting on the database.
I'm personally not familiar with CloudFlare so I can't offer any advice to that effect, but in terms of implementing caching in your application, you should check out:
http://code.google.com/p/memcached/wiki/NewOverview
And read the rest of the Wiki entries there which cover installation/implementation/etc. That should get you started on the right track.

php "page caching" solution suggestions for CMS Applications

Most examples use time-based cache expiration. I'd like to read more about file caches (where the database is called only when there is no file in a given directory). This is for a basic information site with CMS functions made with php/mysql. My searches are returning too many sites on web applications. Adding CMS to the search returns script repositories. I'd appreciate your suggestions.
It's not hard to write something like this yourself. Use file_exists() to check whether a specific file exists, or glob() how many files matching a given pattern there are.
I use a page build system...
Each page created is given a guid - when a request comes in for the page check to see if a file in the cache named GUID.xxx serve it if not build the page and cache.
On editing a page (or if its expiration has passed) delete the file from the cache.
You can elaborate at will as to how the expiration is determined/administered and what protions of the page to cache and which to allow dynamic builds per request...
I'm not quite sure what you're looking for.
If you're talking about generating a page (from the CMS) and placing it at the requested URI (so the next request bypasses even the CMS) - it's possible, but you make refreshing the 'cache' a little difficult.
However, what you may be looking for is just a server side cache (as opposed to telling the browser how long to cache a page). Those are usually file or memory based, and if you place the caching mechanism high in the CMS flow (perhaps where it processes the requests), you'll be caching a large portion of page creation.
Some cache libraries let you set an unlimited lifetime (for example Zend_Cache), leaving the cache maintenance up to you. That may be what you're looking for.

Categories