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
f5d0bcbc
Commit
f5d0bcbc
authored
May 26, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored cache key generation.
parent
f17fde4a
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
27 additions
and
24 deletions
+27
-24
Cache.php
framework/yii/caching/Cache.php
+13
-9
Command.php
framework/yii/db/Command.php
+2
-2
Schema.php
framework/yii/db/Schema.php
+6
-7
CacheSession.php
framework/yii/web/CacheSession.php
+2
-2
UrlManager.php
framework/yii/web/UrlManager.php
+1
-1
FragmentCache.php
framework/yii/widgets/FragmentCache.php
+2
-2
CacheTestCase.php
tests/unit/framework/caching/CacheTestCase.php
+1
-1
No files found.
framework/yii/caching/Cache.php
View file @
f5d0bcbc
...
...
@@ -7,7 +7,9 @@
namespace
yii\caching
;
use
Yii
;
use
yii\base\Component
;
use
yii\base\InvalidConfigException
;
use
yii\helpers\StringHelper
;
/**
...
...
@@ -52,10 +54,12 @@ use yii\helpers\StringHelper;
abstract
class
Cache
extends
Component
implements
\ArrayAccess
{
/**
* @var string a string prefixed to every cache key so that it is unique.
Defaults to null, meaning using
*
the value of [[Application::id]] as the key prefix
. You may set this property to be an empty string
* @var string a string prefixed to every cache key so that it is unique.
If not set,
*
it will use a prefix generated from [[Application::id]]
. You may set this property to be an empty string
* if you don't want to use key prefix. It is recommended that you explicitly set this property to some
* static value if the cached data needs to be shared among multiple applications.
*
* To ensure interoperability, only use alphanumeric characters should be used.
*/
public
$keyPrefix
;
/**
...
...
@@ -78,18 +82,18 @@ abstract class Cache extends Component implements \ArrayAccess
{
parent
::
init
();
if
(
$this
->
keyPrefix
===
null
)
{
$this
->
keyPrefix
=
\Yii
::
$app
->
id
;
$this
->
keyPrefix
=
substr
(
md5
(
Yii
::
$app
->
id
),
0
,
5
);
}
elseif
(
!
ctype_alnum
(
$this
->
keyPrefix
))
{
throw
new
InvalidConfigException
(
get_class
(
$this
)
.
'::keyPrefix should only contain alphanumeric characters.'
);
}
}
/**
* Builds a normalized cache key from a given key.
*
* The generated key contains letters and digits only, and its length is no more than 32.
*
* If the given key is a string containing alphanumeric characters only and no more than 32 characters,
* then the key will be returned back
without change
. Otherwise, a normalized key
* is generated by serializing the given key
and applying MD5 hashing
.
* then the key will be returned back
prefixed with [[keyPrefix]]
. Otherwise, a normalized key
* is generated by serializing the given key
, applying MD5 hashing, and prefixing with [[keyPrefix]]
.
*
* The following example builds a cache key using three parameters:
*
...
...
@@ -97,8 +101,8 @@ abstract class Cache extends Component implements \ArrayAccess
* $key = $cache->buildKey(array($className, $method, $id));
* ~~~
*
* @param
array|string
$key the key to be normalized
* @return string the generated cache key
include $this->keyPrefix
* @param
mixed
$key the key to be normalized
* @return string the generated cache key
*/
public
function
buildKey
(
$key
)
{
...
...
framework/yii/db/Command.php
View file @
f5d0bcbc
...
...
@@ -391,12 +391,12 @@ class Command extends \yii\base\Component
}
if
(
isset
(
$cache
)
&&
$cache
instanceof
Cache
)
{
$cacheKey
=
$cache
->
buildKey
(
array
(
$cacheKey
=
array
(
__CLASS__
,
$db
->
dsn
,
$db
->
username
,
$rawSql
,
)
)
;
);
if
((
$result
=
$cache
->
get
(
$cacheKey
))
!==
false
)
{
Yii
::
trace
(
'Query result served from cache'
,
__METHOD__
);
return
$result
;
...
...
framework/yii/db/Schema.php
View file @
f5d0bcbc
...
...
@@ -89,7 +89,7 @@ abstract class Schema extends \yii\base\Object
/** @var $cache Cache */
$cache
=
is_string
(
$db
->
schemaCache
)
?
Yii
::
$app
->
getComponent
(
$db
->
schemaCache
)
:
$db
->
schemaCache
;
if
(
$cache
instanceof
Cache
)
{
$key
=
$this
->
getCacheKey
(
$
cache
,
$
name
);
$key
=
$this
->
getCacheKey
(
$name
);
if
(
$refresh
||
(
$table
=
$cache
->
get
(
$key
))
===
false
)
{
$table
=
$this
->
loadTableSchema
(
$realName
);
if
(
$table
!==
null
)
{
...
...
@@ -104,18 +104,17 @@ abstract class Schema extends \yii\base\Object
/**
* Returns the cache key for the specified table name.
* @param Cache $cache the cache component
* @param string $name the table name
* @return
string
the cache key
* @return
mixed
the cache key
*/
public
function
getCacheKey
(
$
cache
,
$
name
)
public
function
getCacheKey
(
$name
)
{
return
$cache
->
buildKey
(
array
(
return
array
(
__CLASS__
,
$this
->
db
->
dsn
,
$this
->
db
->
username
,
$name
,
)
)
;
);
}
/**
...
...
@@ -178,7 +177,7 @@ abstract class Schema extends \yii\base\Object
$cache
=
is_string
(
$this
->
db
->
schemaCache
)
?
Yii
::
$app
->
getComponent
(
$this
->
db
->
schemaCache
)
:
$this
->
db
->
schemaCache
;
if
(
$this
->
db
->
enableSchemaCache
&&
$cache
instanceof
Cache
)
{
foreach
(
$this
->
_tables
as
$name
=>
$table
)
{
$cache
->
delete
(
$this
->
getCacheKey
(
$
cache
,
$
name
));
$cache
->
delete
(
$this
->
getCacheKey
(
$name
));
}
}
$this
->
_tableNames
=
array
();
...
...
framework/yii/web/CacheSession.php
View file @
f5d0bcbc
...
...
@@ -97,10 +97,10 @@ class CacheSession extends Session
/**
* Generates a unique key used for storing session data in cache.
* @param string $id session variable name
* @return
string
a safe cache key associated with the session variable name
* @return
mixed
a safe cache key associated with the session variable name
*/
protected
function
calculateKey
(
$id
)
{
return
$this
->
cache
->
buildKey
(
array
(
__CLASS__
,
$id
)
);
return
array
(
__CLASS__
,
$id
);
}
}
framework/yii/web/UrlManager.php
View file @
f5d0bcbc
...
...
@@ -103,7 +103,7 @@ class UrlManager extends Component
$this
->
cache
=
Yii
::
$app
->
getComponent
(
$this
->
cache
);
}
if
(
$this
->
cache
instanceof
Cache
)
{
$key
=
$this
->
cache
->
buildKey
(
__CLASS__
)
;
$key
=
__CLASS__
;
$hash
=
md5
(
json_encode
(
$this
->
rules
));
if
((
$data
=
$this
->
cache
->
get
(
$key
))
!==
false
&&
isset
(
$data
[
1
])
&&
$data
[
1
]
===
$hash
)
{
$this
->
rules
=
$data
[
0
];
...
...
framework/yii/widgets/FragmentCache.php
View file @
f5d0bcbc
...
...
@@ -159,7 +159,7 @@ class FragmentCache extends Widget
/**
* Generates a unique key used for storing the content in cache.
* The key generated depends on both [[id]] and [[variations]].
* @return
string
a valid cache key
* @return
mixed
a valid cache key
*/
protected
function
calculateKey
()
{
...
...
@@ -169,6 +169,6 @@ class FragmentCache extends Widget
$factors
[]
=
$factor
;
}
}
return
$
this
->
cache
->
buildKey
(
$factors
)
;
return
$
factors
;
}
}
tests/unit/framework/caching/CacheTestCase.php
View file @
f5d0bcbc
...
...
@@ -65,7 +65,7 @@ abstract class CacheTestCase extends TestCase
{
$cache
=
$this
->
getCacheInstance
();
$this
->
assertNotNull
(
\Yii
::
$app
->
id
);
$this
->
assert
Equals
(
\Yii
::
$app
->
id
,
$cache
->
keyPrefix
);
$this
->
assert
NotNull
(
$cache
->
keyPrefix
);
}
public
function
testSet
()
...
...
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