Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+25 votes
3.2k views
in Q2A Core by
edited by

It took me a while but I finally figured out a good caching solution and worked around the various issues. The changes are now on the 1.8 branch on GitHub for anyone that wants to help test it.

Be warned: this is beta code! It may not be suitable for production use on live websites. Also note that if you upgrade from 1.7, you won't be able to downgrade again due to changes in how user passwords and IPs are stored.

But if anyone is able to try it either on localhost or a separate test install on their server, it would be much appreciated. I will be doing some testing on my own server.

To enable caching, the following steps need to be taken:

  1. Make a folder to store the cache files and chmod it to 777. Note: for security reasons the folder must be outside your document root. In other words you must not be able to view cache files in your browser like example.com/cache/123abc456def
  2. Set the QA_CACHE_DIRECTORY constant in qa-config.php to the location you made above (see qa-config-example.php for an explanation).
  3. Go to Admin > Caching and tick "Enable caching".

Currently, question caching only applies for logged-out users. The vast majority of visits to my site were not logged in, I think that will be typical for most sites. The following things are cached:

  • Question data (answers, comments, etc) for all questions older than 7 days.
  • Related questions widget
  • Category widget

There is plenty more that can be cached, such as question lists, users page and tags page, but the above are by far the most common queries.

If you find any problems please post an answer with details. Thanks!

UPDATE: I've found a small issue with the upgrade queries when using a shared user base (the QA_MYSQL_USERS_PREFIX option). Will fix asap. Now resolved.

Q2A version: 1.8-beta
by
Cache is really required. Excellent! Is there option to flush cache also?
by
@ProThoughts: not yet but I plan to add it in future :)
by
Nice to see "Related questions widget" in the cache scenario!
by
Wow, what a addition... :) I am impressed...
Thank you Scott. I will try to create a test install soon...
Thank you for the excellent job... :)
by
My first question for you: Does this break plugins? Do they need to be updated?
by
@truthonlytruth no, there shouldn't be any problems with plugins.
by
I have been setting a test domain lately. But I am unable to understand your first point for making a folder. Can you tell exactly where I need to place the folder and from where the cache files need to be copied? Or will those cache files be generated automatically?
by
@Gurjyot The cache files are generated automatically. You just need to make a writable folder where Q2A can put them, similar to the blobs directory.

For the location, it needs to be at least one folder above your root. So whichever folder is the base for your site (where index.php is), go up one level. Most hosting is set up so when you log into the FTP your files go in a folder like "public" or "public_html". You need to put your cache folder alongside that (but not inside it). Hope that's making sense!
by
Cool. Nice job Scott.
I have one question. Not logged in show cache file. Its nice. But this is site show count question views. Question views work after for not logged users?
by
@yerbol Yes, question views still work. That was one of the issues that I needed to work around. Instead of checking 'lastviewip' which might be cached, we do that in the update query instead.
by
My provider enabled mysql cache which caches all queries (this is what you want to do as far as i see manually). Now the q2a pages load lightning fast. It's just awesome. Maybe we don't need the caching solution at all? Ref. http://dev.mysql.com/doc/refman/5.7/en/query-cache.html
by
@q2apro There are a few differences with Q2A's system:
1. Q2A cache avoids going to MySQL so it should be faster in theory.
2. For question pages, multiple queries are cached together rather than checking each one individually.
3. MySQL query cache gets invalidated as soon as anything is written to a table. So if someone posts a new question, ALL caches for all question pages are removed. In Q2A they stay until the expiration time comes.
by
Hi Scott. I'm on 1.8 branch, did a database upgrade and set the caching directory and did chmod. But I'm not finding the option to enable caching :O
by
@arjunsuresh Have you set QA_CACHE_DIRECTORY in your config? If you've done that there should be a "Caching" menu link in admin, next to "Spam".
by
Thanks Scott. It worked. I got confused with "QA_CACHING_DIRECTORY".
by
Oops, silly typo. Sorry about that.
by
No problem :) It works well - no issue so far. I have a question on cookie lifetime - why is it expiring on browser close? Like in this website or in most of them, we expect the users to return every couple of days and not wanting to login again.
by
If you tick 'remember me' that doesn't happen.
by
Thanks Scott. I guess the issue was me changing some cookie option.
by
I setup demo site as per above instruction, you can see cache feature enabled in admin panel.
http://question2answer.org/qa/54155/q2a-v1-8-demo-site

demo site:
http://demo18.question2answer.info
by
I'm using version 1.8 but not showing me caching
by
Have u done these two steps? as soon as you do, you will see option in admin panel.

1. Make a folder to store the cache files and chmod it to 777. Note: for security reasons the folder must be outside your document root. In other words you must not be able to view cache files in your browser like example.com/cache/123abc456def

2. Set the QA_CACHE_DIRECTORY constant in qa-config.php to the location you made above (see qa-config-example.php for an explanation).
by
Thanks
(The directory defined as QA_CACHE_DIRECTORY is not writable by the web server.)
What is the solution?
by
You need make it writable then only it will work. by default this parameter is in comment section, move it outside. If you are not able to do then contact host provider.
by
Hi q2a.info, parameter is not in comment section. its already visible only like below
    define('QA_CACHE_DIRECTORY', '/path/to/writable_cache_directory/'); ===> this is in comment section
*/
    define('QA_CACHE_DIRECTORY', '/path/to/writable_cache_directory/'); ==> this is visible.

But still getting same error, can you plz help to resolve this.

1 Answer

+2 votes
by
Hello Scott,

Is this a plugin or a part of core Q2A? I feel better to have it as a plugin so that other people can add more functionality like CDN etc later and release their version.

Thanks!
by
It's part of the core, but potentially we could make it so the caching class can be overridden if you want slightly different functionality (e.g. using Memcached instead of storing in files).

What do you mean about the CDN?
by
That will be good! others can write diff plugins.

CDN - Content Delivery Network, like Amazon CloudFront https://aws.amazon.com/cloudfront/ or maxCDN (https://www.maxcdn.com/)

Below two plugins are most popular for Wordpress which support CDN.
https://wordpress.org/plugins/w3-total-cache/ (I use this one on my site for CDN)
https://wordpress.org/plugins/wp-super-cache/
by
I think you've misunderstood what this cache does. It doesn't have anything to do with static files like CSS/JS/images. It caches the database queries, those results are used by Q2A itself and not served directly to users. So a CDN can't be used for this.
...