Last active
November 2, 2024 14:22
-
-
Save khoipro/6cd368ccbd64b8aa9371f7f7ccc6d89e to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Use redis cache | |
* | |
* @package codetot-optimization | |
* @author codetot | |
* @since 0.0.1 | |
*/ | |
class Sample_Redis_Cache { | |
/** | |
* Singleton instance | |
* | |
* @var Sample_Redis_Cache | |
*/ | |
protected static $instance; | |
/** | |
* Get singleton instance. | |
* | |
* @return Sample_Redis_Cache | |
*/ | |
public final static function instance() | |
{ | |
if (is_null(self::$instance)) { | |
self::$instance = new self(); | |
} | |
return self::$instance; | |
} | |
public function __construct() | |
{ | |
$this->redis_status= false; | |
//init redis | |
if (defined('REDIS_CONFIG')) { | |
$config = REDIS_CONFIG; | |
$this->redis = new Redis(); | |
$this->redis_status = $this->redis->pconnect($config['host'], $config['port']); | |
$this->redis->select($config['database']); | |
} | |
} | |
function get_status() { | |
return $this->redis_status; | |
} | |
/** | |
* Get | |
* | |
* @param string $key | |
* | |
* @return array|string|null | |
*/ | |
public function get($key) { | |
if ($this->get_status()) { | |
return $this->redis->get($key); | |
} else { | |
return false; | |
} | |
} | |
/** | |
* Incr | |
* | |
* @param string $key | |
* @param array|string|null $value | |
* | |
* @return void | |
*/ | |
public function inc($key, $value = 1) { | |
if ($this->get_status()) { | |
$this->redis->incr($key, $value); | |
} | |
} | |
/** | |
* Decrement | |
* | |
* @param string $key | |
* @param array|string|null $value | |
* | |
* @return void | |
*/ | |
public function dec($key, $value = 1) { | |
if ($this->get_status()) { | |
$this->redis->decr($key, $value); | |
} | |
} | |
/** | |
* Set | |
* | |
* @param string $key | |
* @param array|string|null $value | |
* | |
* @return void | |
*/ | |
public function set($key, $value, $time = 0) { | |
if ($this->get_status()) { | |
$result = $this->redis->set($key, $value); | |
if ($time>0) { | |
$this->redis->expire($key, $time); | |
} | |
return $result; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* Delete | |
* | |
* @param string $key | |
* | |
* @return void | |
*/ | |
public function del($key) { | |
if ($this->get_status()) { | |
$this->redis->del($key); | |
} | |
} | |
/** | |
* Flush by key | |
* | |
* @return void | |
*/ | |
public function delete_by_key($key) { | |
if ($this->get_status()) { | |
$keys = $this->redis->keys($key.'*'); | |
foreach ($keys as $key) { | |
$this->redis->del($key); | |
} | |
} | |
} | |
/** | |
* Flush | |
* | |
* @return void | |
*/ | |
public function flush() { | |
$this->redis->flushAll(); | |
} | |
} | |
Sample_Redis_Cache::instance(); |
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 | |
define('REDIS_CONFIG', [ | |
'host' => 'redis', // or localhost | |
'port' => 6379, | |
'database' => 0, // change for each site | |
'maxttl' => 86400 * 7, | |
'timeout' => 1.0, | |
'read_timeout' => 1.0, | |
'split_alloptions' => true, | |
'debug' => false, | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment