SunCache is a simple, fast, and powerful PHP dynamic cache class that uses the file system for caching.
- Initialization
- Caching Files
- Limited Time Caching
- Caching with Minified Content
- Caching with SEF URL
- Exclude Some Files from Caching
- Delete All Cached Files
- Delete a Specific Cached File
- Delete Specific Cached Files
Download all files (except Test directory), change the htaccess.txt file's name to the .htaccess (important), and move it to your cache directory.
To utilize this class, first import SunCache.php into your project, and require it. SunCache requires PHP 5.5+ to work.
require_once ('SunCache.php');
Simple initialization with default parameters:
$cache = new SunCache(true);
Advanced initialization:
$config = [
'cacheDir' => 'suncache', // cache folder path
'fileExtension' => 'scf', // cache file extension
'storageTime' => 24*60*60, // cache storage time (seconds)
'excludeFiles' => ['file1.php', 'file2.php'], // exclude files from caching (with extensions)
'contentMinify' => true, // cahe content minification
'showTime' => true, // show page load time
'sefUrl' => false // website sef url status
];
$cache = new SunCache(true, $config);
All config parameters are optional.
It will use default parameters that are set in the class if you don't specify the parameters while creating the object.
Cache files using parameters in an internal array
$cache = new SunCache(true, ['cacheDir' => 'suncache', 'fileExtension' => 'scf', 'storageTime' => 60*60, 'contentMinify' => true]);
Cache files using default parameters (set in class)
$cache = new SunCache(true);
Cache files using default parameters except the time (limited time caching)
$cache = new SunCache(true, ['storageTime' => 3600]); // or 60*60 (seconds)
Cache files with minified content (remove all unwanted characters)
$cache = new SunCache(true, ['contentMinify' => true]);
This may cause some problems with javascript code works (if you use js codes on the same page, inline, or top/bottom of the page).
Cache files with SEF URL (if you use HTML extensions instead of PHP)
$cache = new SunCache(true, ['sefUrl' => true]);
Exclude some specific files from caching.
$cache = new SunCache(true, ['excludeFiles' => ['file1.php', 'file2.php']]);
Don't forget to send the file names (with php
extension) in an array parameter.
This method deletes all cached files in the cache directory.
$cache = new SunCache(false, $config);
$cache->emptyCache();
Don't forget to create a new object with false
parameter.
This method deletes a specific cached file in the cache directory.
$cache = new SunCache(false, $config);
$cache->deleteCache('cachedFileName'); // file name (string)
Don't forget to create a new object with false
parameter.
You should send the file name (without extension) as a string parameter to the delete method.
This method deletes some specific cached files in the cache directory.
$cache = new SunCache(false, $config);
$cache->deleteCache(['cachedFileName1', 'cachedFileName2', 'cachedFileName3']); // file names (array)
Don't forget to create a new object with false
parameter.
You should send all file names (without extensions) as an array parameter to the delete method. If you send filename
as a file name, the class will delete all files containing filename
term (ex. filename, filename1, filename_xxx, xxx_filename_yyy, etc.).