1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://www.gnu.org/licenses/lgpl.html |
5
|
|
|
* @copyright Metaways Infosystems GmbH, 2014 |
6
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2020 |
7
|
|
|
* @package MAdmin |
8
|
|
|
* @subpackage Cache |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Aimeos\MAdmin\Cache\Manager; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Redis cache manager implementation. |
17
|
|
|
* |
18
|
|
|
* @package MAdmin |
19
|
|
|
* @subpackage Cache |
20
|
|
|
*/ |
21
|
|
|
class Redis |
22
|
|
|
extends \Aimeos\MAdmin\Common\Manager\Base |
23
|
|
|
implements \Aimeos\MAdmin\Cache\Manager\Iface |
24
|
|
|
{ |
25
|
|
|
private $object; |
26
|
|
|
private $searchConfig = array( |
27
|
|
|
'cache.id' => array( |
28
|
|
|
'code' => 'cache.id', |
29
|
|
|
'internalcode' => '"id"', |
30
|
|
|
'label' => 'Cache ID', |
31
|
|
|
'type' => 'string', |
32
|
|
|
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR, |
33
|
|
|
), |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Returns the cache object |
39
|
|
|
* |
40
|
|
|
* @return \Aimeos\MW\Cache\Iface Cache object |
41
|
|
|
*/ |
42
|
|
|
public function getCache() : \Aimeos\MW\Cache\Iface |
43
|
|
|
{ |
44
|
|
|
if( !isset( $this->object ) ) |
45
|
|
|
{ |
46
|
|
|
$context = $this->getContext(); |
47
|
|
|
$config = $context->getConfig(); |
48
|
|
|
|
49
|
|
|
$conn = $config->get( 'resource/cache/redis/connection' ); |
50
|
|
|
$conf = $config->get( 'resource/cache/redis', [] ); |
51
|
|
|
|
52
|
|
|
if( !class_exists( '\\Predis\\Client' ) ) { |
53
|
|
|
throw new \Aimeos\MAdmin\Cache\Exception( sprintf( 'Please install "%1$s" via composer first', 'predis/predis' ) ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$client = new \Predis\Client( $conn, $conf ); |
57
|
|
|
$conf = array( 'siteid' => $context->getLocale()->getSiteId() ); |
58
|
|
|
|
59
|
|
|
$this->object = \Aimeos\MW\Cache\Factory::create( 'Redis', $conf, $client ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->object; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Creates a new empty item instance |
68
|
|
|
* |
69
|
|
|
* @param array $values Values the item should be initialized with |
70
|
|
|
* @return \Aimeos\MShop\Common\Item\Iface New item object |
71
|
|
|
*/ |
72
|
|
|
public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface |
73
|
|
|
{ |
74
|
|
|
return $this->createItemBase( $values ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Adds a new cache to the storage. |
80
|
|
|
* |
81
|
|
|
* @param \Aimeos\MAdmin\Cache\Item\Iface $item Cache item that should be saved to the storage |
82
|
|
|
* @param bool $fetch True if the new ID should be returned in the item |
83
|
|
|
* @return \Aimeos\MAdmin\Cache\Item\Iface Cache item |
84
|
|
|
*/ |
85
|
|
|
public function saveItem( \Aimeos\MAdmin\Cache\Item\Iface $item, bool $fetch = true ) : \Aimeos\MAdmin\Cache\Item\Iface |
86
|
|
|
{ |
87
|
|
|
if( $item->getId() === null ) { |
88
|
|
|
throw new \Aimeos\MAdmin\Cache\Exception( 'ID is required for caching' ); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if( $item->isModified() ) |
92
|
|
|
{ |
93
|
|
|
$cache = $this->getCache(); |
94
|
|
|
$cache->delete( $item->getId() ); |
95
|
|
|
$cache->set( $item->getId(), $item->getValue(), $item->getTimeExpire(), $item->getTags() ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $item; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Removes multiple items. |
104
|
|
|
* |
105
|
|
|
* @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items |
106
|
|
|
* @return \Aimeos\MAdmin\Cache\Manager\Iface Manager object for chaining method calls |
107
|
|
|
*/ |
108
|
|
|
public function deleteItems( array $itemIds ) : \Aimeos\MShop\Common\Manager\Iface |
109
|
|
|
{ |
110
|
|
|
$this->getCache()->deleteMultiple( $itemIds ); |
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Creates the cache object for the given cache id. |
117
|
|
|
* |
118
|
|
|
* @param string $id Cache ID to fetch cache object for |
119
|
|
|
* @param array $ref List of domains to fetch list items and referenced items for |
120
|
|
|
* @param bool $default Add default criteria |
121
|
|
|
* @return \Aimeos\MAdmin\Cache\Item\Iface Returns the cache item of the given id |
122
|
|
|
* @throws \Aimeos\MAdmin\Cache\Exception If item couldn't be found |
123
|
|
|
*/ |
124
|
|
|
public function get( string $id, array $ref = [], bool $default = false ) : \Aimeos\MShop\Common\Item\Iface |
125
|
|
|
{ |
126
|
|
|
if( ( $value = $this->getCache()->get( $id ) ) === null ) { |
127
|
|
|
throw new \Aimeos\MAdmin\Cache\Exception( sprintf( 'Item with ID "%1$s" not found', $id ) ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $this->createItemBase( array( 'id' => $id, 'value' => $value ) ); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Search for cache entries based on the given criteria. |
136
|
|
|
* |
137
|
|
|
* @param \Aimeos\MW\Criteria\Iface $search Search object containing the conditions |
138
|
|
|
* @param string[] $ref List of domains to fetch list items and referenced items for |
139
|
|
|
* @param int &$total Number of items that are available in total |
140
|
|
|
* @return \Aimeos\Map List of cache items implementing \Aimeos\MAdmin\Cache\Item\Iface |
141
|
|
|
*/ |
142
|
|
|
public function search( \Aimeos\MW\Criteria\Iface $search, array $ref = [], int &$total = null ) : \Aimeos\Map |
143
|
|
|
{ |
144
|
|
|
/** Not available in a reasonable implemented way by Redis */ |
145
|
|
|
return map(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Returns the available manager types |
151
|
|
|
* |
152
|
|
|
* @param bool $withsub Return also the resource type of sub-managers if true |
153
|
|
|
* @return array Type of the manager and submanagers, subtypes are separated by slashes |
154
|
|
|
*/ |
155
|
|
|
public function getResourceType( bool $withsub = true ) : array |
156
|
|
|
{ |
157
|
|
|
$path = 'madmin/cache/manager/submanagers'; |
158
|
|
|
|
159
|
|
|
return $this->getResourceTypeBase( 'cache', $path, [], $withsub ); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Returns the attributes that can be used for searching. |
165
|
|
|
* |
166
|
|
|
* @param bool $withsub Return also attributes of sub-managers if true |
167
|
|
|
* @return array Returns a list of attribtes implementing \Aimeos\MW\Criteria\Attribute\Iface |
168
|
|
|
*/ |
169
|
|
|
public function getSearchAttributes( bool $withsub = true ) : array |
170
|
|
|
{ |
171
|
|
|
$path = 'madmin/cache/manager/submanagers'; |
172
|
|
|
|
173
|
|
|
return $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub ); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Returns a new manager for cache extensions |
179
|
|
|
* |
180
|
|
|
* @param string $manager Name of the sub manager type in lower case |
181
|
|
|
* @param string|null $name Name of the implementation, will be from configuration (or Default) if null |
182
|
|
|
* @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g stock, tags, locations, etc. |
183
|
|
|
*/ |
184
|
|
|
public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface |
185
|
|
|
{ |
186
|
|
|
return $this->getSubManagerBase( 'cache', $manager, $name ); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Create new admin cache item object initialized with given parameters. |
192
|
|
|
* |
193
|
|
|
* @param array $values Associative list of key/value pairs of a job |
194
|
|
|
* @return \Aimeos\MAdmin\Cache\Item\Iface |
195
|
|
|
*/ |
196
|
|
|
protected function createItemBase( array $values = [] ) : \Aimeos\MAdmin\Cache\Item\Iface |
197
|
|
|
{ |
198
|
|
|
$values['siteid'] = $this->getContext()->getLocale()->getSiteId(); |
199
|
|
|
|
200
|
|
|
return new \Aimeos\MAdmin\Cache\Item\Standard( $values ); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|