Commit d1b186b0 authored by tufengqi's avatar tufengqi

cache部分结构调整

parent 265551ba
<?php <?php
namespace fpf\cache; namespace fpf\cache;
use fpf\cache\TableConstant;
/** /**
* 简单的KEY VALUE形式的构造真实KEY的方法 * 简单的KEY VALUE形式的构造真实KEY的方法
*/ */
class DefaultBaseImpl class DefaultBaseImpl
{ {
public static function getTableKey($prefix_tag, $table_name, $key) public static function getSimpleTableKey($db_name, $table_name, $key)
{ {
return $prefix_tag . $table_name . TableConstant::CONNECTOR_TAG . $key; return $db_name . TableConstant::CONNECTOR_TAG . $table_name . TableConstant::CONNECTOR_TAG . $key;
}
public static function getHashTable($db_name, $table_name)
{
return $db_name . TableConstant::CONNECTOR_TAG . $table_name;
}
public static function checkTable($db_name, $table)
{
if (!isset(TableConstant::$table_list[$db_name . TableConstant::CONNECTOR_TAG . $table_name])) {
throw new Exception('hash table is not defined');
}
} }
} }
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
namespace fpf\cache; namespace fpf\cache;
interface BaseIntf interface HashTableIntf
{ {
public function getTableKey($prefix_tag, $table, $key); public function getHashTable($db_name, $table_name);
public function checkTable($table_name);
} }
\ No newline at end of file
<?php
namespace fpf\cache;
interface SimpleTableIntf
{
public function getHashTableKey($db_name, $table_name, $key);
public function checkTable($table_name);
}
\ No newline at end of file
...@@ -7,15 +7,22 @@ namespace fpf\cache; ...@@ -7,15 +7,22 @@ namespace fpf\cache;
class TableConstant class TableConstant
{ {
const CONNECTOR_TAG = '_'; const CONNECTOR_TAG = '_';
const HASH_BTC = 'BTC'; /**
const HASH_USDT = 'USDT'; * table list
const SIMPLE_USD_CNY_RATE = 'USD_CNY_RATE'; */
const MARKET_SERVICE_TABLE_PREFIX = 'market-service:'; const TABLE_HASH_RISE_FALL_BTC = 'RISE_FALL_BTC';
public static $hash_table = [ const TABLE_HASH_RISE_FALL_USDT = 'RISE_FALL_USDT';
TableConstant::HASH_BTC => true, const TABLE_SIMPLE_USD_CNY_RATE = 'USD_CNY_RATE';
TableConstant::HASH_USDT => true, /**
]; * db name list
public static $simple_table = [ */
TableConstant::SIMPLE_USD_CNY_RATE => true, const DB_MARKET_SERVICE = 'market-service';
/**
* table check
*/
public static $table_list = [
self::DB_MARKET_SERVICE . self::CONNECTOR_TAG . self::TABLE_HASH_RISE_FALL_BTC => true,
self::DB_MARKET_SERVICE . self::CONNECTOR_TAG . self::TABLE_HASH_RISE_FALL_USDT => true,
self::DB_MARKET_SERVICE . self::CONNECTOR_TAG . self::TABLE_SIMPLE_USD_CNY_RATE => true,
]; ];
} }
<?php
namespace fpf\cache;
use fpf\cache\DefaultBaseImpl;
trait YiiTraitHashTable
{
public static function getHashTable($db_name, $table_name)
{
return DefaultBaseImpl::getHashTable($db_name, $table_name);
}
public static function getByTable($table)
{
self::checkTable(self::DB_NAME, $table);
$real_table = self::getHashTable(self::DB_NAME, $table);
$ret = Yii::$app->redis->hkeys($real_table);
if (empty($ret)) {
return [];
}
return self::getByTableKeys($table, $ret);
}
public static function getKeysByTable($table)
{
self::checkTable(self::DB_NAME, $table);
$table = self::getHashTable(self::DB_NAME, $table);
return Yii::$app->redis->hkeys($table);
}
public static function getByTableKeys($table, $keys)
{
self::checkTable(self::DB_NAME, $table);
$table = self::getHashTable(self::DB_NAME, $table);
return Yii::$app->redis->hmget($table, ...$keys);
}
public static function getByTableKey($table, $key)
{
self::checkTable(self::DB_NAME, $table);
$table = self::getHashTable(self::DB_NAME, $table);
return Yii::$app->redis->hget($table, $key);
}
public static function setByTableKey($table, $key, $value)
{
self::checkTable(self::DB_NAME, $table);
$table = self::getHashTable(self::DB_NAME, $table);
return Yii::$app->redis->hset($table, $key, $value);
}
private static function checkTable($db_name, $table)
{
DefaultBaseImpl::checkTable($db_name, $table);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment