MyBB Community Forums

Full Version: [VPS/Dedi] Set up a ramdisk file cache
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Skills needed: Ability to work with the terminal.
Requirements: At least a bit of ram over and linux.

Information:
I recently moved my cache away from memcached as some/most times memcached would be the slowest thing to be ran, specially for bigger forums taking sometimes 200 ms to do its things.

I tested quite many cachehandlers (Including my own) and found that apc was ok, db was oh dear no, and files was quite good if sat up with a ramdisk.

So how to set it up "properly":
Step 1: Make the folder
Start by making a folder in the cache folder called "cache" so it's /MYBB_ROOT/cache/cache.

Why: Basically when you restart the files in the folder will be gone and cuz the themes are stored there it isn't really ideal.

mkdir /var/www/cache/cache


Step 2: Chmod
Simply chmod the folder thus php can write to it.

Step 3: Make the ramdisk
Open /etc/fstab with your favorite editor (I prefer nano) and append to its own line,

tmpfs /path/to/cache/cache tmpfs size=200m,mode=0775,uid=www-data,gid=www-data 0 0

Do note:
Change the "/path/to/cache/cache" to your path

200m to the amount of ram is should max be able to use, it will not use ram that isn't being used by the files thus even if you insert it as 200m if your files are just 13 MB it will not use the full 200 MB only 13 MB

www-data is PHP-FPM & nginx; apache might use different so use a bit of googling skills to find out what yours uses.

Step 4: Mount the ramdisk
As easy as running the following command,

mount -a

Step 5: Fixing the cachehandler
Open up /inc/cachehandlers/disk.php and replace every

MYBB_ROOT."/cache

With

MYBB_ROOT."/cache/cache


Step 6: Activating it

Open up /inc/config.php and find

$config['cache_store']

Set it to

$config['cache_store'] = 'files';

Step 7: Verifying that it works
  • Debug view
    • Go into the debug view of the index, showthreads etc (?debug)
    • If it keeps writing to the database (prefix_datacache) it's not correctly writing to the files
  • See if it has saved the cache to files
    • Simply just check the cache folder.

And you're done.

For a bit more advanced people who like to save space and stuff:
Step 1: Install what you need

Install igbinary

You can jump over this and replace the igbinary_ in the file.

Step 2: Enable it

Ehm pretty straight forward in their installation guide

Step 3: Install the cachehandler

You could either write it to a new file or use disk.php, I prefer making it short so disk.php it is.

Replace the disk.php file with:
<?php
/**
 * MyBB 1.8
 * Copyright 2014 MyBB Group, All Rights Reserved
 *
 * Website: http://www.mybb.com
 * License: http://www.mybb.com/about/license
 *
 */

/**
 * Disk Cache Handler
 */
class diskCacheHandler
{
	/**
	 * Connect and initialize this handler.
	 *
	 * @return boolean True if successful, false on failure
	 */
	function connect($silent=false)
	{
		if(!@is_writable(MYBB_ROOT."cache/cache"))
		{
			return false;
		}

		return true;
	}

	/**
	 * Retrieve an item from the cache.
	 *
	 * @param string The name of the cache
	 * @param boolean True if we should do a hard refresh
	 * @return mixed Cache data if successful, false if failure
	 */

	function fetch($name, $hard_refresh=false)
	{
		if(!@file_exists(MYBB_ROOT."/cache/cache/{$name}.php"))
		{
			return false;
		}

		return @igbinary_unserialize(@file_get_contents(MYBB_ROOT."/cache/cache/{$name}.php"));
	}

	/**
	 * Write an item to the cache.
	 *
	 * @param string The name of the cache
	 * @param mixed The data to write to the cache item
	 * @return boolean True on success, false on failure
	 */
	function put($name, $contents)
	{
		global $mybb;
		if(!is_writable(MYBB_ROOT."cache/cache"))
		{
			$mybb->trigger_generic_error("cache_no_write");
			return false;
		}

		$cache_file = fopen(MYBB_ROOT."/cache/cache/{$name}.php", "w") or $mybb->trigger_generic_error("cache_no_write");
		flock($cache_file, LOCK_EX);
		fwrite($cache_file, igbinary_serialize($contents));
		flock($cache_file, LOCK_UN);
		fclose($cache_file);

		return true;
	}

	/**
	 * Delete a cache
	 *
	 * @param string The name of the cache
	 * @return boolean True on success, false on failure
	 */
	function delete($name)
	{
		return @unlink(MYBB_ROOT."/cache/cache/{$name}.php");
	}

	/**
	 * Disconnect from the cache
	 */
	function disconnect()
	{
		return true;
	}

	/**
	 * Select the size of the disk cache
	 *
	 * @param string The name of the cache
	 * @return integer the size of the disk cache
	 */
	function size_of($name='')
	{
		if($name != '')
		{
			return @filesize(MYBB_ROOT."/cache/cache/{$name}.php");
		}
		else
		{
			$total = 0;
			$dir = opendir(MYBB_ROOT."/cache/cache");
			while(($file = readdir($dir)) !== false)
			{
				if($file == "." || $file == ".." || $file == ".svn" || !is_file(MYBB_ROOT."/cache/cache/{$file}"))
				{
					continue;
				}

				$total += filesize(MYBB_ROOT."/cache/cache/{$file}");
			}
			return $total;
		}
	}
}

Step 4: Secure the folder

Anyone can read the files if you use this thus for nginx

location /cache/cache {
    deny all;
}