|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Metaways Infosystems GmbH, 2014 |
|
6
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2016 |
|
7
|
|
|
* @package MW |
|
8
|
|
|
* @subpackage Cache |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace Aimeos\MW\Cache; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* MySQL database cache class. |
|
17
|
|
|
* |
|
18
|
|
|
* @package MW |
|
19
|
|
|
* @subpackage Cache |
|
20
|
|
|
*/ |
|
21
|
|
|
class Mysql |
|
22
|
|
|
extends \Aimeos\MW\Cache\DB |
|
|
|
|
|
|
23
|
|
|
implements \Aimeos\MW\Cache\Iface |
|
24
|
|
|
{ |
|
25
|
|
|
private $sql; |
|
26
|
|
|
private $dbm; |
|
27
|
|
|
private $dbname; |
|
28
|
|
|
private $siteid; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Initializes the object instance. |
|
33
|
|
|
* |
|
34
|
|
|
* The config['search] array must contain these key/array pairs suitable for \Aimeos\MW\Criteria\Attribute\Standard: |
|
35
|
|
|
* [cache.id] => Array containing the codes/types/labels for the unique ID |
|
36
|
|
|
* [cache.siteid] => Array containing the codes/types/labels for the site ID |
|
37
|
|
|
* [cache.value] => Array containing the codes/types/labels for the cached value |
|
38
|
|
|
* [cache.expire] => Array containing the codes/types/labels for the expiration date |
|
39
|
|
|
* [cache.tag.name] => Array containing the codes/types/labels for the tag name |
|
40
|
|
|
* |
|
41
|
|
|
* The config['sql] array must contain these statement: |
|
42
|
|
|
* [delete] => |
|
43
|
|
|
* DELETE FROM cachetable WHERE siteid = ? AND :cond |
|
44
|
|
|
* [deletebytag] => |
|
45
|
|
|
* DELETE FROM cachetable WHERE siteid = ? AND id IN ( |
|
46
|
|
|
* SELECT tid FROM cachetagtable WHERE tsiteid = ? AND :cond |
|
47
|
|
|
* ) |
|
48
|
|
|
* [get] => |
|
49
|
|
|
* SELECT id, value, expire FROM cachetable WHERE siteid = ? AND :cond |
|
50
|
|
|
* [getbytag] => |
|
51
|
|
|
* SELECT id, value, expire FROM cachetable |
|
52
|
|
|
* JOIN cachetagtable ON tid = id |
|
53
|
|
|
* WHERE siteid = ? AND tsiteid = ? AND :cond |
|
54
|
|
|
* [set] => |
|
55
|
|
|
* INSERT INTO cachetable ( id, siteid, expire, value ) VALUES ( ?, ?, ?, ? ) ON DUPLICATE KEY UPDATE |
|
56
|
|
|
* [settag] => |
|
57
|
|
|
* INSERT INTO cachetagtable ( tid, tsiteid, tname ) VALUES :tuples ON DUPLICATE KEY UPDATE |
|
58
|
|
|
* |
|
59
|
|
|
* For using a different database connection, the name of the database connection |
|
60
|
|
|
* can be also given in the "config" parameter. In this case, use e.g. |
|
61
|
|
|
* config['dbname'] = 'db-cache' |
|
62
|
|
|
* |
|
63
|
|
|
* If a site ID is given, the cache is partitioned for different |
|
64
|
|
|
* sites. This also includes access control so cached values can be only |
|
65
|
|
|
* retrieved from the same site. Specify a site ID with |
|
66
|
|
|
* config['siteid'] = 123 |
|
67
|
|
|
* |
|
68
|
|
|
* @param array $config Associative list with SQL statements, search attribute definitions and database name |
|
69
|
|
|
* @param \Aimeos\MW\DB\Manager\Iface $dbm Database manager |
|
70
|
|
|
*/ |
|
71
|
|
|
public function __construct( array $config, \Aimeos\MW\DB\Manager\Iface $dbm ) |
|
72
|
|
|
{ |
|
73
|
|
|
parent::__construct( $config, $dbm ); |
|
74
|
|
|
|
|
75
|
|
|
$this->dbname = ( isset( $config['dbname'] ) ? $config['dbname'] : 'db' ); |
|
76
|
|
|
$this->siteid = ( isset( $config['siteid'] ) ? $config['siteid'] : null ); |
|
77
|
|
|
$this->sql = $config['sql']; |
|
78
|
|
|
$this->dbm = $dbm; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Adds or overwrites the given key/value pairs in the cache, which is much |
|
84
|
|
|
* more efficient than setting them one by one using the set() method. |
|
85
|
|
|
* |
|
86
|
|
|
* @param iterable $pairs Associative list of key/value pairs. Both must be |
|
87
|
|
|
* a string |
|
88
|
|
|
* @param int|string|array $expires Associative list of keys and datetime |
|
|
|
|
|
|
89
|
|
|
* string or integer TTL pairs. |
|
90
|
|
|
* @param array $tags Associative list of key/tag or key/tags pairs that |
|
91
|
|
|
* should be associated to the values identified by their key. The value |
|
92
|
|
|
* associated to the key can either be a tag string or an array of tag strings |
|
93
|
|
|
* @return null |
|
94
|
|
|
* @throws \Aimeos\MW\Cache\Exception If the cache server doesn't respond |
|
95
|
|
|
*/ |
|
96
|
|
|
public function setMultiple( $pairs, $expires = null, array $tags = array() ) |
|
97
|
|
|
{ |
|
98
|
|
|
$type = ( count( $pairs ) > 1 ? \Aimeos\MW\DB\Connection\Base::TYPE_PREP : \Aimeos\MW\DB\Connection\Base::TYPE_SIMPLE ); |
|
99
|
|
|
$conn = $this->dbm->acquire( $this->dbname ); |
|
100
|
|
|
|
|
101
|
|
|
try |
|
102
|
|
|
{ |
|
103
|
|
|
$conn->begin(); |
|
104
|
|
|
$stmt = $conn->create( $this->sql['set'], $type ); |
|
105
|
|
|
|
|
106
|
|
|
foreach( $pairs as $key => $value ) |
|
107
|
|
|
{ |
|
108
|
|
|
$date = ( isset( $expires[$key] ) ? $expires[$key] : null ); |
|
109
|
|
|
|
|
110
|
|
|
$stmt->bind( 1, $key ); |
|
111
|
|
|
$stmt->bind( 2, $this->siteid, \Aimeos\MW\DB\Statement\Base::PARAM_INT ); |
|
112
|
|
|
$stmt->bind( 3, $date ); |
|
113
|
|
|
$stmt->bind( 4, $value ); |
|
114
|
|
|
$stmt->execute()->finish(); |
|
115
|
|
|
|
|
116
|
|
|
if( isset( $tags[$key] ) ) |
|
117
|
|
|
{ |
|
118
|
|
|
$parts = array(); |
|
119
|
|
|
$stmtTagPart = $conn->create( '( ?, ?, ? )' ); |
|
120
|
|
|
|
|
121
|
|
|
foreach( (array) $tags[$key] as $name ) |
|
122
|
|
|
{ |
|
123
|
|
|
$stmtTagPart->bind( 1, $key ); |
|
124
|
|
|
$stmtTagPart->bind( 2, $this->siteid, \Aimeos\MW\DB\Statement\Base::PARAM_INT ); |
|
125
|
|
|
$stmtTagPart->bind( 3, $name ); |
|
126
|
|
|
|
|
127
|
|
|
$parts[] = (string) $stmtTagPart; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
if( !empty ( $parts ) ) |
|
131
|
|
|
{ |
|
132
|
|
|
$stmtTag = $conn->create( str_replace( ':tuples', join( ',', $parts ), $this->sql['settag'] ) ); |
|
133
|
|
|
$stmtTag->execute()->finish(); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$conn->commit(); |
|
139
|
|
|
$this->dbm->release( $conn, $this->dbname ); |
|
140
|
|
|
} |
|
141
|
|
|
catch( \Exception $e ) |
|
142
|
|
|
{ |
|
143
|
|
|
$conn->rollback(); |
|
144
|
|
|
$this->dbm->release( $conn, $this->dbname ); |
|
145
|
|
|
throw $e; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|