43 lines
972 B
PHP
43 lines
972 B
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
class QueryCacheModel extends Model
|
|
{
|
|
protected $table = 'query_cache';
|
|
protected $primaryKey = 'id';
|
|
protected $useAutoIncrement = true;
|
|
protected $returnType = 'array';
|
|
protected $protectFields = true;
|
|
|
|
protected $allowedFields = [
|
|
'cache_key',
|
|
'data_source_id',
|
|
'result_data',
|
|
'row_count',
|
|
'expires_at',
|
|
];
|
|
|
|
protected bool $allowEmptyInserts = false;
|
|
protected bool $updateOnlyChanged = true;
|
|
|
|
protected $useTimestamps = false;
|
|
|
|
public function findValid(string $cacheKey): ?array
|
|
{
|
|
$row = $this->where('cache_key', $cacheKey)->first();
|
|
if (! is_array($row)) {
|
|
return null;
|
|
}
|
|
|
|
$expiresAt = strtotime((string) ($row['expires_at'] ?? '1970-01-01 00:00:00'));
|
|
if ($expiresAt < time()) {
|
|
return null;
|
|
}
|
|
|
|
return $row;
|
|
}
|
|
}
|