mirror of
https://github.com/friendica/friendica
synced 2025-04-23 10:30:11 +00:00
Add support for Memcached/Improve database cache
- Create Cache Driver interface - Update cache table fields - Add CacheSessionHandler
This commit is contained in:
parent
7bd4a52156
commit
3628b62aeb
9 changed files with 326 additions and 192 deletions
56
src/Core/Cache/DatabaseCacheDriver.php
Normal file
56
src/Core/Cache/DatabaseCacheDriver.php
Normal file
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Cache;
|
||||
|
||||
use dba;
|
||||
use Friendica\Core\Cache;
|
||||
use Friendica\Database\DBM;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
|
||||
/**
|
||||
* Database Cache Driver
|
||||
*
|
||||
* @author Hypolite Petovan <mrpetovan@gmail.com>
|
||||
*/
|
||||
class DatabaseCacheDriver implements ICacheDriver
|
||||
{
|
||||
public function get($key)
|
||||
{
|
||||
$cache = dba::selectFirst('cache', ['v'], ['`k` = ? AND `expires` >= NOW()`', $key]);
|
||||
|
||||
if (DBM::is_result($cache)) {
|
||||
$cached = $cache['v'];
|
||||
$value = @unserialize($cached);
|
||||
|
||||
// Only return a value if the serialized value is valid.
|
||||
// We also check if the db entry is a serialized
|
||||
// boolean 'false' value (which we want to return).
|
||||
if ($cached === serialize(false) || $value !== false) {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function set($key, $value, $duration = Cache::MONTH)
|
||||
{
|
||||
$fields = [
|
||||
'v' => serialize($value),
|
||||
'expires' => DateTimeFormat::utc('now + ' . Cache::duration($duration) . ' seconds'),
|
||||
'updated' => DateTimeFormat::utcNow()
|
||||
];
|
||||
|
||||
return dba::update('cache', $fields, ['k' => $key], true);
|
||||
}
|
||||
|
||||
public function delete($key)
|
||||
{
|
||||
return dba::delete('cache', ['k' => $key]);
|
||||
}
|
||||
|
||||
public function clear()
|
||||
{
|
||||
return dba::delete('cache', ['`expires` < NOW()']);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue