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
Hide 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
if
(
$db
===
null
)
{
$db
=
$modelClass
::
getDb
();
}
return
$db
->
executeCommand
(
'LLEN'
,
[
$modelClass
::
tableName
()]);
return
$db
->
executeCommand
(
'LLEN'
,
[
$modelClass
::
keyPrefix
()]);
}
else
{
return
$this
->
executeScript
(
$db
,
'Count'
);
}
...
...
@@ -296,7 +296,7 @@ class ActiveQuery extends \yii\base\Component implements ActiveQueryInterface
$data
=
[];
foreach
(
$pks
as
$pk
)
{
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
]);
if
(
!
empty
(
$result
))
{
$data
[]
=
$result
;
...
...
extensions/redis/ActiveRecord.php
View file @
48199cfc
...
...
@@ -10,6 +10,7 @@ namespace yii\redis;
use
yii\base\InvalidConfigException
;
use
yii\base\NotSupportedException
;
use
yii\db\BaseActiveRecord
;
use
yii\helpers\Inflector
;
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
*/
public
function
insert
(
$runValidation
=
true
,
$attributes
=
null
)
...
...
@@ -103,15 +116,15 @@ class ActiveRecord extends BaseActiveRecord
foreach
(
$this
->
primaryKey
()
as
$key
)
{
$pk
[
$key
]
=
$values
[
$key
]
=
$this
->
getAttribute
(
$key
);
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
]);
}
}
// }
// 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
$args
=
[
$key
];
foreach
(
$values
as
$attribute
=>
$value
)
{
...
...
@@ -151,7 +164,7 @@ class ActiveRecord extends BaseActiveRecord
foreach
(
static
::
fetchPks
(
$condition
)
as
$pk
)
{
$newPk
=
$pk
;
$pk
=
static
::
buildKey
(
$pk
);
$key
=
static
::
tableName
()
.
':a:'
.
$pk
;
$key
=
static
::
keyPrefix
()
.
':a:'
.
$pk
;
// save attributes
$args
=
[
$key
];
foreach
(
$attributes
as
$attribute
=>
$value
)
{
...
...
@@ -162,13 +175,13 @@ class ActiveRecord extends BaseActiveRecord
$args
[]
=
$value
;
}
$newPk
=
static
::
buildKey
(
$newPk
);
$newKey
=
static
::
tableName
()
.
':a:'
.
$newPk
;
$newKey
=
static
::
keyPrefix
()
.
':a:'
.
$newPk
;
// rename index if pk changed
if
(
$newPk
!=
$pk
)
{
$db
->
executeCommand
(
'MULTI'
);
$db
->
executeCommand
(
'HMSET'
,
$args
);
$db
->
executeCommand
(
'LINSERT'
,
[
static
::
tableName
(),
'AFTER'
,
$pk
,
$newPk
]);
$db
->
executeCommand
(
'LREM'
,
[
static
::
tableName
(),
0
,
$pk
]);
$db
->
executeCommand
(
'LINSERT'
,
[
static
::
keyPrefix
(),
'AFTER'
,
$pk
,
$newPk
]);
$db
->
executeCommand
(
'LREM'
,
[
static
::
keyPrefix
(),
0
,
$pk
]);
$db
->
executeCommand
(
'RENAME'
,
[
$key
,
$newKey
]);
$db
->
executeCommand
(
'EXEC'
);
}
else
{
...
...
@@ -202,7 +215,7 @@ class ActiveRecord extends BaseActiveRecord
$db
=
static
::
getDb
();
$n
=
0
;
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
)
{
$db
->
executeCommand
(
'HINCRBY'
,
[
$key
,
$attribute
,
$value
]);
}
...
...
@@ -234,8 +247,8 @@ class ActiveRecord extends BaseActiveRecord
$db
->
executeCommand
(
'MULTI'
);
foreach
(
$pks
as
$pk
)
{
$pk
=
static
::
buildKey
(
$pk
);
$db
->
executeCommand
(
'LREM'
,
[
static
::
tableName
(),
0
,
$pk
]);
$attributeKeys
[]
=
static
::
tableName
()
.
':a:'
.
$pk
;
$db
->
executeCommand
(
'LREM'
,
[
static
::
keyPrefix
(),
0
,
$pk
]);
$attributeKeys
[]
=
static
::
keyPrefix
()
.
':a:'
.
$pk
;
}
if
(
empty
(
$attributeKeys
))
{
$db
->
executeCommand
(
'EXEC'
);
...
...
extensions/redis/LuaScriptBuilder.php
View file @
48199cfc
...
...
@@ -29,7 +29,7 @@ class LuaScriptBuilder extends \yii\base\Object
// TODO add support for orderBy
/** @var ActiveRecord $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'
);
}
...
...
@@ -43,7 +43,7 @@ class LuaScriptBuilder extends \yii\base\Object
// TODO add support for orderBy
/** @var ActiveRecord $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'
);
}
...
...
@@ -58,7 +58,7 @@ class LuaScriptBuilder extends \yii\base\Object
// TODO add support for orderBy and indexBy
/** @var ActiveRecord $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'
);
}
...
...
@@ -82,7 +82,7 @@ class LuaScriptBuilder extends \yii\base\Object
{
/** @var ActiveRecord $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'
);
}
...
...
@@ -96,7 +96,7 @@ class LuaScriptBuilder extends \yii\base\Object
{
/** @var ActiveRecord $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'
);
}
...
...
@@ -110,7 +110,7 @@ class LuaScriptBuilder extends \yii\base\Object
{
/** @var ActiveRecord $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'
);
}
...
...
@@ -124,7 +124,7 @@ class LuaScriptBuilder extends \yii\base\Object
{
/** @var ActiveRecord $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'
);
}
...
...
@@ -152,7 +152,7 @@ class LuaScriptBuilder extends \yii\base\Object
/** @var ActiveRecord $modelClass */
$modelClass
=
$query
->
modelClass
;
$key
=
$this
->
quoteValue
(
$modelClass
::
tableName
());
$key
=
$this
->
quoteValue
(
$modelClass
::
keyPrefix
());
$loadColumnValues
=
''
;
foreach
(
$columns
as
$column
=>
$alias
)
{
$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