Download the PHP package modulework/cachework without Composer
On this page you can find all versions of the php package modulework/cachework. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download modulework/cachework
More information about modulework/cachework
Files in modulework/cachework
Package cachework
Short Description A simple way of caching information to disk
License Apache 2.0
Informations about the package cachework
CACHEWork
A simple way of caching information to disk
Installation:
- Place the cache.php file into your application folder
-
Include it
- and initate it:
Make sure that you enter the relative path to the cache directory from where the is called! The MODULE will do the work for you of determining the absolute path. This way you can can cache items from everywhere and don' t need to worry of pathes.
- Make sure that PHP can write into the cache folder!
- Make sure that you are NOT creating objects of this class!
- Cache some stuff
- Do NOT re-init the class.
HowTo
There are 5 methods available for you to interact with your cache.
Everywhere you find something like this , it' s an optional value. If you don' t pass any information for this variable it will set it to the default value. The default values are the most commonly used.
PUT
This method will store a value into the cache for a undefined time.
- : string: the unique key for this cache item. Used for retrieval.
- : mixed: The value you want to cache. Objects, arrays, closures are possible.
- : bool: will serialize the value (not when the value is callable!
- : bool: will override any existing cache items.
Example usage:
$variable = file_get_contents('http://google.com');
Cache::put('key', $variable);
This will store the result of the into the cache. But you can even store closures (functions) into the cache like this:
Cache::put('get_google', function() {
$localvar = file_get_contents('http://google.com');
//complicated alogrithem
return $result;
});
The will get stored into the cache.
GET
This method will retrieve a value from the cache, but only if it is NOT older then seconds
- : string: the unique key for this cache item. Used in the method.
- : int: The time in seconds the file could be old.
- : mixed: The value will get returned if no cache item exists or the cache item is too old.
Example usage:
$key = 'get_google';
$expire = 60 * 60 * 24; // 1 day
Cache::get('key', $expire);
This will return the result the closure (used in the previous example) as long as the was not before 1 day.
FORGET
This will remove the cache item for the given key.
Cache::forget('get_google');
This will remove the file from the disk, forever (a very long time).
REMEMBER
This is the most used method from this class and combines and .
The syntax looks like that:
As you can see it is using the same parameters as and .
An example:
Cache::remember('get_google', function() {
return file_get_contents('http://google.com');
}, 60 * 60 * 24); //1 day
This will save the the contents of into the cache and refreshes the the result every 24 hours (if you are visting the site every 1 hour :D). This is in most cases the most useful method, because you do not have two write your own refresh code.
CLEAR
This function will clear all cached items older than seconds.
Cache::clear($expire);
Thats it! Simple and straight forward.
You can always have a look at the PHP doc for a brief explanation.