| 
<?php 
/**
 *   __ _  ___  ___ ___   ___   ___     ____ _ __ ___   ___
 *  / _` |/  / / __/ _ \ / _ \ /  /    / __/| '_ ` _ \ /  /
 * | (_| |\  \| (_| (_) | (_) |\  \   | (__ | | | | | |\  \
 *  \__,_|/__/ \___\___/ \___/ /__/    \___\|_| |_| |_|/__/
 *
 *
 ************************************************************************************
 * @ASCOOS-NAME            : ASCOOS CMS 25'                                            *
 * @ASCOOS-VERSION         : 25.0.0                                                    *
 * @ASCOOS-CATEGORY        : Framework (Frontend and Administrator Side)               *
 * @ASCOOS-CREATOR         : Drogidis Christos                                         *
 * @ASCOOS-SITE            : www.ascoos.com                                            *
 * @ASCOOS-LICENSE         : [Commercial] http://docs.ascoos.com/lics/ascoos/AGL.html  *
 * @ASCOOS-COPYRIGHT       : Copyright (c) 2007 - 2025, AlexSoft Software.             *
 ************************************************************************************
 *
 * @package                : ASCOOS FRAMEWORK Examples
 * @subpackage             : Handles APCu-based Cache.
 * @source                 : afw-examples/classes/TCacheHandler/TCacheAPCuHandler.php
 * @fileNo                 :
 * @version                : 25.0.0
 * @build               : 10829
 * @created                : 2024-07-01 20:00:00 UTC+2
 * @updated                : 2025-01-01 07:00:00 UTC+2
 * @author                 : Drogidis Christos
 * @authorSite             : www.alexsoft.gr
 * @license             : AGL-F
 *
 * @since PHP 8.2.0
 */
 declare(strict_types=1);
 
 require_once "../../autoload.php";
 
 use ASCOOS\FRAMEWORK\Kernel\{
 Core\TObject,
 Cache\Apcu\TCacheAPCuHandler
 };
 
 /******************************************************************************
 * @startcode TExampleAPCuObject
 *****************************************************************************/
 /**
 * @class   TExampleAPCuObject
 * @extends TObject
 *
 * @summary Example object using APCu for caching.
 *
 * [ PROPERTIES ]
 * @property $cacheHandler    Protected Property for APCu Cache Handler.
 *
 * [ METHODS ]
 * @method void __construct(array $properties = [])                         Initialize the class with given properties.
 * @method mixed fetchData(string $cacheKey)                                Fetch data from the cache if available, or retrieve from the source.
 * @method array getCacheStat()                                             Get statistics from APCu.
 * @method void clearCache(?string $cacheKey = null)                        Clears cache, optionally for a specific key.
 */
 class TExampleAPCuObject extends TObject
 {
 protected $cacheHandler;
 
 /**
 * Constructor.
 *
 * @desc <English>  Initialize the class with given properties.
 * @desc <Greek>    ??????????? ??? ????? ?? ??? ??????????? ?????????.
 *
 * @param array $properties   <English>  An associative array of properties to initialize the class with.
 *                            <Greek>    ???? ????????????? ??????? ????????? ??? ??? ???????????? ??? ??????.
 */
 public function __construct(array $properties = [])
 {
 parent::__construct($properties);
 $this->cacheHandler = new TCacheAPCuHandler(3600);
 }
 
 /**
 * Fetch data from cache or source.
 *
 * @desc <English>  Fetch data from the cache if available, or retrieve from the source.
 * @desc <Greek>    ?????? ???????? ??? ??? cache ??? ????? ????????? ? ??? ??? ????.
 *
 * @param string $cacheKey  <English> The key for the cache.
 *                          <Greek> ?? ?????? ??? ??? cache.
 * @return mixed    <English> The fetched data.
 *                  <Greek> ?? ?????????? ????????.
 */
 public function fetchData(string $cacheKey): mixed
 {
 $cachedData = $this->cacheHandler->checkCache($cacheKey);
 if ($cachedData) {
 return $cachedData;
 }
 
 // Simulate data retrieval from a data source (e.g., database)
 $data = ['data' => 'sample data'];
 
 // Save data to cache
 $this->cacheHandler->saveCache($cacheKey, $data);
 
 return $data;
 }
 
 /**
 * Get Cache Statistics.
 *
 * @desc <English>  Get statistics from APCu.
 * @desc <Greek>    ???? ??????????? ??? APCu.
 *
 * @return array <English> Array containing APCu statistics.
 *               <Greek>   ??????? ?? ?? ?????????? ??? APCu.
 */
 public function getCacheStat(): array
 {
 return $this->cacheHandler->getStats();
 }
 
 /**
 * Clear Cache.
 *
 * @desc <English>  Clear cache, optionally for a specific key.
 * @desc <Greek>    ?????????? cache, ??????????? ??? ???????????? ??????.
 *
 * @param ?string $cacheKey     <English> The key for the cache (optional).
 *                              <Greek> ?? ?????? ??? ??? cache (???????????).
 */
 public function clearCache(?string $cacheKey = null): void
 {
 $this->cacheHandler->clearCache($cacheKey);
 }
 }
 
 /*
 <English>   Example of use the TExampleAPCuObject class
 <Greek>     ?????????? ?????? ??? ?????? TExampleAPCuObject
 */
 $example = new TExampleAPCuObject([
 'config' => [
 'extensions' => [
 'subExtension1' => ['version' => '1.0.0'],
 'subExtension2' => ['enabled' => true]
 ],
 'newProperty' => 'newValue'
 ]
 ]);
 
 /*
 <English>   Get data using cache.
 <Greek>     ???????? ????????? ??????????????? ??? cache.
 */
 $data = $example->fetchData('example_key');
 print_r($data);
 print_r($example->getCacheStat());
 //$example->clearCache('example_key');
 
 $example->Free($example);
 
 |