![]() |
|
Article Options |
Using Memcached to optimize vB hacks
by
![]() As we all know, there are lots of great hacks here at vB.org, but some of them are certainly not written with large forums in mind. On my site, www.pentaxforums.com, which averages 1,200-2,000 simultaneous members, I would love to install every useful hack that I come across, but I can't really afford to have silly statistics and gimmicks add global queries to the database. Many hacks have one or more of these issues: -Add global database queries -Use slow/redundant database queries -Repetitively perform strenuous computations I've therefore turned to Memcached to cache frequently-updated yet non-critical data in order to save queries and increase page generation time. I've applied this to the following hacks, just to name a few: -Top poster on forum home (5 minute caching) -Forumhome social group stats (5 minute caching) -Moderated posts / subscribed threads in notifications (5 minute caching) -Cyb advanced new posts (5 minute caching) In addition, I use this for: -Fully caching the current forum activity in index.php -Fully caching the output of showgroups.php All in all, I've saved a total of 5 queries on forumhome and 3 global queries by applying the cache, and also significantly reduced page generation time, which is the real biggie. For example, without caching, my forumhome would take about 0.30 seconds to generate. With caching, however, the generation time falls to about 0.07 seconds - that's over 4x faster! I'd like to share with you the code I wrote to accomplish the caching. It assumes you have Memcached installed on your server. This particular code is for the forumhome top poster hack. As you can see, it's quite simple in structure, and can therefore easily be adapted to other hacks as well. The general pseudocode is:
Here, $postcount['count'] and $threadcount['count'] is the data from the queries which ends up being cached. The nice thing is that even if the data is fetched from the cache and not the database, it can be accessed through the same variable. This is because you can store anything in memcache- even templates that have already been evaluated. If you currently don't have memcached installed on your server, it's quite easy to install using PECL. To check if you have it installed, upload a script to your server that contains the following code:
A few notes:
So, in conclusion, if used properly, this code can speed up your forum tremendously. If you have a big board, try giving it a spin! Also, if you're interested in reducing your server's memory load, look into installing APC for PHP. Note that on a production environment, it would be better to have a global memcache connection instead of initializing it every time you try fetching data. I hope you guys found this useful! Last edited by MoMan; 11 Sep 2011 at 22:09.. Reason: Updated with more efficient code |
||||||||||||
Views: 15753
|
Comments |
#2
|
||||
|
||||
How hard is this to adapt to xcache. Running only 2 servers and no reason to run memcache yet.
__________________
Immortal Guild - My Guilds Website No members have liked this post.
|
#3
|
|||
|
|||
I found the xcache API here: http://xcache.lighttpd.net/wiki/XcacheApi
So you'd basically want to change the memcache set and get calls to xcache_set and xcache_get No members have liked this post.
|
#4
|
||||
|
||||
I'd love to see a method working with xCache. I'm not able to at the moment but will try when I can, so if someone figures out this method, please post it for others to make use of~
Great idea btw MoMan.
__________________
Blame it on the girls.. or blame it on the boys~? No members have liked this post.
|
#6
|
||||
|
||||
Would it be possible to see the code you used for the showgroups page?
__________________
Immortal Guild - My Guilds Website No members have liked this post.
|
#7
|
|||
|
|||
I'm not going to post the whole thing as it's site specific and would likely break things on others' sites, but the general steps I followed are (note that there are other approaches as well, such as caching the entire query, but I figured it would be best performance-wise to just cache everything):
1. Strip out the navbar/header/footer from the SHOWGROUPS template 2. In showgroups.php, cache the evaled output of SHOWGROUPS in a variable called $HTML using the code model at the top of this thread 3. At the end of the file, print the output using GENERIC_SHELL. This will prevent caching of the navbar/header/footer, so that they are always current. Note that if you use multiple styles that don't share the same graphics, you either have to set $cache['datafile'] = 'something' . $styleid, or use a global replace for image folder paths. No members have liked this post.
|
#9
|
|||
|
|||
You should make of the Datastore system: http://members.vbulletin.com/api/vBu...Datastore.html
No members have liked this post.
Last edited by compwhizii; 15 Sep 2010 at 20:15. Reason: Wrong again |
#10
|
|||
|
|||
I've found that going through the datastore is much slower than this on-the-fly approach (which is still fine from a design standpoint as long as you use $vbulletin->config for the server info and an option setting for the caching time), since memcached isn't good at dealing with large amounts of data.
No members have liked this post.
|
#11
|
||||
|
||||
thanks 4 this article
No members have liked this post.
|
#12
|
|||
|
|||
A small update: if your server has PHP stability issues, I recommend you wrap a class_exists('Memcache') conditional around the memcached calls.
No members have liked this post.
|
#13
|
||||
|
||||
extra ordinary
will give this a try I got many mods that slows my forum down No members have liked this post.
|
#14
|
|||
|
|||
Would it be possible to see the code you used for the showgroups page?
__________________
افلام No members have liked this post.
|
#15
|
||||
|
||||
You are aware that there is an extra setting area for this in the config.php where you can switch the default datastore handler to memcached?
It's completely disastrous and unnecessary to re-code all hacks if this can be done with uncommenting a codeblock. Read this: https://www.vbulletin.com/forum/entr...percharge-your... I highly recommend to update the first post. No members have liked this post.
|
![]() |
«
Previous Article
|
Next Article
»
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Article Options | |
|
|
New To Site? | Need Help? |
All times are GMT. The time now is 07:41.