Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
48199cfc
Commit
48199cfc
authored
Nov 30, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
renamed redis tableName() to keyPrefix()
parent
d0fc987f
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
20 deletions
+33
-20
ActiveQuery.php
extensions/redis/ActiveQuery.php
+2
-2
ActiveRecord.php
extensions/redis/ActiveRecord.php
+23
-10
LuaScriptBuilder.php
extensions/redis/LuaScriptBuilder.php
+8
-8
No files found.
extensions/redis/ActiveQuery.php
View file @
48199cfc
...
@@ -132,7 +132,7 @@ class ActiveQuery extends \yii\base\Component implements ActiveQueryInterface
...
@@ -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
...
@@ -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
;
...
...
extensions/redis/ActiveRecord.php
View file @
48199cfc
...
@@ -10,6 +10,7 @@ namespace yii\redis;
...
@@ -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
...
@@ -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
...
@@ -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
...
@@ -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
...
@@ -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
...
@@ -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
...
@@ -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'
);
...
...
extensions/redis/LuaScriptBuilder.php
View file @
48199cfc
...
@@ -29,7 +29,7 @@ class LuaScriptBuilder extends \yii\base\Object
...
@@ -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
...
@@ -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
...
@@ -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
...
@@ -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
...
@@ -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
...
@@ -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
...
@@ -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
...
@@ -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
"
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment