banker/src/Driver/AbstractDriver.php

84 lines
1.8 KiB
PHP
Raw Normal View History

2016-10-19 09:57:06 -04:00
<?php declare(strict_types=1);
2016-08-31 12:18:46 -04:00
/**
2016-09-05 16:43:37 -04:00
* Banker
2016-08-31 12:18:46 -04:00
*
* A Caching library implementing psr/cache (PSR 6) and psr/simple-cache (PSR 16)
2016-08-31 12:18:46 -04:00
*
* PHP version 7.4
2016-08-31 12:18:46 -04:00
*
2016-10-19 09:57:06 -04:00
* @package Banker
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2016 - 2020 Timothy J. Warren
2016-10-19 09:57:06 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2020-05-08 12:30:43 -04:00
* @version 3.1.0
2016-10-19 09:57:06 -04:00
* @link https://git.timshomepage.net/timw4mail/banker
2016-08-31 12:18:46 -04:00
*/
namespace Aviat\Banker\Driver;
2017-03-01 13:04:00 -05:00
use Aviat\Banker\LoggerTrait;
2016-09-06 17:03:43 -04:00
use Psr\Log\LoggerAwareInterface;
2016-08-31 12:18:46 -04:00
/**
* Base class for cache backends
*/
2016-09-06 17:03:43 -04:00
abstract class AbstractDriver implements DriverInterface, LoggerAwareInterface {
2016-09-06 20:57:24 -04:00
2017-03-01 13:04:00 -05:00
use LoggerTrait;
2016-09-05 16:43:37 -04:00
/**
* Data to be stored later
*
* @var array
*/
2020-05-07 17:17:03 -04:00
protected array $deferred = [];
2016-09-05 16:43:37 -04:00
/**
* Common constructor interface for driver classes
*
* @param array $config - Connection parameters for the specified backend
2016-09-06 20:57:24 -04:00
* @param array $options - Special connection options for the specified backend
2016-09-05 16:43:37 -04:00
*/
2016-09-06 20:57:24 -04:00
abstract public function __construct(array $config = [], array $options = []);
/**
* Retrieve a set of values by their cache key
*
* @param string[] $keys
* @return array
*/
public function getMultiple(array $keys = []): array
{
$output = [];
foreach ($keys as $key)
{
if ($this->exists($key))
{
$output[$key] = $this->get($key);
}
}
return $output;
}
/**
* Set multiple cache values
*
* @param array $items
* @param int|null $expires
* @return bool
*/
public function setMultiple(array $items, ?int $expires = NULL): bool
{
$setResults = [];
foreach ($items as $k => $v)
{
2020-05-08 18:58:25 -04:00
$setResults[] = ($expires === NULL)
? $this->set($k, $v)
: $this->set($k, $v, $expires);
}
// Only return true if all the results are true
return array_reduce($setResults, fn ($carry, $item) => $item && $carry, TRUE);
}
2016-08-31 12:18:46 -04:00
}