1. Go to this page and download the library: Download taitava/php-cachedcall library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
taitava / php-cachedcall example snippets
class Page
{
public $template = "page_template.html";
public $title;
public $author;
public $content;
public $date;
public function __construct($data)
{
foreach ($data as $key => $value)
{
$this->$key = $value;
}
}
public static function getPageFromDB($id)
{
$data = ORM::table("Page")->getById($id); // ORM class is not defined in this example, but let's just pretend that it exists. :)
return new self($data);
}
public function RenderContent()
{
$template = file_get_contents($this->template);
$data = [
$this->title,
$this->author,
$this->content,
$this->date,
];
// ...
// Do some placeholder replacements to $template with $data here...
// ...
return $template
}
}
class Page
{
use Taitava\CachedCall\CachedCallTrait; // Apply the trait for this class.
public $template = "page_template.html";
public $title;
public $author;
public $content;
public $date;
public function __construct($data)
{
foreach ($data as $key => $value)
{
$this->$key = $value;
}
}
public static function getPageFromDB($id)
{
return static::cached_static_call(__METHOD__, func_get_args(), function ($id)
{
$data = ORM::table("Page")->getById($id); // ORM class is not defined in this example, but let's just pretend that it exists. :)
return new self($data);
});
}
public function renderContent()
{
return $this->cached_call(__METHOD__, func_get_args() /* Actually this is an empty array because this method has no parameters. */, function ()
{
$template = file_get_contents($this->template);
$data = [
$this->title,
$this->author,
$this->content,
$this->date,
];
// ...
// Do some placeholder replacements to $template with $data here...
// ...
return $template
});
}
}
class Page
{
use Taitava\CachedCall\CachedCallTrait; // Apply the trait for this class.
public $template = "page_template.html";
public $title;
public $author;
public $content;
public $date;
// ...
public function doSomething(array $an_array)
{
$stringified_array = implode(",", $an_array); // Create a custom cache key. This can be done IF we know that the array is likely to be *small*. Be careful!
$parameters = [$stringified_array];
return $this->cached_call(__METHOD__, $parameters, function () use ($an_array)
{
// ...
// Do something with $an_array...
// ...
return $result
});
}
}
class Page
{
use Taitava\CachedCall\CachedCallTrait;
public function method1($parameter)
{
$this->enable_cached_calls = false; // Disable cache
$result = $this->cached_call(__METHOD__, func_get_args(), function ()
{
// ...
// Do something...
// ...
return $result
});
$this->enable_cached_calls = true; // Re-enable cache
return $result;
}
public static function method2($parameter)
{
static::$enable_cached_static_calls = false; // Disable cache
$result = $this->cached_call(__METHOD__, func_get_args(), function ()
{
// ...
// Do something...
// ...
return $result
});
static::$enable_cached_static_calls = true; // Re-enable cache
}
}
sh
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.