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
78623f3e
Commit
78623f3e
authored
May 29, 2012
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finished all cache components.
parent
23be1a7c
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
198 additions
and
228 deletions
+198
-228
DbCache.php
framework/caching/DbCache.php
+79
-106
FileCache.php
framework/caching/FileCache.php
+108
-119
MemCache.php
framework/caching/MemCache.php
+1
-1
Command.php
framework/db/dao/Command.php
+10
-2
No files found.
framework/caching/DbCache.php
View file @
78623f3e
...
@@ -16,16 +16,28 @@ use yii\db\dao\Query;
...
@@ -16,16 +16,28 @@ use yii\db\dao\Query;
/**
/**
* DbCache implements a cache application component by storing cached data in a database.
* DbCache implements a cache application component by storing cached data in a database.
*
*
* DbCache stores cache data in a DB table named {@link cacheTableName}.
* DbCache stores cache data in a DB table whose name is specified via [[cacheTableName]].
* If the table does not exist, it will be automatically created.
* For MySQL database, the table should be created beforehand as follows :
* By setting {@link autoCreateCacheTable} to false, you can also manually create the DB table.
*
*
* DbCache relies on {@link http://www.php.net/manual/en/ref.pdo.php PDO} to access database.
* ~~~
* By default, it will use a SQLite3 database under the application runtime directory.
* CREATE TABLE tbl_cache (
* You can also specify {@link connectionID} so that it makes use of
* id char(128) NOT NULL,
* a DB application component to access database.
* expire int(11) DEFAULT NULL,
* data LONGBLOB,
* PRIMARY KEY (id),
* KEY expire (expire)
* );
* ~~~
*
*
* See {@link CCache} manual for common cache operations that are supported by DbCache.
* You should replace `LONGBLOB` as follows if you are using a different DBMS:
*
* - PostgreSQL: `BYTEA`
* - SQLite, SQL server, Oracle: `BLOB`
*
* DbCache connects to the database via the DB connection specified in [[connectionID]]
* which must refer to a valid DB application component.
*
* Please refer to [[Cache]] for common cache operations that are supported by DbCache.
*
*
* @property Connection $dbConnection The DB connection instance.
* @property Connection $dbConnection The DB connection instance.
*
*
...
@@ -35,30 +47,16 @@ use yii\db\dao\Query;
...
@@ -35,30 +47,16 @@ use yii\db\dao\Query;
class
DbCache
extends
Cache
class
DbCache
extends
Cache
{
{
/**
/**
* @var string the ID of the {@link Connection} application component. If not set,
* @var string the ID of the [[Connection|DB connection]] application component.
* a SQLite3 database will be automatically created and used. The SQLite database file
* Defaults to 'db'.
* is <code>protected/runtime/cache-YiiVersion.db</code>.
*/
*/
public
$connectionID
;
public
$connectionID
=
'db'
;
/**
/**
* @var string name of the DB table to store cache content. Defaults to 'YiiCache'.
* @var string name of the DB table to store cache content. Defaults to 'tbl_cache'.
* Note, if {@link autoCreateCacheTable} is false and you want to create the DB table
* The table must be created before using this cache component.
* manually by yourself, you need to make sure the DB table is of the following structure:
* <pre>
* (id CHAR(128) PRIMARY KEY, expire INTEGER, value BLOB)
* </pre>
* Note, some DBMS might not support BLOB type. In this case, replace 'BLOB' with a suitable
* binary data type (e.g. LONGBLOB in MySQL, BYTEA in PostgreSQL.)
* @see autoCreateCacheTable
*/
*/
public
$cacheTableName
=
'tbl_cache'
;
public
$cacheTableName
=
'tbl_cache'
;
/**
/**
* @var boolean whether the cache DB table should be created automatically if it does not exist. Defaults to true.
* If you already have the table created, it is recommended you set this property to be false to improve performance.
* @see cacheTableName
*/
public
$autoCreateCacheTable
=
true
;
/**
* @var integer the probability (parts per million) that garbage collection (GC) should be performed
* @var integer the probability (parts per million) that garbage collection (GC) should be performed
* when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
* when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
* This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all.
* This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all.
...
@@ -70,57 +68,6 @@ class DbCache extends Cache
...
@@ -70,57 +68,6 @@ class DbCache extends Cache
private
$_db
;
private
$_db
;
/**
/**
* Initializes this application component.
*
* This method is required by the {@link IApplicationComponent} interface.
* It ensures the existence of the cache DB table.
* It also removes expired data items from the cache.
*/
public
function
init
()
{
parent
::
init
();
$db
=
$this
->
getDbConnection
();
$db
->
setActive
(
true
);
if
(
$this
->
autoCreateCacheTable
)
{
$sql
=
"DELETE FROM
{
$this
->
cacheTableName
}
WHERE expire>0 AND expire<"
.
time
();
try
{
$db
->
createCommand
(
$sql
)
->
execute
();
}
catch
(
Exception
$e
)
{
$this
->
createCacheTable
(
$db
,
$this
->
cacheTableName
);
}
}
}
/**
* Creates the cache DB table.
* @param Connection $db the database connection
* @param string $tableName the name of the table to be created
*/
protected
function
createCacheTable
(
$db
,
$tableName
)
{
$driver
=
$db
->
getDriverName
();
if
(
$driver
===
'mysql'
)
{
$blob
=
'LONGBLOB'
;
}
else
{
if
(
$driver
===
'pgsql'
)
{
$blob
=
'BYTEA'
;
}
else
{
$blob
=
'BLOB'
;
}
}
$sql
=
<<<EOD
CREATE TABLE $tableName
(
id CHAR(128) PRIMARY KEY,
expire INTEGER,
value $blob
)
EOD;
$db
->
createCommand
(
$sql
)
->
execute
();
}
/**
* Returns the DB connection instance used for caching purpose.
* Returns the DB connection instance used for caching purpose.
* @return Connection the DB connection instance
* @return Connection the DB connection instance
* @throws Exception if [[connectionID]] does not point to a valid application component.
* @throws Exception if [[connectionID]] does not point to a valid application component.
...
@@ -128,7 +75,8 @@ EOD;
...
@@ -128,7 +75,8 @@ EOD;
public
function
getDbConnection
()
public
function
getDbConnection
()
{
{
if
(
$this
->
_db
===
null
)
{
if
(
$this
->
_db
===
null
)
{
if
(
$this
->
connectionID
!==
null
&&
(
$db
=
\Yii
::
$application
->
getComponent
(
$this
->
connectionID
))
instanceof
Connection
)
{
$db
=
\Yii
::
$application
->
getComponent
(
$this
->
connectionID
);
if
(
$db
instanceof
Connection
)
{
$this
->
_db
=
$db
;
$this
->
_db
=
$db
;
}
else
{
}
else
{
throw
new
Exception
(
"DbCache.connectionID must refer to the ID of a DB connection application component."
);
throw
new
Exception
(
"DbCache.connectionID must refer to the ID of a DB connection application component."
);
...
@@ -155,10 +103,9 @@ EOD;
...
@@ -155,10 +103,9 @@ EOD;
protected
function
getValue
(
$key
)
protected
function
getValue
(
$key
)
{
{
$query
=
new
Query
;
$query
=
new
Query
;
$query
->
select
(
array
(
'
value
'
))
$query
->
select
(
array
(
'
data
'
))
->
from
(
$this
->
cacheTableName
)
->
from
(
$this
->
cacheTableName
)
->
where
(
array
(
'id'
=>
$key
))
->
where
(
'id = :id AND (expire = 0 OR expire > :time)'
,
array
(
':id'
=>
$key
,
':time'
=>
time
()));
->
andWhere
(
'expire = 0 OR expire > '
.
time
());
$db
=
$this
->
getDbConnection
();
$db
=
$this
->
getDbConnection
();
if
(
$db
->
queryCachingDuration
>=
0
)
{
if
(
$db
->
queryCachingDuration
>=
0
)
{
$duration
=
$db
->
queryCachingDuration
;
$duration
=
$db
->
queryCachingDuration
;
...
@@ -181,19 +128,20 @@ EOD;
...
@@ -181,19 +128,20 @@ EOD;
if
(
empty
(
$keys
))
{
if
(
empty
(
$keys
))
{
return
array
();
return
array
();
}
}
$query
=
new
Query
;
$ids
=
implode
(
"','"
,
$keys
);
$query
->
select
(
array
(
'id'
,
'data'
))
$time
=
time
();
->
from
(
$this
->
cacheTableName
)
$sql
=
"SELECT id, value FROM
{
$this
->
cacheTableName
}
WHERE id IN ('
$ids
') AND (expire=0 OR expire>
$time
)"
;
->
where
(
array
(
'id'
=>
$keys
))
->
andWhere
(
"expire = 0 OR expire > "
.
time
()
.
")"
);
$db
=
$this
->
getDbConnection
();
$db
=
$this
->
getDbConnection
();
if
(
$db
->
queryCachingDuration
>
0
)
{
if
(
$db
->
queryCachingDuration
>
=
0
)
{
$duration
=
$db
->
queryCachingDuration
;
$duration
=
$db
->
queryCachingDuration
;
$db
->
queryCachingDuration
=
0
;
$db
->
queryCachingDuration
=
-
1
;
$rows
=
$
db
->
createCommand
(
$sql
)
->
queryAll
();
$rows
=
$
query
->
createCommand
(
$db
)
->
queryAll
();
$db
->
queryCachingDuration
=
$duration
;
$db
->
queryCachingDuration
=
$duration
;
}
else
{
}
else
{
$rows
=
$
db
->
createCommand
(
$sql
)
->
queryAll
();
$rows
=
$
query
->
createCommand
(
$db
)
->
queryAll
();
}
}
$results
=
array
();
$results
=
array
();
...
@@ -201,7 +149,7 @@ EOD;
...
@@ -201,7 +149,7 @@ EOD;
$results
[
$key
]
=
false
;
$results
[
$key
]
=
false
;
}
}
foreach
(
$rows
as
$row
)
{
foreach
(
$rows
as
$row
)
{
$results
[
$row
[
'id'
]]
=
$row
[
'
value
'
];
$results
[
$row
[
'id'
]]
=
$row
[
'
data
'
];
}
}
return
$results
;
return
$results
;
}
}
...
@@ -217,9 +165,21 @@ EOD;
...
@@ -217,9 +165,21 @@ EOD;
*/
*/
protected
function
setValue
(
$key
,
$value
,
$expire
)
protected
function
setValue
(
$key
,
$value
,
$expire
)
{
{
$this
->
deleteValue
(
$key
);
$query
=
new
Query
;
return
$this
->
addValue
(
$key
,
$value
,
$expire
);
$command
=
$query
->
update
(
$this
->
cacheTableName
,
array
(
}
'expire'
=>
$expire
>
0
?
$expire
+
time
()
:
0
,
'data'
=>
array
(
$value
,
\PDO
::
PARAM_LOB
),
),
array
(
'id'
=>
$key
,
))
->
createCommand
(
$this
->
getDbConnection
());
if
(
$command
->
execute
())
{
$this
->
gc
();
return
true
;
}
else
{
return
$this
->
addValue
(
$key
,
$value
,
$expire
);
}
}
/**
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* Stores a value identified by a key into cache if the cache does not contain this key.
...
@@ -232,19 +192,21 @@ EOD;
...
@@ -232,19 +192,21 @@ EOD;
*/
*/
protected
function
addValue
(
$key
,
$value
,
$expire
)
protected
function
addValue
(
$key
,
$value
,
$expire
)
{
{
if
(
mt_rand
(
0
,
1000000
)
<
$this
->
_gcProbability
)
{
$this
->
gc
();
$this
->
gc
();
}
if
(
$expire
>
0
)
{
if
(
$expire
>
0
)
{
$expire
+=
time
();
$expire
+=
time
();
}
else
{
}
else
{
$expire
=
0
;
$expire
=
0
;
}
}
$sql
=
"INSERT INTO
{
$this
->
cacheTableName
}
(id,expire,value) VALUES ('
$key
',
$expire
,:value)"
;
$query
=
new
Query
;
$command
=
$query
->
insert
(
$this
->
cacheTableName
,
array
(
'id'
=>
$key
,
'expire'
=>
$expire
,
'data'
=>
array
(
$value
,
\PDO
::
PARAM_LOB
),
))
->
createCommand
(
$this
->
getDbConnection
());
try
{
try
{
$command
=
$this
->
getDbConnection
()
->
createCommand
(
$sql
);
$command
->
bindValue
(
':value'
,
$value
,
PDO
::
PARAM_LOB
);
$command
->
execute
();
$command
->
execute
();
return
true
;
return
true
;
}
catch
(
Exception
$e
)
{
}
catch
(
Exception
$e
)
{
...
@@ -260,28 +222,39 @@ EOD;
...
@@ -260,28 +222,39 @@ EOD;
*/
*/
protected
function
deleteValue
(
$key
)
protected
function
deleteValue
(
$key
)
{
{
$sql
=
"DELETE FROM
{
$this
->
cacheTableName
}
WHERE id='
$key
'"
;
$query
=
new
Query
;
$this
->
getDbConnection
()
->
createCommand
(
$sql
)
->
execute
();
$query
->
delete
(
$this
->
cacheTableName
,
array
(
'id'
=>
$key
))
->
createCommand
(
$this
->
getDbConnection
())
->
execute
();
return
true
;
return
true
;
}
}
/**
/**
* Removes the expired data values.
* Removes the expired data values.
* @param boolean $force whether to enforce the garbage collection regardless of [[gcProbability]].
* Defaults to false, meaning the actual deletion happens with the probability as specified by [[gcProbability]].
*/
*/
p
rotected
function
gc
(
)
p
ublic
function
gc
(
$force
=
false
)
{
{
$this
->
getDbConnection
()
->
createCommand
(
"DELETE FROM
{
$this
->
cacheTableName
}
WHERE expire>0 AND expire<"
.
time
())
->
execute
();
if
(
$force
||
mt_rand
(
0
,
1000000
)
<
$this
->
gcProbability
)
{
$query
=
new
Query
;
$query
->
delete
(
$this
->
cacheTableName
,
'expire > 0 AND expire < '
.
time
())
->
createCommand
(
$this
->
getDbConnection
())
->
execute
();
}
}
}
/**
/**
* Deletes all values from cache.
* Deletes all values from cache.
* This is the implementation of the method declared in the parent class.
* This is the implementation of the method declared in the parent class.
* @return boolean whether the flush operation was successful.
* @return boolean whether the flush operation was successful.
* @since 1.1.5
*/
*/
protected
function
flushValues
()
protected
function
flushValues
()
{
{
$this
->
getDbConnection
()
->
createCommand
(
"DELETE FROM
{
$this
->
cacheTableName
}
"
)
->
execute
();
$query
=
new
Query
;
$query
->
delete
(
$this
->
cacheTableName
)
->
createCommand
(
$this
->
getDbConnection
())
->
execute
();
return
true
;
return
true
;
}
}
}
}
framework/caching/FileCache.php
View file @
78623f3e
<?php
<?php
/**
/**
*
C
FileCache class file
* FileCache class file
*
*
* @link http://www.yiiframework.com/
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2012 Yii Software LLC
* @copyright Copyright © 2008-2012 Yii Software LLC
...
@@ -9,92 +9,57 @@
...
@@ -9,92 +9,57 @@
namespace
yii\caching
;
namespace
yii\caching
;
use
yii\base\Exception
;
/**
/**
* CFileCache provides a file-based caching mechanism.
* FileCache implements a cache component using files.
*
* For each data value being cached, CFileCache will use store it in a separate file
* under {@link cachePath} which defaults to 'protected/runtime/cache'.
* CFileCache will perform garbage collection automatically to remove expired cache files.
*
*
* See {@link CCache} manual for common cache operations that are supported by CFileCache.
* For each data value being cached, FileCache will store it in a separate file.
* The cache files are placed under [[cachePath]]. FileCache will perform garbage collection
* automatically to remove expired cache files.
*
*
* @property integer $gCProbability The probability (parts per million) that garbage collection (GC) should be performed
* Please refer to [[Cache]] for common cache operations that are supported by FileCache.
* when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
*
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
* @since 2.0
*/
*/
class
CFileCache
extends
C
Cache
class
FileCache
extends
Cache
{
{
/**
/**
* @var string the directory to store cache files. Defaults to null, meaning
* @var string the directory to store cache files. You may use path alias here.
* using 'protected/runtime/cache' as the directory.
*/
*/
public
$cachePath
;
public
$cachePath
=
'@application/runtime/cache'
;
/**
/**
* @var string cache file suffix. Defaults to '.bin'.
* @var string cache file suffix. Defaults to '.bin'.
*/
*/
public
$cacheFileSuffix
=
'.bin'
;
public
$cacheFileSuffix
=
'.bin'
;
/**
* @var integer the level of sub-directories to store cache files. Defaults to 0,
* meaning no sub-directories. If the system has huge number of cache files (e.g. 10K+),
* you may want to set this value to be 1 or 2 so that the file system is not over burdened.
* The value of this property should not exceed 16 (less than 3 is recommended).
*/
public
$directoryLevel
=
0
;
private
$_gcProbability
=
100
;
private
$_gced
=
false
;
/**
* Initializes this application component.
* This method is required by the {@link IApplicationComponent} interface.
* It checks the availability of memcache.
* @throws CException if APC cache extension is not loaded or is disabled.
*/
public
function
init
()
{
parent
::
init
();
if
(
$this
->
cachePath
===
null
)
$this
->
cachePath
=
\Yii
::
$application
->
getRuntimePath
()
.
DIRECTORY_SEPARATOR
.
'cache'
;
if
(
!
is_dir
(
$this
->
cachePath
))
mkdir
(
$this
->
cachePath
,
0777
,
true
);
}
/**
/**
* @return integer the probability (parts per million) that garbage collection (GC) should be performed
* @var integer the level of sub-directories to store cache files. Defaults to 1.
* when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
* If the system has huge number of cache files (e.g. one million), you may use a bigger value
* (usually no bigger than 3). Using sub-directories is mainly to ensure the file system
* is not over burdened with a single directory having too many files.
*/
*/
public
function
getGCProbability
()
public
$directoryLevel
=
1
;
{
return
$this
->
_gcProbability
;
}
/**
/**
* @
param integer $value
the probability (parts per million) that garbage collection (GC) should be performed
* @
var integer
the probability (parts per million) that garbage collection (GC) should be performed
* when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
* when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
* This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all.
* This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all.
*/
**/
public
function
setGCProbability
(
$value
)
public
$gcProbability
=
100
;
{
$value
=
(
int
)
$value
;
if
(
$value
<
0
)
$value
=
0
;
if
(
$value
>
1000000
)
$value
=
1000000
;
$this
->
_gcProbability
=
$value
;
}
/**
/**
* Deletes all values from cache.
* Initializes this component by ensuring the existence of the cache path.
* This is the implementation of the method declared in the parent class.
* @return boolean whether the flush operation was successful.
* @since 1.1.5
*/
*/
p
rotected
function
flushValues
()
p
ublic
function
init
()
{
{
$this
->
gc
(
false
);
parent
::
init
();
return
true
;
$this
->
cachePath
=
\Yii
::
getAlias
(
$this
->
cachePath
);
if
(
$this
->
cachePath
===
false
)
{
throw
new
Exception
(
'FileCache.cachePath must be a valid path alias.'
);
}
if
(
!
is_dir
(
$this
->
cachePath
))
{
mkdir
(
$this
->
cachePath
,
0777
,
true
);
}
}
}
/**
/**
...
@@ -105,12 +70,12 @@ class CFileCache extends CCache
...
@@ -105,12 +70,12 @@ class CFileCache extends CCache
*/
*/
protected
function
getValue
(
$key
)
protected
function
getValue
(
$key
)
{
{
$cacheFile
=
$this
->
getCacheFile
(
$key
);
$cacheFile
=
$this
->
getCacheFile
(
$key
);
if
((
$time
=@
filemtime
(
$cacheFile
))
>
time
())
if
((
$time
=
@
filemtime
(
$cacheFile
))
>
time
())
{
return
@
file_get_contents
(
$cacheFile
);
return
@
file_get_contents
(
$cacheFile
);
else
if
(
$time
>
0
)
}
else
{
@
unlink
(
$cacheFile
)
;
return
false
;
return
false
;
}
}
}
/**
/**
...
@@ -122,28 +87,23 @@ class CFileCache extends CCache
...
@@ -122,28 +87,23 @@ class CFileCache extends CCache
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
*/
protected
function
setValue
(
$key
,
$value
,
$expire
)
protected
function
setValue
(
$key
,
$value
,
$expire
)
{
{
if
(
!
$this
->
_gced
&&
mt_rand
(
0
,
1000000
)
<
$this
->
_gcProbability
)
if
(
$expire
<=
0
)
{
{
$expire
=
31536000
;
// 1 year
$this
->
gc
();
$this
->
_gced
=
true
;
}
}
$expire
+=
time
();
if
(
$expire
<=
0
)
$cacheFile
=
$this
->
getCacheFile
(
$key
);
$expire
=
31536000
;
// 1 year
if
(
$this
->
directoryLevel
>
0
)
{
$expire
+=
time
();
@
mkdir
(
dirname
(
$cacheFile
),
0777
,
true
);
$cacheFile
=
$this
->
getCacheFile
(
$key
);
if
(
$this
->
directoryLevel
>
0
)
@
mkdir
(
dirname
(
$cacheFile
),
0777
,
true
);
if
(
@
file_put_contents
(
$cacheFile
,
$value
,
LOCK_EX
)
!==
false
)
{
@
chmod
(
$cacheFile
,
0777
);
return
@
touch
(
$cacheFile
,
$expire
);
}
}
else
if
(
@
file_put_contents
(
$cacheFile
,
$value
,
LOCK_EX
)
!==
false
)
{
@
chmod
(
$cacheFile
,
0777
);
return
@
touch
(
$cacheFile
,
$expire
);
}
else
{
return
false
;
return
false
;
}
}
}
/**
/**
...
@@ -155,12 +115,13 @@ class CFileCache extends CCache
...
@@ -155,12 +115,13 @@ class CFileCache extends CCache
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
*/
protected
function
addValue
(
$key
,
$value
,
$expire
)
protected
function
addValue
(
$key
,
$value
,
$expire
)
{
{
$cacheFile
=
$this
->
getCacheFile
(
$key
);
$cacheFile
=
$this
->
getCacheFile
(
$key
);
if
(
@
filemtime
(
$cacheFile
)
>
time
())
if
(
@
filemtime
(
$cacheFile
)
>
time
())
{
return
false
;
return
false
;
return
$this
->
setValue
(
$key
,
$value
,
$expire
);
}
return
$this
->
setValue
(
$key
,
$value
,
$expire
);
}
}
/**
/**
...
@@ -171,7 +132,7 @@ class CFileCache extends CCache
...
@@ -171,7 +132,7 @@ class CFileCache extends CCache
*/
*/
protected
function
deleteValue
(
$key
)
protected
function
deleteValue
(
$key
)
{
{
$cacheFile
=
$this
->
getCacheFile
(
$key
);
$cacheFile
=
$this
->
getCacheFile
(
$key
);
return
@
unlink
(
$cacheFile
);
return
@
unlink
(
$cacheFile
);
}
}
...
@@ -182,41 +143,69 @@ class CFileCache extends CCache
...
@@ -182,41 +143,69 @@ class CFileCache extends CCache
*/
*/
protected
function
getCacheFile
(
$key
)
protected
function
getCacheFile
(
$key
)
{
{
if
(
$this
->
directoryLevel
>
0
)
if
(
$this
->
directoryLevel
>
0
)
{
{
$base
=
$this
->
cachePath
;
$base
=
$this
->
cachePath
;
for
(
$i
=
0
;
$i
<
$this
->
directoryLevel
;
++
$i
)
{
for
(
$i
=
0
;
$i
<
$this
->
directoryLevel
;
++
$i
)
if
((
$prefix
=
substr
(
$key
,
$i
+
$i
,
2
))
!==
false
)
{
{
$base
.=
DIRECTORY_SEPARATOR
.
$prefix
;
if
((
$prefix
=
substr
(
$key
,
$i
+
$i
,
2
))
!==
false
)
}
$base
.=
DIRECTORY_SEPARATOR
.
$prefix
;
}
}
return
$base
.
DIRECTORY_SEPARATOR
.
$key
.
$this
->
cacheFileSuffix
;
return
$base
.
DIRECTORY_SEPARATOR
.
$key
.
$this
->
cacheFileSuffix
;
}
else
{
return
$this
->
cachePath
.
DIRECTORY_SEPARATOR
.
$key
.
$this
->
cacheFileSuffix
;
}
}
else
}
return
$this
->
cachePath
.
DIRECTORY_SEPARATOR
.
$key
.
$this
->
cacheFileSuffix
;
/**
* Deletes all values from cache.
* This is the implementation of the method declared in the parent class.
* @return boolean whether the flush operation was successful.
*/
protected
function
flushValues
()
{
$this
->
gc
(
true
,
false
);
return
true
;
}
}
/**
/**
* Removes expired cache files.
* Removes expired cache files.
* @param boolean $expiredOnly whether to removed expired cache files only. If true, all cache files under {@link cachePath} will be removed.
* @param boolean $force whether to enforce the garbage collection regardless of [[gcProbability]].
* @param string $path the path to clean with. If null, it will be {@link cachePath}.
* Defaults to false, meaning the actual deletion happens with the probability as specified by [[gcProbability]].
* @param boolean $expiredOnly whether to removed expired cache files only.
* If true, all cache files under [[cachePath]] will be removed.
*/
public
function
gc
(
$force
=
false
,
$expiredOnly
=
true
)
{
if
(
$force
||
mt_rand
(
0
,
1000000
)
<
$this
->
gcProbability
)
{
$this
->
gcRecursive
(
$this
->
cachePath
,
$expiredOnly
);
}
}
/**
* Recursively removing expired cache files under a directory.
* This method is mainly used by [[gc()]].
* @param string $path the directory under which expired cache files are removed.
* @param boolean $expiredOnly whether to only remove expired cache files. If false, all files
* under `$path` will be removed.
*/
*/
p
ublic
function
gc
(
$expiredOnly
=
true
,
$path
=
null
)
p
rotected
function
gcRecursive
(
$path
,
$expiredOnly
)
{
{
if
(
$path
===
null
)
if
((
$handle
=
opendir
(
$path
))
!==
false
)
{
$path
=
$this
->
cachePath
;
while
((
$file
=
readdir
(
$handle
))
!==
false
)
{
if
((
$handle
=
opendir
(
$path
))
===
false
)
if
(
$file
[
0
]
===
'.'
)
{
return
;
continue
;
while
((
$file
=
readdir
(
$handle
))
!==
false
)
}
{
$fullPath
=
$path
.
DIRECTORY_SEPARATOR
.
$file
;
if
(
$file
[
0
]
===
'.'
)
if
(
is_dir
(
$fullPath
))
{
continue
;
$this
->
gcRecursive
(
$fullPath
,
$expiredOnly
);
$fullPath
=
$path
.
DIRECTORY_SEPARATOR
.
$file
;
if
(
!
$expiredOnly
)
{
if
(
is_dir
(
$fullPath
))
@
rmdir
(
$fullPath
);
$this
->
gc
(
$expiredOnly
,
$fullPath
);
}
else
if
(
$expiredOnly
&&
@
filemtime
(
$fullPath
)
<
time
()
||
!
$expiredOnly
)
}
elseif
(
!
$expiredOnly
||
$expiredOnly
&&
@
filemtime
(
$fullPath
)
<
time
())
{
@
unlink
(
$fullPath
);
@
unlink
(
$fullPath
);
}
}
closedir
(
$handle
);
}
}
closedir
(
$handle
);
}
}
}
}
framework/caching/MemCache.php
View file @
78623f3e
...
@@ -54,7 +54,7 @@ use yii\base\Exception;
...
@@ -54,7 +54,7 @@ use yii\base\Exception;
* In the above, two memcache servers are used: server1 and server2. You can configure more properties of
* In the above, two memcache servers are used: server1 and server2. You can configure more properties of
* each server, such as `persistent`, `weight`, `timeout`. Please see [[MemCacheServer]] for available options.
* each server, such as `persistent`, `weight`, `timeout`. Please see [[MemCacheServer]] for available options.
*
*
* @property
mix
ed $memCache The memcache instance (or memcached if [[useMemcached]] is true) used by this component.
* @property
\Memcache|\Memcach
ed $memCache The memcache instance (or memcached if [[useMemcached]] is true) used by this component.
* @property MemCacheServer[] $servers List of memcache server configurations.
* @property MemCacheServer[] $servers List of memcache server configurations.
*
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
...
...
framework/db/dao/Command.php
View file @
78623f3e
...
@@ -186,7 +186,9 @@ class Command extends \yii\base\Component
...
@@ -186,7 +186,9 @@ class Command extends \yii\base\Component
* Note that the SQL data type of each value is determined by its PHP type.
* Note that the SQL data type of each value is determined by its PHP type.
* @param array $values the values to be bound. This must be given in terms of an associative
* @param array $values the values to be bound. This must be given in terms of an associative
* array with array keys being the parameter names, and array values the corresponding parameter values,
* array with array keys being the parameter names, and array values the corresponding parameter values,
* e.g. `array(':name'=>'John', ':age'=>25)`.
* e.g. `array(':name'=>'John', ':age'=>25)`. By default, the PDO type of each value is determined
* by its PHP type. You may explicitly specify the PDO type by using an array: `array(value, type)`,
* e.g. `array(':name'=>'John', ':profile'=>array($profile, \PDO::PARAM_LOB))`.
* @return Command the current command being executed
* @return Command the current command being executed
*/
*/
public
function
bindValues
(
$values
)
public
function
bindValues
(
$values
)
...
@@ -194,7 +196,13 @@ class Command extends \yii\base\Component
...
@@ -194,7 +196,13 @@ class Command extends \yii\base\Component
if
(
!
empty
(
$values
))
{
if
(
!
empty
(
$values
))
{
$this
->
prepare
();
$this
->
prepare
();
foreach
(
$values
as
$name
=>
$value
)
{
foreach
(
$values
as
$name
=>
$value
)
{
$this
->
pdoStatement
->
bindValue
(
$name
,
$value
,
$this
->
connection
->
getPdoType
(
gettype
(
$value
)));
if
(
is_array
(
$value
))
{
$type
=
$value
[
1
];
$value
=
$value
[
0
];
}
else
{
$type
=
$this
->
connection
->
getPdoType
(
gettype
(
$value
));
}
$this
->
pdoStatement
->
bindValue
(
$name
,
$value
,
$type
);
$this
->
_params
[
$name
]
=
$value
;
$this
->
_params
[
$name
]
=
$value
;
}
}
}
}
...
...
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