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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
b06d7a86
Commit
b06d7a86
authored
Jun 12, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored cache dependency methods.
parent
870f6534
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
40 additions
and
32 deletions
+40
-32
Cache.php
framework/yii/caching/Cache.php
+13
-15
ChainedDependency.php
framework/yii/caching/ChainedDependency.php
+9
-6
DbDependency.php
framework/yii/caching/DbDependency.php
+3
-2
Dependency.php
framework/yii/caching/Dependency.php
+11
-7
ExpressionDependency.php
framework/yii/caching/ExpressionDependency.php
+2
-1
FileDependency.php
framework/yii/caching/FileDependency.php
+2
-1
No files found.
framework/yii/caching/Cache.php
View file @
b06d7a86
...
...
@@ -95,16 +95,10 @@ abstract class Cache extends Component implements \ArrayAccess
* 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:
*
* ~~~
* $key = $cache->buildKey(array($className, $method, $id));
* ~~~
*
* @param mixed $key the key to be normalized
* @return string the generated cache key
*/
p
ublic
function
buildKey
(
$key
)
p
rotected
function
buildKey
(
$key
)
{
if
(
is_string
(
$key
))
{
$key
=
ctype_alnum
(
$key
)
&&
StringHelper
::
strlen
(
$key
)
<=
32
?
$key
:
md5
(
$key
);
...
...
@@ -116,7 +110,8 @@ abstract class Cache extends Component implements \ArrayAccess
/**
* Retrieves a value from cache with a specified key.
* @param string $key a key identifying the cached value
* @param mixed $key a key identifying the cached value. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @return mixed the value stored in cache, false if the value is not in the cache, expired,
* or the dependency associated with the cached data has changed.
*/
...
...
@@ -131,7 +126,7 @@ abstract class Cache extends Component implements \ArrayAccess
}
else
{
$value
=
call_user_func
(
$this
->
serializer
[
1
],
$value
);
}
if
(
is_array
(
$value
)
&&
!
(
$value
[
1
]
instanceof
Dependency
&&
$value
[
1
]
->
getHasChanged
()))
{
if
(
is_array
(
$value
)
&&
!
(
$value
[
1
]
instanceof
Dependency
&&
$value
[
1
]
->
getHasChanged
(
$this
)))
{
return
$value
[
0
];
}
else
{
return
false
;
...
...
@@ -165,7 +160,7 @@ abstract class Cache extends Component implements \ArrayAccess
$value
=
$this
->
serializer
===
null
?
unserialize
(
$values
[
$newKey
])
:
call_user_func
(
$this
->
serializer
[
1
],
$values
[
$newKey
]);
if
(
is_array
(
$value
)
&&
!
(
$value
[
1
]
instanceof
Dependency
&&
$value
[
1
]
->
getHasChanged
()))
{
if
(
is_array
(
$value
)
&&
!
(
$value
[
1
]
instanceof
Dependency
&&
$value
[
1
]
->
getHasChanged
(
$this
)))
{
$results
[
$key
]
=
$value
[
0
];
}
}
...
...
@@ -179,7 +174,8 @@ abstract class Cache extends Component implements \ArrayAccess
* If the cache already contains such a key, the existing value and
* expiration time will be replaced with the new ones, respectively.
*
* @param string $key the key identifying the value to be cached
* @param mixed $key a key identifying the value to be cached value. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @param mixed $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param Dependency $dependency dependency of the cached item. If the dependency changes,
...
...
@@ -190,7 +186,7 @@ abstract class Cache extends Component implements \ArrayAccess
public
function
set
(
$key
,
$value
,
$expire
=
0
,
$dependency
=
null
)
{
if
(
$dependency
!==
null
&&
$this
->
serializer
!==
false
)
{
$dependency
->
evaluateDependency
();
$dependency
->
evaluateDependency
(
$this
);
}
if
(
$this
->
serializer
===
null
)
{
$value
=
serialize
(
array
(
$value
,
$dependency
));
...
...
@@ -204,7 +200,8 @@ abstract class Cache extends Component implements \ArrayAccess
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* Nothing will be done if the cache already contains the key.
* @param string $key the key identifying the value to be cached
* @param mixed $key a key identifying the value to be cached value. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @param mixed $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param Dependency $dependency dependency of the cached item. If the dependency changes,
...
...
@@ -215,7 +212,7 @@ abstract class Cache extends Component implements \ArrayAccess
public
function
add
(
$key
,
$value
,
$expire
=
0
,
$dependency
=
null
)
{
if
(
$dependency
!==
null
&&
$this
->
serializer
!==
false
)
{
$dependency
->
evaluateDependency
();
$dependency
->
evaluateDependency
(
$this
);
}
if
(
$this
->
serializer
===
null
)
{
$value
=
serialize
(
array
(
$value
,
$dependency
));
...
...
@@ -228,7 +225,8 @@ abstract class Cache extends Component implements \ArrayAccess
/**
* Deletes a value with the specified key from cache
* @param string $key the key of the value to be deleted
* @param mixed $key a key identifying the value to be deleted from cache. This can be a simple string or
* a complex data structure consisting of factors representing the key.
* @return boolean if no error happens during deletion
*/
public
function
delete
(
$key
)
...
...
framework/yii/caching/ChainedDependency.php
View file @
b06d7a86
...
...
@@ -48,20 +48,22 @@ class ChainedDependency extends Dependency
/**
* Evaluates the dependency by generating and saving the data related with dependency.
* @param Cache $cache the cache component that is currently evaluating this dependency
*/
public
function
evaluateDependency
()
public
function
evaluateDependency
(
$cache
)
{
foreach
(
$this
->
dependencies
as
$dependency
)
{
$dependency
->
evaluateDependency
();
$dependency
->
evaluateDependency
(
$cache
);
}
}
/**
* Generates the data needed to determine if dependency has been changed.
* This method does nothing in this class.
* @param Cache $cache the cache component that is currently evaluating this dependency
* @return mixed the data needed to determine if dependency has been changed.
*/
protected
function
generateDependencyData
()
protected
function
generateDependencyData
(
$cache
)
{
return
null
;
}
...
...
@@ -70,14 +72,15 @@ class ChainedDependency extends Dependency
* Performs the actual dependency checking.
* This method returns true if any of the dependency objects
* reports a dependency change.
* @param Cache $cache the cache component that is currently evaluating this dependency
* @return boolean whether the dependency is changed or not.
*/
public
function
getHasChanged
()
public
function
getHasChanged
(
$cache
)
{
foreach
(
$this
->
dependencies
as
$dependency
)
{
if
(
$this
->
dependOnAll
&&
$dependency
->
getHasChanged
())
{
if
(
$this
->
dependOnAll
&&
$dependency
->
getHasChanged
(
$cache
))
{
return
true
;
}
elseif
(
!
$this
->
dependOnAll
&&
!
$dependency
->
getHasChanged
())
{
}
elseif
(
!
$this
->
dependOnAll
&&
!
$dependency
->
getHasChanged
(
$cache
))
{
return
false
;
}
}
...
...
framework/yii/caching/DbDependency.php
View file @
b06d7a86
...
...
@@ -52,10 +52,11 @@ class DbDependency extends Dependency
/**
* Generates the data needed to determine if dependency has been changed.
* This method returns the value of the global state.
* @
throws InvalidConfigException
* @
param Cache $cache the cache component that is currently evaluating this dependency
* @return mixed the data needed to determine if dependency has been changed.
* @throws InvalidConfigException if [[db]] is not a valid application component ID
*/
protected
function
generateDependencyData
()
protected
function
generateDependencyData
(
$cache
)
{
$db
=
Yii
::
$app
->
getComponent
(
$this
->
db
);
if
(
!
$db
instanceof
Connection
)
{
...
...
framework/yii/caching/Dependency.php
View file @
b06d7a86
...
...
@@ -46,35 +46,38 @@ abstract class Dependency extends \yii\base\Object
/**
* Evaluates the dependency by generating and saving the data related with dependency.
* This method is invoked by cache before writing data into it.
* @param Cache $cache the cache component that is currently evaluating this dependency
*/
public
function
evaluateDependency
()
public
function
evaluateDependency
(
$cache
)
{
if
(
!
$this
->
reusable
)
{
$this
->
data
=
$this
->
generateDependencyData
();
$this
->
data
=
$this
->
generateDependencyData
(
$cache
);
}
else
{
if
(
$this
->
_hash
===
null
)
{
$this
->
_hash
=
sha1
(
serialize
(
$this
));
}
if
(
!
array_key_exists
(
$this
->
_hash
,
self
::
$_reusableData
))
{
self
::
$_reusableData
[
$this
->
_hash
]
=
$this
->
generateDependencyData
();
self
::
$_reusableData
[
$this
->
_hash
]
=
$this
->
generateDependencyData
(
$cache
);
}
$this
->
data
=
self
::
$_reusableData
[
$this
->
_hash
];
}
}
/**
* Returns a value indicating whether the dependency has changed.
* @param Cache $cache the cache component that is currently evaluating this dependency
* @return boolean whether the dependency has changed.
*/
public
function
getHasChanged
()
public
function
getHasChanged
(
$cache
)
{
if
(
!
$this
->
reusable
)
{
return
$this
->
generateDependencyData
()
!==
$this
->
data
;
return
$this
->
generateDependencyData
(
$cache
)
!==
$this
->
data
;
}
else
{
if
(
$this
->
_hash
===
null
)
{
$this
->
_hash
=
sha1
(
serialize
(
$this
));
}
if
(
!
array_key_exists
(
$this
->
_hash
,
self
::
$_reusableData
))
{
self
::
$_reusableData
[
$this
->
_hash
]
=
$this
->
generateDependencyData
();
self
::
$_reusableData
[
$this
->
_hash
]
=
$this
->
generateDependencyData
(
$cache
);
}
return
self
::
$_reusableData
[
$this
->
_hash
]
!==
$this
->
data
;
}
...
...
@@ -91,7 +94,8 @@ abstract class Dependency extends \yii\base\Object
/**
* Generates the data needed to determine if dependency has been changed.
* Derived classes should override this method to generate the actual dependency data.
* @param Cache $cache the cache component that is currently evaluating this dependency
* @return mixed the data needed to determine if dependency has been changed.
*/
abstract
protected
function
generateDependencyData
();
abstract
protected
function
generateDependencyData
(
$cache
);
}
framework/yii/caching/ExpressionDependency.php
View file @
b06d7a86
...
...
@@ -50,9 +50,10 @@ class ExpressionDependency extends Dependency
/**
* Generates the data needed to determine if dependency has been changed.
* This method returns the result of the PHP expression.
* @param Cache $cache the cache component that is currently evaluating this dependency
* @return mixed the data needed to determine if dependency has been changed.
*/
protected
function
generateDependencyData
()
protected
function
generateDependencyData
(
$cache
)
{
return
eval
(
"return
{
$this
->
expression
}
;"
);
}
...
...
framework/yii/caching/FileDependency.php
View file @
b06d7a86
...
...
@@ -38,9 +38,10 @@ class FileDependency extends Dependency
/**
* Generates the data needed to determine if dependency has been changed.
* This method returns the file's last modification time.
* @param Cache $cache the cache component that is currently evaluating this dependency
* @return mixed the data needed to determine if dependency has been changed.
*/
protected
function
generateDependencyData
()
protected
function
generateDependencyData
(
$cache
)
{
return
@
filemtime
(
$this
->
fileName
);
}
...
...
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