Browse Source

renamed redis tableName() to keyPrefix()

tags/2.0.0-beta
Carsten Brandt 11 years ago
parent
commit
48199cfc00
  1. 4
      extensions/redis/ActiveQuery.php
  2. 33
      extensions/redis/ActiveRecord.php
  3. 16
      extensions/redis/LuaScriptBuilder.php

4
extensions/redis/ActiveQuery.php

@ -132,7 +132,7 @@ class ActiveQuery extends \yii\base\Component implements ActiveQueryInterface
if ($db === null) { if ($db === null) {
$db = $modelClass::getDb(); $db = $modelClass::getDb();
} }
return $db->executeCommand('LLEN', [$modelClass::tableName()]); return $db->executeCommand('LLEN', [$modelClass::keyPrefix()]);
} else { } else {
return $this->executeScript($db, 'Count'); return $this->executeScript($db, 'Count');
} }
@ -296,7 +296,7 @@ class ActiveQuery extends \yii\base\Component implements ActiveQueryInterface
$data = []; $data = [];
foreach($pks as $pk) { foreach($pks as $pk) {
if (++$i > $start && ($this->limit === null || $i <= $start + $this->limit)) { if (++$i > $start && ($this->limit === null || $i <= $start + $this->limit)) {
$key = $modelClass::tableName() . ':a:' . $modelClass::buildKey($pk); $key = $modelClass::keyPrefix() . ':a:' . $modelClass::buildKey($pk);
$result = $db->executeCommand('HGETALL', [$key]); $result = $db->executeCommand('HGETALL', [$key]);
if (!empty($result)) { if (!empty($result)) {
$data[] = $result; $data[] = $result;

33
extensions/redis/ActiveRecord.php

@ -10,6 +10,7 @@ namespace yii\redis;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\base\NotSupportedException; use yii\base\NotSupportedException;
use yii\db\BaseActiveRecord; use yii\db\BaseActiveRecord;
use yii\helpers\Inflector;
use yii\helpers\StringHelper; use yii\helpers\StringHelper;
/** /**
@ -88,6 +89,18 @@ class ActiveRecord extends BaseActiveRecord
} }
/** /**
* Declares prefix of the key that represents the keys that store this records in redis.
* By default this method returns the class name as the table name by calling [[Inflector::camel2id()]].
* For example, 'Customer' becomes 'customer', and 'OrderItem' becomes
* 'order_item'. You may override this method if you want different key naming.
* @return string the prefix to apply to all AR keys
*/
public static function keyPrefix()
{
return Inflector::camel2id(StringHelper::basename(get_called_class()), '_');
}
/**
* @inheritdoc * @inheritdoc
*/ */
public function insert($runValidation = true, $attributes = null) public function insert($runValidation = true, $attributes = null)
@ -103,15 +116,15 @@ class ActiveRecord extends BaseActiveRecord
foreach ($this->primaryKey() as $key) { foreach ($this->primaryKey() as $key) {
$pk[$key] = $values[$key] = $this->getAttribute($key); $pk[$key] = $values[$key] = $this->getAttribute($key);
if ($pk[$key] === null) { if ($pk[$key] === null) {
$pk[$key] = $values[$key] = $db->executeCommand('INCR', [static::tableName() . ':s:' . $key]); $pk[$key] = $values[$key] = $db->executeCommand('INCR', [static::keyPrefix() . ':s:' . $key]);
$this->setAttribute($key, $values[$key]); $this->setAttribute($key, $values[$key]);
} }
} }
// } // }
// save pk in a findall pool // save pk in a findall pool
$db->executeCommand('RPUSH', [static::tableName(), static::buildKey($pk)]); $db->executeCommand('RPUSH', [static::keyPrefix(), static::buildKey($pk)]);
$key = static::tableName() . ':a:' . static::buildKey($pk); $key = static::keyPrefix() . ':a:' . static::buildKey($pk);
// save attributes // save attributes
$args = [$key]; $args = [$key];
foreach($values as $attribute => $value) { foreach($values as $attribute => $value) {
@ -151,7 +164,7 @@ class ActiveRecord extends BaseActiveRecord
foreach(static::fetchPks($condition) as $pk) { foreach(static::fetchPks($condition) as $pk) {
$newPk = $pk; $newPk = $pk;
$pk = static::buildKey($pk); $pk = static::buildKey($pk);
$key = static::tableName() . ':a:' . $pk; $key = static::keyPrefix() . ':a:' . $pk;
// save attributes // save attributes
$args = [$key]; $args = [$key];
foreach($attributes as $attribute => $value) { foreach($attributes as $attribute => $value) {
@ -162,13 +175,13 @@ class ActiveRecord extends BaseActiveRecord
$args[] = $value; $args[] = $value;
} }
$newPk = static::buildKey($newPk); $newPk = static::buildKey($newPk);
$newKey = static::tableName() . ':a:' . $newPk; $newKey = static::keyPrefix() . ':a:' . $newPk;
// rename index if pk changed // rename index if pk changed
if ($newPk != $pk) { if ($newPk != $pk) {
$db->executeCommand('MULTI'); $db->executeCommand('MULTI');
$db->executeCommand('HMSET', $args); $db->executeCommand('HMSET', $args);
$db->executeCommand('LINSERT', [static::tableName(), 'AFTER', $pk, $newPk]); $db->executeCommand('LINSERT', [static::keyPrefix(), 'AFTER', $pk, $newPk]);
$db->executeCommand('LREM', [static::tableName(), 0, $pk]); $db->executeCommand('LREM', [static::keyPrefix(), 0, $pk]);
$db->executeCommand('RENAME', [$key, $newKey]); $db->executeCommand('RENAME', [$key, $newKey]);
$db->executeCommand('EXEC'); $db->executeCommand('EXEC');
} else { } else {
@ -202,7 +215,7 @@ class ActiveRecord extends BaseActiveRecord
$db = static::getDb(); $db = static::getDb();
$n=0; $n=0;
foreach(static::fetchPks($condition) as $pk) { foreach(static::fetchPks($condition) as $pk) {
$key = static::tableName() . ':a:' . static::buildKey($pk); $key = static::keyPrefix() . ':a:' . static::buildKey($pk);
foreach($counters as $attribute => $value) { foreach($counters as $attribute => $value) {
$db->executeCommand('HINCRBY', [$key, $attribute, $value]); $db->executeCommand('HINCRBY', [$key, $attribute, $value]);
} }
@ -234,8 +247,8 @@ class ActiveRecord extends BaseActiveRecord
$db->executeCommand('MULTI'); $db->executeCommand('MULTI');
foreach($pks as $pk) { foreach($pks as $pk) {
$pk = static::buildKey($pk); $pk = static::buildKey($pk);
$db->executeCommand('LREM', [static::tableName(), 0, $pk]); $db->executeCommand('LREM', [static::keyPrefix(), 0, $pk]);
$attributeKeys[] = static::tableName() . ':a:' . $pk; $attributeKeys[] = static::keyPrefix() . ':a:' . $pk;
} }
if (empty($attributeKeys)) { if (empty($attributeKeys)) {
$db->executeCommand('EXEC'); $db->executeCommand('EXEC');

16
extensions/redis/LuaScriptBuilder.php

@ -29,7 +29,7 @@ class LuaScriptBuilder extends \yii\base\Object
// TODO add support for orderBy // TODO add support for orderBy
/** @var ActiveRecord $modelClass */ /** @var ActiveRecord $modelClass */
$modelClass = $query->modelClass; $modelClass = $query->modelClass;
$key = $this->quoteValue($modelClass::tableName() . ':a:'); $key = $this->quoteValue($modelClass::keyPrefix() . ':a:');
return $this->build($query, "n=n+1 pks[n]=redis.call('HGETALL',$key .. pk)", 'pks'); return $this->build($query, "n=n+1 pks[n]=redis.call('HGETALL',$key .. pk)", 'pks');
} }
@ -43,7 +43,7 @@ class LuaScriptBuilder extends \yii\base\Object
// TODO add support for orderBy // TODO add support for orderBy
/** @var ActiveRecord $modelClass */ /** @var ActiveRecord $modelClass */
$modelClass = $query->modelClass; $modelClass = $query->modelClass;
$key = $this->quoteValue($modelClass::tableName() . ':a:'); $key = $this->quoteValue($modelClass::keyPrefix() . ':a:');
return $this->build($query, "do return redis.call('HGETALL',$key .. pk) end", 'pks'); return $this->build($query, "do return redis.call('HGETALL',$key .. pk) end", 'pks');
} }
@ -58,7 +58,7 @@ class LuaScriptBuilder extends \yii\base\Object
// TODO add support for orderBy and indexBy // TODO add support for orderBy and indexBy
/** @var ActiveRecord $modelClass */ /** @var ActiveRecord $modelClass */
$modelClass = $query->modelClass; $modelClass = $query->modelClass;
$key = $this->quoteValue($modelClass::tableName() . ':a:'); $key = $this->quoteValue($modelClass::keyPrefix() . ':a:');
return $this->build($query, "n=n+1 pks[n]=redis.call('HGET',$key .. pk," . $this->quoteValue($column) . ")", 'pks'); return $this->build($query, "n=n+1 pks[n]=redis.call('HGET',$key .. pk," . $this->quoteValue($column) . ")", 'pks');
} }
@ -82,7 +82,7 @@ class LuaScriptBuilder extends \yii\base\Object
{ {
/** @var ActiveRecord $modelClass */ /** @var ActiveRecord $modelClass */
$modelClass = $query->modelClass; $modelClass = $query->modelClass;
$key = $this->quoteValue($modelClass::tableName() . ':a:'); $key = $this->quoteValue($modelClass::keyPrefix() . ':a:');
return $this->build($query, "n=n+redis.call('HGET',$key .. pk," . $this->quoteValue($column) . ")", 'n'); return $this->build($query, "n=n+redis.call('HGET',$key .. pk," . $this->quoteValue($column) . ")", 'n');
} }
@ -96,7 +96,7 @@ class LuaScriptBuilder extends \yii\base\Object
{ {
/** @var ActiveRecord $modelClass */ /** @var ActiveRecord $modelClass */
$modelClass = $query->modelClass; $modelClass = $query->modelClass;
$key = $this->quoteValue($modelClass::tableName() . ':a:'); $key = $this->quoteValue($modelClass::keyPrefix() . ':a:');
return $this->build($query, "n=n+1 if v==nil then v=0 end v=v+redis.call('HGET',$key .. pk," . $this->quoteValue($column) . ")", 'v/n'); return $this->build($query, "n=n+1 if v==nil then v=0 end v=v+redis.call('HGET',$key .. pk," . $this->quoteValue($column) . ")", 'v/n');
} }
@ -110,7 +110,7 @@ class LuaScriptBuilder extends \yii\base\Object
{ {
/** @var ActiveRecord $modelClass */ /** @var ActiveRecord $modelClass */
$modelClass = $query->modelClass; $modelClass = $query->modelClass;
$key = $this->quoteValue($modelClass::tableName() . ':a:'); $key = $this->quoteValue($modelClass::keyPrefix() . ':a:');
return $this->build($query, "n=redis.call('HGET',$key .. pk," . $this->quoteValue($column) . ") if v==nil or n<v then v=n end", 'v'); return $this->build($query, "n=redis.call('HGET',$key .. pk," . $this->quoteValue($column) . ") if v==nil or n<v then v=n end", 'v');
} }
@ -124,7 +124,7 @@ class LuaScriptBuilder extends \yii\base\Object
{ {
/** @var ActiveRecord $modelClass */ /** @var ActiveRecord $modelClass */
$modelClass = $query->modelClass; $modelClass = $query->modelClass;
$key = $this->quoteValue($modelClass::tableName() . ':a:'); $key = $this->quoteValue($modelClass::keyPrefix() . ':a:');
return $this->build($query, "n=redis.call('HGET',$key .. pk," . $this->quoteValue($column) . ") if v==nil or n>v then v=n end", 'v'); return $this->build($query, "n=redis.call('HGET',$key .. pk," . $this->quoteValue($column) . ") if v==nil or n>v then v=n end", 'v');
} }
@ -152,7 +152,7 @@ class LuaScriptBuilder extends \yii\base\Object
/** @var ActiveRecord $modelClass */ /** @var ActiveRecord $modelClass */
$modelClass = $query->modelClass; $modelClass = $query->modelClass;
$key = $this->quoteValue($modelClass::tableName()); $key = $this->quoteValue($modelClass::keyPrefix());
$loadColumnValues = ''; $loadColumnValues = '';
foreach($columns as $column => $alias) { foreach($columns as $column => $alias) {
$loadColumnValues .= "local $alias=redis.call('HGET',$key .. ':a:' .. pk, '$column')\n"; $loadColumnValues .= "local $alias=redis.call('HGET',$key .. ':a:' .. pk, '$column')\n";

Loading…
Cancel
Save