The Arcive of Official vBulletin Modifications Site.It is not a VB3 engine, just a parsed copy! |
|
Using Memcached to optimize vB hacks
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: Code:
connect to cache get data from cache if data expired: get data from database update cache PHP Code:
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: PHP Code:
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! |
#12
|
|||
|
|||
thanks 4 this article
|
#13
|
|||
|
|||
A small update: if your server has PHP stability issues, I recommend you wrap a class_exists('Memcache') conditional around the memcached calls.
|
#14
|
||||
|
||||
extra ordinary
will give this a try I got many mods that slows my forum down |
#15
|
|||
|
|||
Would it be possible to see the code you used for the showgroups page?
|
#16
|
||||
|
||||
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. |
#17
|
|||
|
|||
Well, if a mod isn't written to use the datastore then enabling memcached for the datastore won't change anything. And if it is written to use the datastore then it probably doesn't need to be changed. But I suppose you could argue that if you are going to change a mod then it makes more sense to change it to use the datastore.
|
Благодарность от: | ||
abualjori |
#18
|
|||
|
|||
One problem with the memcached is that it can't store anything over 1Mb, and it's slow at reading out large chunks of data. Not surprisingly, I've thus found the database datastore to be better (faster) than the memcached datastore as it contains quite a bit of data, especially if you're running vbseo. Therefore, caching the smaller stuff in memcache pays off, as does the extra effort taken to modify inefficient mods.
What I've done is I added a global $vbulletin->memcache state via global.php. As such, storing data is very easy as I don't need to create an instance of the memcache object every time I want to use it. On my board, using memcached to cache the forumhome "who's online" block cuts index.php's generation time from 0.15s to about 0.07s. This is because fully caching the listings saves hundreds of evaluations forumhome_loggedinuser. I think that in total, I cache 15 or so mods, and it really pays off. This includes my very own vb3 sidebar mod, which would otherwise take over a second to fetch the most recent data from the databse. One piece of advice I can give everyone is that you need to understand what memcache is good at prior to using it for caching. You don't want to write mods that rely on memcache to work, or cache things for more than an hour- as then, you're probably better off just using the database to begin with. --------------------- Regarding showgroups.php, let me start by saying that nobody ever visits that page. But if you still want to cache it, there are two approaches: you can save the result of the query and fetch it from the cache, or you can cache the entire page. Note that unless you have hundreds of users on your showgroups page, you probably won't gain much from caching. My forum has some 1,100 users listed, and takes between 1.5 and 2 seconds to generate if you don't cache. With the cache, it's spat out in under 0.1s, of which 0.07s can be attributed to memcache (again, on my server- this varies from site to site). Here's the rough idea, with the actual vb code cut out as required by the license: PHP Code:
--------------------- Another idea for those who want to be even more efficient is to get both the time and data in a single $memcache->get call, which will save you 1 get per pageload at the expense of fetching unecessary data if it turns out the cache need to be updated. You can also try playing around with $memcache->add, although I've found the behavior of this to be somewhat unpredictable. |
#19
|
|||
|
|||
FWIW I have come up with a new way to cache that doesn't have to check for a timestamp every time. This works especially nicely if you write your own memcached wrapper class.
PHP Code:
|
#20
|
|||
|
|||
I have now updated the article in the first post with my new approach, which only makes 1 memcache call per regular pageload, and 2 when the cache needs updating. The old approach used 2 calls per pageload and 3 when the cache needed updating
|
#21
|
|||
|
|||
Here's my latest update to the caching function to make things even easier. Simply specify a cache key and a callback function - if the data exists in the cache, it will be fetched. Otherwise, the callback will be called and the returned data will be stored in cache and returned to the user.
PHP Code:
$cacheobj->fetch('data_key', 'some_function'); See php's documentation on call_user_func for more info on what $callback can contain. |
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|