101 lines
2.8 KiB
PHP
101 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace Hura8\System;
|
|
|
|
|
|
use Symfony\Component\Cache\Adapter\RedisAdapter;
|
|
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
|
|
|
|
|
|
class Cache
|
|
{
|
|
|
|
private static $redisCache = null;
|
|
private static $fileCache = null;
|
|
|
|
protected function __construct() { }
|
|
|
|
|
|
public static function getFileInstance(): FilesystemAdapter {
|
|
|
|
if(static::$fileCache instanceof FilesystemAdapter) {
|
|
return static::$fileCache;
|
|
}
|
|
|
|
static::$fileCache = new FilesystemAdapter(
|
|
|
|
// a string used as the subdirectory of the root cache directory, where cache
|
|
// items will be stored
|
|
$namespace = '',
|
|
|
|
// the default lifetime (in seconds) for cache items that do not define their
|
|
// own lifetime, with a value 0 causing items to be stored indefinitely (i.e.
|
|
// until the files are deleted)
|
|
$defaultLifetime = 30,
|
|
|
|
// the main cache directory (the application needs read-write permissions on it)
|
|
// if none is specified, a directory is created inside the system temporary directory
|
|
$directory = CACHE_FILE_SYSTEM_DIRECTORY
|
|
);
|
|
|
|
return static::$fileCache;
|
|
}
|
|
|
|
|
|
/**
|
|
* @return RedisAdapter | false
|
|
*/
|
|
public static function getRedisInstance() {
|
|
|
|
if(!is_null(static::$redisCache)) {
|
|
return static::$redisCache;
|
|
}
|
|
|
|
try {
|
|
// pass a single DSN string to register a single server with the client
|
|
$redisConnection = RedisAdapter::createConnection( CACHE_REDIS_DSN , [
|
|
//'read_timeout' => 10,
|
|
//'retry_interval' => 2,
|
|
/*
|
|
'persistent' => 1,
|
|
'persistent_id' => null,
|
|
'timeout' => 10,
|
|
|
|
'tcp_keepalive' => 0,
|
|
'lazy' => null,
|
|
'redis_cluster' => false,
|
|
'redis_sentinel' => null,*/
|
|
] );
|
|
|
|
//var_dump($redisConnection->isConnected());
|
|
|
|
}catch (\Exception $exception) {
|
|
//echo $exception->getMessage();
|
|
$redisConnection = false;
|
|
}
|
|
|
|
if(!$redisConnection) {
|
|
static::$redisCache = false;
|
|
return false;
|
|
}
|
|
|
|
static::$redisCache = new RedisAdapter(
|
|
// the object that stores a valid connection to your Redis system
|
|
$redisConnection,
|
|
|
|
// the string prefixed to the keys of the items stored in this cache
|
|
$namespace = '',
|
|
|
|
// the default lifetime (in seconds) for cache items that do not define their
|
|
// own lifetime, with a value 0 causing items to be stored indefinitely (i.e.
|
|
// until RedisAdapter::clear() is invoked or the server(s) are purged)
|
|
$defaultLifetime = 10
|
|
);
|
|
|
|
return static::$redisCache;
|
|
|
|
}
|
|
|
|
|
|
}
|