banker/src/Driver/AbstractDriver.php

87 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
*
2021-02-05 17:05:56 -05: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>
2021-02-05 17:05:56 -05:00
* @copyright 2016 - 2021 Timothy J. Warren
2016-10-19 09:57:06 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2021-02-05 17:05:56 -05:00
* @version 3.2.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;
use Aviat\Banker\KeyValidateTrait;
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
use KeyValidateTrait;
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
*/
abstract public function __construct();
2016-09-06 20:57:24 -04:00
/**
* Retrieve a set of values by their cache key
*
* @param string[] $keys
* @return array
*/
public function getMultiple(array $keys = []): array
{
$this->validateKeys($keys);
$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
{
$this->validateKeys($items, TRUE);
$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
}