CacheFileTrait
Build cache data for model.
To use cache data, call to loadCacheData([$this, 'yourBuildCacheContent'])
.
Example:
// To use cache data with your build cache content. class MyModel extends \Rdb\System\Core\Models\BaseModel { public function __construct(\Rdb\System\Container $Container) { $this->storageFile = 'mymodel-db.php'; $this->beginCacheFileTrait($Container); } public function myBuildCacheContent() { $sql = 'SELECT * FROM `' . $this->Db->tableName('mytable') . '`'; $Pdo = $this->Db->PDO(); $Sth = $Pdo->prepare($sql); $Sth->execute(); $result = $Sth->fetchAll(); $Sth->closeCursor(); $output = 'config_name . '\' => \'' . $row->config_value . '\',';' } $output .= '];' . PHP_EOL; return $output; } public function getData($name, $default = null) { $this->loadCacheData([$this, 'myBuildCacheContent']); print_r($this->storageData); } } // To use auto generated build cache content. class MyModel extends \Rdb\Modules\RdbAdmin\Models\BaseModel { public function __construct(\Rdb\System\Container $Container) { $this->storageFile = 'mymodel-db.php'; $this->beginCacheFileTrait($Container); } public function myBuildCacheContent() { $sql = 'SELECT * FROM `' . $this->Db->tableName('mytable') . '`'; $Pdo = $this->Db->PDO(); $Sth = $Pdo->prepare($sql); $Sth->execute(); $result = $Sth->fetchAll(); $Sth->closeCursor(); return $this->buildCacheContentFromResult($result); } public function getData($name, $default = null) { $this->loadCacheData([$this, 'myBuildCacheContent']); print_r($this->storageData); } }
Tags
Table of Contents
Properties
- $FileSystem : FileSystem
- $storageData : mixed
- $storageFile : string
- $storagePath : string
Methods
- resetCacheProperties() : mixed
- Reset cache properties.
- resetGetData() : mixed
- Reset some properties that was used while get the data using `loadCacheData()`.
- beginCacheFileTrait() : mixed
- Trait initialize method.
- buildCacheContentFromResult() : string
- Buld cache content from result that got from `PDOStatement::fetchAll()`.
- deleteCachedFile() : mixed
- Delete the file that were built and cached.
- loadCacheData() : mixed
- Load the cache file data into class property. (`storageData`).
- buildCacheFile() : bool
- Build the cache file.
- getCacheFilePath() : string
- Get full path to cache file.
- isNeedRebuildCache() : bool
- Check if it is needed to rebuild the cache file.
Properties
$FileSystem
protected
FileSystem
$FileSystem
$storageData
protected
mixed
$storageData
The data that was generated from DB. This property is depend on how you set $content
argument in buildCacheFile()
method.
$storageFile
protected
string
$storageFile
File name with extension for cache. Example: 'tablename-db.php'
.
$storagePath
protected
string
$storagePath
Full path to storage folder and specific name without trailing slash.
Example STORAGE_PATH . '/cache/tablename'
for store data from selected table in DB.
Only set this if you want to use cache.
Methods
resetCacheProperties()
Reset cache properties.
public
resetCacheProperties() : mixed
This is useful when you call to any methods that have called to loadCacheData()
method on this trait.
This method will be reset storageData
, storageFile
, storagePath
properties.
Tags
resetGetData()
Reset some properties that was used while get the data using `loadCacheData()`.
public
resetGetData() : mixed
This is useful when you call to extended class that is get a cache data row using loadCacheData()
and you want to call it again or it is in the loop.
This method will be reset storageData
, storageFile
properties.
Tags
beginCacheFileTrait()
Trait initialize method.
protected
beginCacheFileTrait(Container $Container) : mixed
This method must be called before it can be working.
Parameters
- $Container : Container
-
The DI container class.
buildCacheContentFromResult()
Buld cache content from result that got from `PDOStatement::fetchAll()`.
protected
buildCacheContentFromResult(mixed $result[, string $formatMethod = 'raw' ]) : string
Parameters
- $result : mixed
-
The result from
PDOStatement::fetchAll()
. - $formatMethod : string = 'raw'
-
The format option. Value can be 'array', 'json_array', 'replace_setstate', 'serialize', 'raw'. Default is 'raw'.
Return values
string —Return generated content ready for cache.
deleteCachedFile()
Delete the file that were built and cached.
protected
deleteCachedFile() : mixed
This method was called from buildCacheFile()
.
Tags
loadCacheData()
Load the cache file data into class property. (`storageData`).
protected
loadCacheData(callable $BuildContent) : mixed
Also build it if it is not exists or too old (expired).
You can access cache data via storageData
property.
Parameters
- $BuildContent : callable
-
The callback array to build content if needed. Example:
[$this, 'buildCacheContent']
. The callback must return content string of generated php data for write to cache.
buildCacheFile()
Build the cache file.
private
buildCacheFile(string $content) : bool
This method was called from getCacheFilePath()
.
Parameters
- $content : string
-
The file content to build cache. This content should be array or object from db in php data that is ready to access.
Return values
bool —Return true on success build cache file.
getCacheFilePath()
Get full path to cache file.
private
getCacheFilePath(callable $BuildContent) : string
This method was called from loadCacheData()
.
Parameters
- $BuildContent : callable
-
The callback array to build content if needed.
Return values
string —Return full path to cache file. Return empty string if cache file was not found and unable to build.
isNeedRebuildCache()
Check if it is needed to rebuild the cache file.
private
isNeedRebuildCache() : bool
This method was called from getCacheFilePath()
, MetaFieldsTrait->listObjectsFields()
.
Return values
bool —Return true
if it is need to build, rebuild the cache file, return false
if it is not.