Cache
For computations that involve heavy processing, or to avoid repeatedly querying the database (to retrieve data whose information rarely changes), it is sometimes necessary to store the result of a computation or the retrieved data. For this purpose, you can use the packages\base\cache class.
For each item that is stored, you can specify an expiration time, and after the specified time elapses, the data is automatically deleted and is no longer accessible. Data storage in this class is performed in three ways:
- Storing data in a file
- Storing data in a database
- Storing data using Memcache
Specifying the Storage Method
For more information, refer to the Options page.
By adding an option named packages.base.cache with one of the values file (to store data in a file), memcache (to store data using memcache), or database (to store data in the database), you can specify the storage method.
If no storage method is specified, the file storage method is used automatically.
Using all three methods is identical, and the cache class automatically stores and manages the data using the specified method.
The methods of this class can be called both statically (static) and in an object-oriented manner through instantiation.
Storing Data
cache::set(name, value, timeout);
This method takes an arbitrary name/key as its first parameter and the data as its second parameter. In the third parameter, you can specify the expiration time of this storage in seconds. Note that the name you specify in the first parameter of this method must be unique across the entire program; otherwise, the new data will replace the previous data.
Checking Whether Data Exists
Using the has method of the cache class, you can confirm whether data exists. This method takes the key name as its first parameter. The output of this method is true if data with the specified key is found, and false otherwise.
cache::has(name);
Retrieving Data
To retrieve data, you must use the get method of the cache class. This method searches for the key name it receives as its parameter among the stored data. If it finds data with the specified key, it returns the value of that key; otherwise, it returns null.
cache::get(name);
Deleting Data
Although data is deleted after the specified expiration time elapses, you can use the delete method to delete the data of a specific key. This method searches for the key it receives as its parameter among the stored data and, if found, deletes it.
cache::delete(name);
Example
namespace packages\packagename\controllers;
use packages\base\{controller, cache};
use packages\packagename\plan;
class Main extends controller {
public function getEconomicPlans() {
$response = new response(true);
$cache = new cache();
if ($cache->has("packages.packagename.controllers.economicPlans")) {
$plans = $cache->get("packages.packagename.controllers.economicPlans");
} else {
$plan = new plan();
$plan->where("status", plan::active);
$plan->where("economic", plan::economic);
$plans = $plan->get();
$cache->set("packages.packagename.controllers.economicPlans", $plans, 86400);
// 86400 s = 1 day
}
$response->setData($plans, "plans");
return $response;
}
}
Example
namespace packages\packagename\controllers;
use packages\base\{controller, cache};
class Main extends controller {
public function update_economic_plans() {
$response = new response(true);
cache::delete("packages.packagename.controllers.economicPlans");
$response->Go(base\url("plans/economic"));
return $response;
}
}