Commit d1b186b0 authored by tufengqi's avatar tufengqi

cache部分结构调整

parent 265551ba
<?php
namespace fpf\cache;
use fpf\cache\TableConstant;
/**
* 简单的KEY VALUE形式的构造真实KEY的方法
*/
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 @@
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;
class TableConstant
{
const CONNECTOR_TAG = '_';
const HASH_BTC = 'BTC';
const HASH_USDT = 'USDT';
const SIMPLE_USD_CNY_RATE = 'USD_CNY_RATE';
const MARKET_SERVICE_TABLE_PREFIX = 'market-service:';
public static $hash_table = [
TableConstant::HASH_BTC => true,
TableConstant::HASH_USDT => true,
];
public static $simple_table = [
TableConstant::SIMPLE_USD_CNY_RATE => true,
/**
* table list
*/
const TABLE_HASH_RISE_FALL_BTC = 'RISE_FALL_BTC';
const TABLE_HASH_RISE_FALL_USDT = 'RISE_FALL_USDT';
const TABLE_SIMPLE_USD_CNY_RATE = 'USD_CNY_RATE';
/**
* db name list
*/
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