2019-01-30 14:26:17 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Test\Util;
|
|
|
|
|
2020-01-19 10:29:55 -05:00
|
|
|
use Friendica\Core\Cache\Duration;
|
|
|
|
|
2019-01-30 14:26:17 -05:00
|
|
|
trait DbaCacheMockTrait
|
|
|
|
{
|
2019-08-03 14:48:56 -04:00
|
|
|
/**
|
|
|
|
* @var
|
|
|
|
*/
|
|
|
|
protected $dba;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
2019-01-30 14:26:17 -05:00
|
|
|
|
|
|
|
protected function mockDelete($key, $return = true, $times = null)
|
|
|
|
{
|
|
|
|
$this->mockDBADelete('cache', ['k' => $key], $return, $times);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function mockGet($key, $return = null, $time = null, $times = null)
|
|
|
|
{
|
|
|
|
if ($time === null) {
|
|
|
|
$time = time();
|
|
|
|
}
|
|
|
|
|
|
|
|
$value = @serialize($return);
|
|
|
|
|
|
|
|
$this->mockSelectFirst('cache', ['v'], ['`k` = ? AND (`expires` >= ? OR `expires` = -1)', $key, $time], ['v' => $value], $times);
|
|
|
|
$this->mockIsResult(['v' => $value], isset($return), $times);
|
|
|
|
}
|
|
|
|
|
2020-01-19 10:29:55 -05:00
|
|
|
protected function mockSet($key, $value, $ttl = Duration::FIVE_MINUTES, $time = null, $return = true, $times = null)
|
2019-01-30 14:26:17 -05:00
|
|
|
{
|
|
|
|
if ($time === null) {
|
|
|
|
$time = time();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($ttl > 0) {
|
|
|
|
$this->mockUtc('now + ' . $ttl . 'seconds', $time + $ttl, $times);
|
|
|
|
$fields = [
|
|
|
|
'v' => serialize($value),
|
|
|
|
'expires' => $time + $ttl,
|
|
|
|
'updated' => $time
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$fields = [
|
|
|
|
'v' => serialize($value),
|
|
|
|
'expires' => -1,
|
|
|
|
'updated' => $time
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->mockDBAUpdate('cache', $fields, ['k' => $key], true, $return, $times);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function mockClear($outdated = true, $return = true, $times = null)
|
|
|
|
{
|
|
|
|
if ($outdated) {
|
|
|
|
$this->mockDBADelete('cache', ['`expires` < NOW()'], $return, $times);
|
|
|
|
} else {
|
|
|
|
$this->mockDBADelete('cache', ['`k` IS NOT NULL '], $return, $times);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function mockGetAllKeys($prefix = null, $return = [], $time = null, $times = null)
|
|
|
|
{
|
|
|
|
if ($time === null) {
|
|
|
|
$time = time();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($prefix)) {
|
|
|
|
$where = ['`expires` >= ?', $time];
|
|
|
|
} else {
|
|
|
|
$where = ['`expires` >= ? AND `k` LIKE CONCAT(?, \'%\')', $time, $prefix];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->mockSelect('cache', ['k'], $where, $return, $times);
|
|
|
|
$this->mockFetchLoop($return, $times);
|
|
|
|
$this->mockDbaClose(true, $times);
|
|
|
|
}
|
|
|
|
}
|