Memcached button.php for Aardvark Topsites
This mod replaces button.php with a simpler, ultra fast memcached based button.php for Aardvark Topsites.
Memcached allows you to cache data in the servers’ memory and is very simple and fast.
First off, this mod isn’t for everyone as most servers do NOT have memcached installed.
If you don’t understand this, ask your host to installed them.
My topsite receives over 500 requests to button.php per second, I find the stats interesting so I didn’t want remove it.
After I switched to this my server load went from 80%+ to less than 5%.
This is not really the right way to use memcached although it works very well and has saved me a lot of hassle.
Before you do anything, back up your database!
This mod is released as-is, use it at your own risk.
I will not be held reponsible if anything happens to your site or server.
Requirements:
- memcached installed on the server
- memcache php module
- crontab / cron jobs to trigger update_memcache.php
Contents of this mod:
- button.php – rewritten to use memcached
- update_memcache.php – periodically updates the database with the collected statistics and resets the memcache
Possible problems:
Buttons containing stats no longer work which is probably a good thing as they put a massive load on the server.
Installing memcached:
Debian / Ubuntu:
(sudo) apt-get install memcached
CentOS: (may need the rpmforge repo enabled)
yum install memcached
Installing memcache php module:
Using PECL which comes with php:
pecl install memcache
You could always compile both from source if you wish.
Installing this mod:
- Overwrite button.php with the new version included in this mod.
- Copy update_memcache.php to somewhere on your server
If you place it outside of the aardvark root, update the require_once(…) to the correct path - Add the following to settings_sql.php and change the settings to suit your site
$CONF['memcached_host'] = "127.0.0.1"; //Address of memcached server, usually localhost $CONF['memcached_port'] = '11211'; //Port used by above server$CONF['button_num_enabled'] = false; //Do you want to use multiple buttons depending on rank, e.g. 0.png, 1.png... $CONF['button_num_count'] = 20; //How many buttons have you created? $CONF['button_num_url'] = "<a href="http://www.domain.com/images">http://www.domain.com/images</a>"; //Where are the images located? $CONF['button_num_type'] = "png"; //The images extension $CONF['button_single_url'] = "<a href="http://www.domain.com/images/button.png">http://www.domain.com/images/button.png</a>"; //Url of the main image, used if button_num_enabled is false or rank is higher than button_num_count
- Create a cronjob to trigger update_memcache.php every so often.
I recommend every 15 minutes although it really doesn’t matter.
Every 15 minutes: */15 * * * * php /path/to/update_memcache.php
And you’re done! You probably want to run update_memcache.php manually now so that button.php has data to work with and doesn’t have to wait for the cron to kick in.
If you have any questions or problems, leave a comment!