| 1 |
<?php |
| 2 |
/** |
| 3 |
* Cache Helper class |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\Services; |
| 9 |
|
| 10 |
use WCPOS\Vendor\Phpfastcache\Drivers\Files\Config; |
| 11 |
use WCPOS\Vendor\Phpfastcache\Drivers\Files\Driver; |
| 12 |
use WCPOS\Vendor\Phpfastcache\EventManager; |
| 13 |
use WCPOS\Vendor\Phpfastcache\Helper\Psr16Adapter; |
| 14 |
|
| 15 |
/** |
| 16 |
* Cache class. |
| 17 |
*/ |
| 18 |
class Cache { |
| 19 |
/** |
| 20 |
* Get the cache instance |
| 21 |
* |
| 22 |
* @param string $instance_id Instance ID. |
| 23 |
* |
| 24 |
* @return Psr16Adapter|object |
| 25 |
*/ |
| 26 |
public static function get_cache_instance( string $instance_id = 'default' ) { |
| 27 |
static $cache = null; |
| 28 |
|
| 29 |
if ( null === $cache ) { |
| 30 |
$upload_dir = wp_upload_dir(); |
| 31 |
$cache_dir = $upload_dir['basedir'] . '/woocommerce_pos_cache'; |
| 32 |
|
| 33 |
if ( ! file_exists( $cache_dir ) ) { |
| 34 |
wp_mkdir_p( $cache_dir ); |
| 35 |
} |
| 36 |
|
| 37 |
// @phpstan-ignore-next-line. |
| 38 |
$config = new Config( |
| 39 |
array( |
| 40 |
'path' => $cache_dir, |
| 41 |
) |
| 42 |
); |
| 43 |
|
| 44 |
$event_manager = EventManager::getInstance(); |
| 45 |
|
| 46 |
// Generate a unique instance ID for each logged-in user. |
| 47 |
// @phpstan-ignore-next-line. |
| 48 |
$driver = new Driver( $config, $instance_id ); |
| 49 |
$driver->setEventManager( $event_manager ); |
| 50 |
|
| 51 |
// @phpstan-ignore-next-line. |
| 52 |
$cache = new Psr16Adapter( $driver ); |
| 53 |
} |
| 54 |
|
| 55 |
return $cache; |
| 56 |
} |
| 57 |
} |
| 58 |
|