Last active
May 23, 2024 22:55
-
-
Save brunoconstantino/32a7ec5ae89530f9c55da637984b2d6e to your computer and use it in GitHub Desktop.
Usando RedisCache no cache de objetos do Adianti Framework. Localização arquivo: app/lib/registry/RedisCache.php Link artigo: https://www.adianti.com.br/forum/pt/view_7449?cache-de-objetos-com-redis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//namespace Adianti\Registry; | |
use Adianti\Registry\AdiantiRegistryInterface; | |
//use Redis; | |
/** | |
* Adianti Redis Record Cache | |
* | |
* @version 7.4 | |
* @package registry | |
* @author Bruno Constantino | |
* @copyright Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br) | |
* @license http://www.adianti.com.br/framework-license | |
*/ | |
class RedisCache implements AdiantiRegistryInterface | |
{ | |
private static $instance; // singleton instance | |
private $redis; | |
private $config_path; | |
private $conn_cache; | |
/** | |
* Returns the singleton instance | |
* @return Instance of self | |
*/ | |
public static function getInstance() | |
{ | |
// if there's no instance | |
if (empty(self::$instance)) | |
{ | |
// creates a new object | |
self::$instance = new self; | |
} | |
// returns the created instance | |
return self::$instance; | |
} | |
/** | |
* Destruct class | |
* | |
*/ | |
public function __destruct() | |
{ | |
//var_dump("Destroying " . __CLASS__ . "\n"); | |
$instance = self::getInstance(); | |
if(isset($instance->redis)) | |
{ | |
return $instance->redis->close(); | |
} | |
} | |
/** | |
* Change database configuration Path | |
* | |
* @param $path Config path | |
*/ | |
public static function setConfigPath($path) | |
{ | |
$instance = self::getInstance(); | |
$instance->config_path = $path; | |
} | |
/** | |
* Returns the database information as an array | |
*/ | |
public static function getDatabaseInfo() | |
{ | |
$database = 'redis'; | |
$instance = self::getInstance(); | |
$path = empty($instance->config_path) ? 'app/config' : $instance->config_path; | |
$path = 'app/config'; | |
$filei = "{$path}/{$database}.ini"; | |
$filep = "{$path}/{$database}.php"; | |
if (!empty($instance->conn_cache)) | |
{ | |
return $instance->conn_cache; | |
} | |
// check if the database configuration file exists | |
if (file_exists($filei)) | |
{ | |
// read the INI and retuns an array | |
$ini = parse_ini_file($filei); | |
$instance->conn_cache = $ini; | |
return $ini; | |
} | |
else if (file_exists($filep)) | |
{ | |
$ini = require $filep; | |
$instance->conn_cache = $ini; | |
return $ini; | |
} | |
else | |
{ | |
return FALSE; | |
} | |
} | |
/** | |
* Set database info | |
* @param $info Database connection information | |
*/ | |
public static function setDatabaseInfo($info) | |
{ | |
$instance = self::getInstance(); | |
$instance->conn_cache = $info; | |
} | |
///////////////////////////// | |
// AdiantiRegistryInterface | |
///////////////////////////// | |
/** | |
* Returns if the service is active | |
*/ | |
public static function enabled() | |
{ | |
$extension_loaded = extension_loaded('redis'); | |
if(!$extension_loaded) | |
{ | |
return FALSE; | |
} | |
$instance = self::getInstance(); | |
$ini = $instance->getDatabaseInfo(); | |
return $instance->connect(); | |
} | |
/** | |
* Returns if the service is active | |
*/ | |
public static function connect() | |
{ | |
$instance = self::getInstance(); | |
if(isset($instance->redis)) | |
{ | |
return TRUE; | |
} | |
$ini = $instance->getDatabaseInfo(); | |
// Se não tiver conexão definida, define uma conexão local. | |
$host = $ini['host'] ?? "127.0.0.1"; | |
$port = $ini['port'] ?? "6379"; | |
$connectTimeout = $ini['connectTimeout'] ?? "2.5"; | |
$instance->redis = new Redis(); | |
$connect = $instance->redis->connect( $host, $port, $connectTimeout ); | |
if($connect) | |
{ | |
if(!empty($ini['password'])) | |
{ | |
if(!$redis->auth($ini['password'])) | |
{ | |
return FALSE; | |
} | |
} | |
return $connect; | |
} | |
return FALSE; | |
} | |
/** | |
* Store a variable in cache | |
* @param $key Key | |
* @param $value Value | |
*/ | |
public static function setValue($key, $value) | |
{ | |
$instance = self::getInstance(); | |
if(!$instance->connect()) | |
{ | |
return false; | |
} | |
$ini = $instance->getDatabaseInfo(); | |
if(isset($ini['lifetimeKey']) && isset($ini['lifetimeKey']) > 0) | |
{ | |
/* Setando um tempo de duração do registro em 3600 segundos */ | |
return $instance->redis->setex(APPLICATION_NAME . '_' . $key, $ini['lifetimeKey'], serialize($value)); | |
} | |
else | |
{ | |
return $instance->redis->set(APPLICATION_NAME . '_' . $key, serialize($value)); | |
} | |
} | |
/** | |
* Get a variable from cache | |
* @param $key Key | |
*/ | |
public static function getValue($key) | |
{ | |
$instance = self::getInstance(); | |
if(!$instance->connect()) | |
{ | |
return false; | |
} | |
return unserialize($instance->redis->get(APPLICATION_NAME . '_' . $key)); | |
} | |
/** | |
* Delete a variable from cache | |
* @param $key Key | |
*/ | |
public static function delValue($key) | |
{ | |
$instance = self::getInstance(); | |
if(!$instance->connect()) | |
{ | |
return false; | |
} | |
return $instance->redis->del(APPLICATION_NAME . '_' . $key); | |
} | |
/** | |
* Clear cache | |
*/ | |
public static function clear() | |
{ | |
$instance = self::getInstance(); | |
if(!$instance->connect()) | |
{ | |
return false; | |
} | |
// Opção 1: | |
// $prefix = APPLICATION_NAME . '_'; | |
// $keys = $instance->redis->keys($prefix.'*'); | |
// return $instance->redis->del($keys); | |
// Opção 2: | |
return $instance->redis->flushDb(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment