Create Simple Cache For CSB

In this post I will show you how to create a simple cache system for all your CSB scripts V.2.0 and up, you can add it to older versions also, just ask me how by opening a ticket. It will cache the index page, pagination, categories and the gallery pages allowing you to serve cached files to users wich will greatly reduce the load on your server since no MySQl queries will be made anymore, pages will also be shown to the user faster. Of coarse adding a cache system will only help you out once you have some traffic hitting your site.

The only technical requirement is that Apache needs the ability to create cache files into the cache folder. On all the server this code was tested, I never had to change the cache folder permission. But if for whatever reason your server doesn’t allow the creation of those files, I will show you how to solve it without having to change the cache folder permission.

In each cache code you will find the following line of PHP code. The number 180 is the expiration time of each cache file, it’s in seconds, so 180 means 3 X 60 seconds; so basically 3 minutes. You can raise that value, just don’t raise it too high; you might end up showing offline models…

 PHP | 
 
 copy code

1
2
$cache_time = 180;
3

The Index Page

First create a /cache/ folder in the root of your domain name so that its path looks like: yourdomain.com/cache/.

Then using your FTP client or file manager open the index.php file located in the root of your domain name.

Now let’s start adding the PHP code in order to create a caching system; on top of the file look for this:

 PHP | 
 
 copy code

1
2
<?php
3
 
4
// Required files
5
require_once 'cb-builder/cb.header.php';
6
 
7
?>
8

Now for Versions up to 2.3 replace it with this code:

 PHP | 
 
 copy code

01
02
<?php
03
 
04
error_reporting(0);
05
 
06
// Set cache
07
if (!empty($_SERVER['QUERY_STRING'])){
08
	$query_string = '?' . $_SERVER['QUERY_STRING'];
09
}
10
else {
11
	$query_string = '';
12
}
13
$cache_file = dirname(__FILE__) . '/cache/' . basename($_SERVER['PHP_SELF']) . $query_string . '.cache';
14
$cache_time = 180;
15
 
16
if(file_exists($cache_file) && time() - $cache_time <= filemtime($cache_file)){
17
	$c = @file_get_contents($cache_file);
18
	echo $c;
19
  	exit;
20
} 
21
else {
22
	unlink($cache_file);
23
}
24
 
25
ob_start();
26
 
27
// Required files
28
require_once 'cb-builder/cb.header.php';
29
 
30
?>
31

For Versions 2.3 and up replace it with this code:

 PHP | 
 
 copy code

01
02
<?php
03
 
04
error_reporting(0);
05
 
06
// Set cache
07
$requested_url = trim(rtrim($_SERVER['REQUEST_URI'], '/'));
08
if (!empty($requested_url)){
09
	$query = trim(ltrim($_SERVER['REQUEST_URI'], '/')) . '.cache';
10
}
11
else {
12
	$query = basename($_SERVER['PHP_SELF']) . '.cache';
13
}
14
 
15
if (substr_count($query, '.') <= 2) {
16
	$encoded_name = base64_encode($query);
17
	$file_name = md5($query);
18
	$cache_file = dirname(__FILE__) . '/cache/' . $file_name;
19
}
20
 
21
$cache_time = 180;
22
if(file_exists($cache_file) && time() - $cache_time <= filemtime($cache_file)){
23
	$c = @file_get_contents($cache_file);
24
	echo $c;
25
  	exit;
26
} 
27
else {
28
	unlink($cache_file);
29
}
30
 
31
ob_start();
32
 
33
// Required files
34
require_once 'cb-builder/cb.header.php';
35
 
36
?>
37

Then scroll down to the end of the index.php file and look for this:

 PHP | 
 
 copy code

1
2
</body>
3
</html>
4

Now replace it with this code:

 PHP | 
 
 copy code

1
2
</body>
3
</html>
4
<?php
5
	$cache_page = ob_get_contents();
6
	file_put_contents($cache_file, $cache_page);
7
?>
8

At last save the index.php file, reload your CSB website; make sure to reload your main index page and look in the /cache/ folder if a cache file was created, if yes you can continue adding a cache system to your galley page. If not you will need to do something in order to allow Apache to create cache files into the cache folder.

How To Troubleshoot This Issue

The easiest way is to set the permission to 777 for the cache folder, it is not something I would do or recommend. The best and secure way to do it is to change the user of the cache folder to Apache. You will need to open a Shell window, navigate to your cache folder and run this command:

chown apache /cache

If you don’t know what the hell I am talking about, open a ticket or hit me up on Skype I will gladly help you out. You can always try to ask your host to do it for you.

The only down side is if you need to delete those cache files for whatever reason, you will need to connect as root with your FTP client in order to do so or use my super deleter PHP script: which will delete anything inside a folder and that you can run by shell or by hitting it from your browser.

If you need that deleter script, feel free to ask me, I will send it to you by email.

The Gallery Pages

First navigate to the /cam/ folder or whatever you called it and create a /cache/ folder inside the cam folder so that its path looks like: yourdomain.com/cam/cache/.

Then using your FTP client or file manager open the index.php file located in the /cam/ folder.

Now let’s start adding the PHP code in order to create a caching system; on top of the file look for this:

 PHP | 
 
 copy code

1
2
<?php
3
 
4
// Required files
5
require_once 'cb.header.php';
6
 
7
?>
8

Now replace it with this code:

 PHP | 
 
 copy code

01
02
<?php
03
 
04
error_reporting(0);
05
 
06
// Set cache
07
if (!empty($_SERVER['QUERY_STRING'])){
08
	$query_string = '?' . $_SERVER['QUERY_STRING'];
09
}
10
else {
11
	$query_string = '';
12
}
13
 
14
$encoded_name = base64_encode(basename($_SERVER['PHP_SELF']) . $query_string);
15
$file_name = md5($encoded_name);
16
$cache_file = dirname(__FILE__) . '/cache/' . $file_name . '.cache';
17
$cache_time = 180;
18
if(file_exists($cache_file) && time() - $cache_time <= filemtime($cache_file)){
19
	$c = @file_get_contents($cache_file);
20
	echo $c;
21
  	exit;
22
} 
23
else {
24
	unlink($cache_file);
25
}
26
 
27
ob_start();
28
 
29
// Required files
30
require_once 'cb.header.php';
31
 
32
?>
33

Then scroll down to the end of the index.php file and look for this:

 PHP | 
 
 copy code

1
2
</body>
3
</html>
4

Now replace it with this code:

 PHP | 
 
 copy code

1
2
</body>
3
</html>
4
<?php
5
	$cache_page = ob_get_contents();
6
	file_put_contents($cache_file, $cache_page);
7
?>
8

At last save the index.php file, reload any gallery page and look in the /cache/ folder if a cache file was created, if yes, congratulation you have finished creating a caching system for your CSB. If not follow the “how to troubleshoot this issue” found above.

Regards,

Nick