PHP Class yii\caching\DbCache
By default, DbCache stores session data in a DB table named 'cache'. This table
must be pre-created. The table name can be changed by setting [[cacheTable]].
Please refer to
Cache for common cache operations that are supported by DbCache.
The following example shows how you can configure the application to use DbCache:
php
'cache' => [
'class' => 'yii\caching\DbCache',
'db' => 'mydb',
'cacheTable' => 'my_cache',
]
For more details and usage information on Cache, see the
guide article on caching.
Exibir arquivo
Open project: yiisoft/yii2
Class Usage Examples
Public Properties
Property |
Type |
Description |
|
$cacheTable |
|
name of the DB table to store cache content.
The table should be pre-created as follows:
php
CREATE TABLE cache (
id char(128) NOT NULL PRIMARY KEY,
expire int(11),
data BLOB
);
where 'BLOB' refers to the BLOB-type of your preferred DBMS. Below are the BLOB type
that can be used for some popular DBMS:
- MySQL: LONGBLOB
- PostgreSQL: BYTEA
- MSSQL: BLOB
When using DbCache in a production server, we recommend you create a DB index for the 'expire'
column in the cache table to improve the performance. |
|
$db |
|
the DB connection object or the application component ID of the DB connection.
After the DbCache object is created, if you want to change this property, you should only assign it
with a DB connection object.
Starting from version 2.0.2, this can also be a configuration array for creating the object. |
|
$gcProbability |
|
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.
This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all. |
|
Public Methods
Method |
Description |
|
exists ( mixed $key ) : boolean |
Checks whether a specified key exists in the cache. |
|
gc ( boolean $force = false ) |
Removes the expired data values. |
|
init ( ) |
Initializes the DbCache component. |
|
Protected Methods
Method Details
addValue()
protected method
This is the implementation of the method declared in the parent class.
protected addValue ( string $key, string $value, integer $duration ) : boolean |
$key |
string |
the key identifying the value to be cached |
$value |
string |
the value to be cached. Other types (if you have disabled [[serializer]]) cannot be saved. |
$duration |
integer |
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 |
deleteValue()
protected method
Deletes a value with the specified key from cache
This is the implementation of the method declared in the parent class.
This can be faster than getting the value from the cache if the data is big.
Note that this method does not check whether the dependency associated
with the cached data, if there is any, has changed. So a call to [[get]]
may return false while exists returns true.
public exists ( mixed $key ) : boolean |
$key |
mixed |
a key identifying the cached value. This can be a simple string or
a complex data structure consisting of factors representing the key. |
return |
boolean |
true if a value exists in cache, false if the value is not in the cache or expired. |
flushValues()
protected method
This is the implementation of the method declared in the parent class.
Removes the expired data values.
public gc ( boolean $force = false ) |
$force |
boolean |
whether to enforce the garbage collection regardless of [[gcProbability]].
Defaults to false, meaning the actual deletion happens with the probability as specified by [[gcProbability]]. |
getValue()
protected method
This is the implementation of the method declared in the parent class.
protected getValue ( string $key ) : string | false |
$key |
string |
a unique key identifying the cached value |
return |
string | false |
the value stored in cache, false if the value is not in the cache or expired. |
getValues()
protected method
Retrieves multiple values from cache with the specified keys.
protected getValues ( array $keys ) : array |
$keys |
array |
a list of keys identifying the cached values |
return |
array |
a list of cached values indexed by the keys |
This method will initialize the [[db]] property to make sure it refers to a valid DB connection.
setValue()
protected method
This is the implementation of the method declared in the parent class.
protected setValue ( string $key, string $value, integer $duration ) : boolean |
$key |
string |
the key identifying the value to be cached |
$value |
string |
the value to be cached. Other types (if you have disabled [[serializer]]) cannot be saved. |
$duration |
integer |
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 |
Property Details
$cacheTable public_oe property
name of the DB table to store cache content.
The table should be pre-created as follows:
php
CREATE TABLE cache (
id char(128) NOT NULL PRIMARY KEY,
expire int(11),
data BLOB
);
where 'BLOB' refers to the BLOB-type of your preferred DBMS. Below are the BLOB type
that can be used for some popular DBMS:
- MySQL: LONGBLOB
- PostgreSQL: BYTEA
- MSSQL: BLOB
When using DbCache in a production server, we recommend you create a DB index for the 'expire'
column in the cache table to improve the performance.
the DB connection object or the application component ID of the DB connection.
After the DbCache object is created, if you want to change this property, you should only assign it
with a DB connection object.
Starting from version 2.0.2, this can also be a configuration array for creating the object.
$gcProbability public_oe property
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.
This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all.