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