Merge pull request #4549 from MrPetovan/task/4518-add-memcached-support

[develop] Performance: Add memcached support/Improve database cache (redux)
This commit is contained in:
Michael Vogel 2018-03-06 16:34:28 +01:00 committed by GitHub
commit cc688e2b4f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 439 additions and 223 deletions

View file

@ -1,6 +1,6 @@
<?php <?php
/** /**
* @file_tag_unsave_file boot.php * @file boot.php
* This file defines some global constants and includes the central App class. * This file defines some global constants and includes the central App class.
*/ */
@ -21,6 +21,7 @@ require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'a
use Friendica\App; use Friendica\App;
use Friendica\Core\Addon; use Friendica\Core\Addon;
use Friendica\Core\Cache;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\PConfig; use Friendica\Core\PConfig;
@ -39,7 +40,7 @@ define('FRIENDICA_PLATFORM', 'Friendica');
define('FRIENDICA_CODENAME', 'Asparagus'); define('FRIENDICA_CODENAME', 'Asparagus');
define('FRIENDICA_VERSION', '3.6-dev'); define('FRIENDICA_VERSION', '3.6-dev');
define('DFRN_PROTOCOL_VERSION', '2.23'); define('DFRN_PROTOCOL_VERSION', '2.23');
define('DB_UPDATE_VERSION', 1255); define('DB_UPDATE_VERSION', 1256);
define('NEW_UPDATE_ROUTINE_VERSION', 1170); define('NEW_UPDATE_ROUTINE_VERSION', 1170);
/** /**
@ -121,18 +122,20 @@ define('LOGGER_ALL', 4);
/** /**
* @name Cache * @name Cache
* @deprecated since version 3.6
* @see Cache
* *
* Cache levels * Cache levels
* @{ * @{
*/ */
define('CACHE_MONTH', 0); define('CACHE_MONTH', Cache::MONTH);
define('CACHE_WEEK', 1); define('CACHE_WEEK', Cache::WEEK);
define('CACHE_DAY', 2); define('CACHE_DAY', Cache::DAY);
define('CACHE_HOUR', 3); define('CACHE_HOUR', Cache::HOUR);
define('CACHE_HALF_HOUR', 4); define('CACHE_HALF_HOUR', Cache::HALF_HOUR);
define('CACHE_QUARTER_HOUR', 5); define('CACHE_QUARTER_HOUR', Cache::QUARTER_HOUR);
define('CACHE_FIVE_MINUTES', 6); define('CACHE_FIVE_MINUTES', Cache::FIVE_MINUTES);
define('CACHE_MINUTE', 7); define('CACHE_MINUTE', Cache::MINUTE);
/* @}*/ /* @}*/
/** /**

View file

@ -1,6 +1,6 @@
-- ------------------------------------------ -- ------------------------------------------
-- Friendica 3.6-dev (Asparagus) -- Friendica 3.6-dev (Asparagus)
-- DB_UPDATE_VERSION 1255 -- DB_UPDATE_VERSION 1256
-- ------------------------------------------ -- ------------------------------------------
@ -55,12 +55,12 @@ CREATE TABLE IF NOT EXISTS `auth_codes` (
-- TABLE cache -- TABLE cache
-- --
CREATE TABLE IF NOT EXISTS `cache` ( CREATE TABLE IF NOT EXISTS `cache` (
`k` varbinary(255) NOT NULL COMMENT '', `k` varbinary(255) NOT NULL COMMENT 'cache key',
`v` mediumtext COMMENT '', `v` mediumtext COMMENT 'cached serialized value',
`expire_mode` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '', `expires` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT 'datetime of cache expiration',
`updated` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '', `updated` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT 'datetime of cache insertion',
PRIMARY KEY(`k`), PRIMARY KEY(`k`),
INDEX `expire_mode_updated` (`expire_mode`,`updated`) INDEX `k_expires` (`k`,`expires`)
) DEFAULT COLLATE utf8mb4_general_ci; ) DEFAULT COLLATE utf8mb4_general_ci;
-- --

View file

@ -1,11 +1,13 @@
Table cache Table cache
=========== ===========
Stores temporary data
| Field | Description | Type | Null | Key | Default | Extra | | Field | Description | Type | Null | Key | Default | Extra |
| ------------ | ---------------------------------- | ------------ | ---- | --- | ------------------- | ----- | | ------------ | ---------------------------------- | ------------ | ---- | --- | ------------------- | ----- |
| k | horizontal width + url or resource | varchar(255) | NO | PRI | NULL | | | k | cache key | varchar(255) | NO | PRI | NULL | |
| v | OEmbed response from site | text | NO | | NULL | | | v | cached serialized value | text | NO | | NULL | |
| expires | datetime of cache expiration | datetime | NO | MUL | 0001-01-01 00:00:00 | |
| updated | datetime of cache insertion | datetime | NO | MUL | 0001-01-01 00:00:00 | | | updated | datetime of cache insertion | datetime | NO | MUL | 0001-01-01 00:00:00 | |
| expire_mode | | int(11) | NO | | 0 | |
Return to [database documentation](help/database) Return to [database documentation](help/database)

View file

@ -58,9 +58,11 @@ Example: To set the automatic database cleanup process add this line to your .ht
* **max_processes_backend** - Maximum number of concurrent database processes for background tasks. Default value is 5. * **max_processes_backend** - Maximum number of concurrent database processes for background tasks. Default value is 5.
* **max_processes_frontend** - Maximum number of concurrent database processes for foreground tasks. Default value is 20. * **max_processes_frontend** - Maximum number of concurrent database processes for foreground tasks. Default value is 20.
* **min_poll_interval** - minimal distance in minutes between two polls for a contact. Default is 1. Reasonable values are between 1 and 59. * **min_poll_interval** - minimal distance in minutes between two polls for a contact. Default is 1. Reasonable values are between 1 and 59.
* **memcache** (Boolean) - Use memcache. To use memcache the PECL extension "memcache" has to be installed and activated. * **session_handler** (database|cache|native) - Whether to use Cache to store session data or to use PHP native session storage. Default value is `database`.
* **memcache_host** - Hostname of the memcache daemon. Default is '127.0.0.1'. * **cache_driver** (database|memcache|memcached) - Whether to use Memcache or Memcached to store temporary cache. Default value is `database`.
* **memcache_port** - Portnumber of the memcache daemon. Default is 11211. * **memcache_host** - Host name of the memcache daemon. Default is '127.0.0.1'.
* **memcache_port** - Port number of the memcache daemon. Default is 11211.
* **memcached_hosts** - Array of Memcached servers info `[host, port(, weight)]`. Default value is `[['127.0.0.1', 11211]]`.
* **no_count** (Boolean) - Don't do count calculations (currently only when showing albums) * **no_count** (Boolean) - Don't do count calculations (currently only when showing albums)
* **no_oembed** (Boolean) - Don't use OEmbed to fetch more information about a link. * **no_oembed** (Boolean) - Don't use OEmbed to fetch more information about a link.
* **no_smilies** (Boolean) - Don't show smilies. * **no_smilies** (Boolean) - Don't show smilies.

File diff suppressed because one or more lines are too long

View file

@ -317,12 +317,16 @@ function api_call(App $a)
/// @TODO round() really everywhere? /// @TODO round() really everywhere?
logger( logger(
parse_url($a->query_string, PHP_URL_PATH) . ": " . sprintf( parse_url($a->query_string, PHP_URL_PATH) . ": " . sprintf(
"Database: %s/%s, Network: %s, I/O: %s, Other: %s, Total: %s", "Database: %s/%s, Cache %s/%s, Network: %s, I/O: %s, Other: %s, Total: %s",
round($a->performance["database"] - $a->performance["database_write"], 3), round($a->performance["database"] - $a->performance["database_write"], 3),
round($a->performance["database_write"], 3), round($a->performance["database_write"], 3),
round($a->performance["cache"], 3),
round($a->performance["cache_write"], 3),
round($a->performance["network"], 2), round($a->performance["network"], 2),
round($a->performance["file"], 2), round($a->performance["file"], 2),
round($duration - ($a->performance["database"] + $a->performance["network"] + $a->performance["file"]), 2), round($duration - ($a->performance["database"]
+ $a->performance["cache"] + $a->performance["cache_write"]
+ $a->performance["network"] + $a->performance["file"]), 2),
round($duration, 2) round($duration, 2)
), ),
LOGGER_DEBUG LOGGER_DEBUG
@ -344,6 +348,21 @@ function api_call(App $a)
} }
} }
$o = "Cache Read:\n";
foreach ($a->callstack["cache"] as $func => $time) {
$time = round($time, 3);
if ($time > 0) {
$o .= $func . ": " . $time . "\n";
}
}
$o .= "\nCache Write:\n";
foreach ($a->callstack["cache_write"] as $func => $time) {
$time = round($time, 3);
if ($time > 0) {
$o .= $func . ": " . $time . "\n";
}
}
$o .= "\nNetwork:\n"; $o .= "\nNetwork:\n";
foreach ($a->callstack["network"] as $func => $time) { foreach ($a->callstack["network"] as $func => $time) {
$time = round($time, 3); $time = round($time, 3);

View file

@ -148,6 +148,8 @@ class App
$this->performance['start'] = microtime(true); $this->performance['start'] = microtime(true);
$this->performance['database'] = 0; $this->performance['database'] = 0;
$this->performance['database_write'] = 0; $this->performance['database_write'] = 0;
$this->performance['cache'] = 0;
$this->performance['cache_write'] = 0;
$this->performance['network'] = 0; $this->performance['network'] = 0;
$this->performance['file'] = 0; $this->performance['file'] = 0;
$this->performance['rendering'] = 0; $this->performance['rendering'] = 0;
@ -157,6 +159,8 @@ class App
$this->callstack['database'] = []; $this->callstack['database'] = [];
$this->callstack['database_write'] = []; $this->callstack['database_write'] = [];
$this->callstack['cache'] = [];
$this->callstack['cache_write'] = [];
$this->callstack['network'] = []; $this->callstack['network'] = [];
$this->callstack['file'] = []; $this->callstack['file'] = [];
$this->callstack['rendering'] = []; $this->callstack['rendering'] = [];

View file

@ -4,44 +4,45 @@
*/ */
namespace Friendica\Core; namespace Friendica\Core;
use Friendica\Core\Cache;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Database\DBM;
use Friendica\Util\DateTimeFormat;
use dba;
use Memcache;
require_once 'include/dba.php';
/** /**
* @brief Class for storing data for a short time * @brief Class for storing data for a short time
*/ */
class Cache class Cache extends \Friendica\BaseObject
{ {
const MONTH = 0;
const WEEK = 1;
const DAY = 2;
const HOUR = 3;
const HALF_HOUR = 4;
const QUARTER_HOUR = 5;
const FIVE_MINUTES = 6;
const MINUTE = 7;
/** /**
* @brief Check for Memcache and open a connection if configured * @var Cache\ICacheDriver
*
* @return Memcache|boolean The Memcache object - or "false" if not successful
*/ */
public static function memcache() static $driver = null;
public static function init()
{ {
if (!class_exists('Memcache', false)) { switch(Config::get('system', 'cache_driver', 'database')) {
return false; case 'memcache':
$memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
$memcache_port = Config::get('system', 'memcache_port', 11211);
self::$driver = new Cache\MemcacheCacheDriver($memcache_host, $memcache_port);
break;
case 'memcached':
$memcached_hosts = Config::get('system', 'memcached_hosts', [['127.0.0.1', 11211]]);
self::$driver = new Cache\MemcachedCacheDriver($memcached_hosts);
break;
default:
self::$driver = new Cache\DatabaseCacheDriver();
} }
if (!Config::get('system', 'memcache')) {
return false;
}
$memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
$memcache_port = Config::get('system', 'memcache_port', 11211);
$memcache = new Memcache();
if (!$memcache->connect($memcache_host, $memcache_port)) {
return false;
}
return $memcache;
} }
/** /**
@ -51,31 +52,31 @@ class Cache
* *
* @return integer The cache duration in seconds * @return integer The cache duration in seconds
*/ */
private static function duration($level) public static function duration($level)
{ {
switch ($level) { switch ($level) {
case CACHE_MONTH: case self::MONTH:
$seconds = 2592000; $seconds = 2592000;
break; break;
case CACHE_WEEK: case self::WEEK:
$seconds = 604800; $seconds = 604800;
break; break;
case CACHE_DAY: case self::DAY:
$seconds = 86400; $seconds = 86400;
break; break;
case CACHE_HOUR: case self::HOUR:
$seconds = 3600; $seconds = 3600;
break; break;
case CACHE_HALF_HOUR: case self::HALF_HOUR:
$seconds = 1800; $seconds = 1800;
break; break;
case CACHE_QUARTER_HOUR: case self::QUARTER_HOUR:
$seconds = 900; $seconds = 900;
break; break;
case CACHE_FIVE_MINUTES: case self::FIVE_MINUTES:
$seconds = 300; $seconds = 300;
break; break;
case CACHE_MINUTE: case self::MINUTE:
default: default:
$seconds = 60; $seconds = 60;
break; break;
@ -83,6 +84,20 @@ class Cache
return $seconds; return $seconds;
} }
/**
* Returns the current cache driver
*
* @return Cache\ICacheDriver
*/
private static function getDriver()
{
if (self::$driver === null) {
self::init();
}
return self::$driver;
}
/** /**
* @brief Fetch cached data according to the key * @brief Fetch cached data according to the key
* *
@ -92,40 +107,13 @@ class Cache
*/ */
public static function get($key) public static function get($key)
{ {
$memcache = self::memcache(); $time = microtime(true);
if (is_object($memcache)) {
// We fetch with the hostname as key to avoid problems with other applications
$cached = $memcache->get(get_app()->get_hostname().":".$key);
$value = @unserialize($cached);
// Only return a value if the serialized value is valid. $return = self::getDriver()->get($key);
// 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; self::getApp()->save_timestamp($time, 'cache');
}
// Frequently clear cache return $return;
self::clear();
$cache = dba::selectFirst('cache', ['v'], ['k' => $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;
} }
/** /**
@ -137,20 +125,17 @@ class Cache
* @param mixed $value The value that is about to be stored * @param mixed $value The value that is about to be stored
* @param integer $duration The cache lifespan * @param integer $duration The cache lifespan
* *
* @return void * @return bool
*/ */
public static function set($key, $value, $duration = CACHE_MONTH) public static function set($key, $value, $duration = self::MONTH)
{ {
// Do we have an installed memcache? Use it instead. $time = microtime(true);
$memcache = self::memcache();
if (is_object($memcache)) { $return = self::getDriver()->set($key, $value, $duration);
// We store with the hostname as key to avoid problems with other applications
$memcache->set(get_app()->get_hostname().":".$key, serialize($value), MEMCACHE_COMPRESSED, self::duration($duration)); self::getApp()->save_timestamp($time, 'cache_write');
return;
} return $return;
$fields = ['v' => serialize($value), 'expire_mode' => $duration, 'updated' => DateTimeFormat::utcNow()];
$condition = ['k' => $key];
dba::update('cache', $fields, $condition, true);
} }
/** /**
@ -160,76 +145,8 @@ class Cache
* *
* @return void * @return void
*/ */
public static function clear($max_level = CACHE_MONTH) public static function clear()
{ {
// Clear long lasting cache entries only once a day return self::getDriver()->clear();
if (Config::get("system", "cache_cleared_day") < time() - self::duration(CACHE_DAY)) {
if ($max_level == CACHE_MONTH) {
$condition = ["`updated` < ? AND `expire_mode` = ?",
DateTimeFormat::utc("now - 30 days"),
CACHE_MONTH];
dba::delete('cache', $condition);
}
if ($max_level <= CACHE_WEEK) {
$condition = ["`updated` < ? AND `expire_mode` = ?",
DateTimeFormat::utc("now - 7 days"),
CACHE_WEEK];
dba::delete('cache', $condition);
}
if ($max_level <= CACHE_DAY) {
$condition = ["`updated` < ? AND `expire_mode` = ?",
DateTimeFormat::utc("now - 1 days"),
CACHE_DAY];
dba::delete('cache', $condition);
}
Config::set("system", "cache_cleared_day", time());
}
if (($max_level <= CACHE_HOUR) && (Config::get("system", "cache_cleared_hour")) < time() - self::duration(CACHE_HOUR)) {
$condition = ["`updated` < ? AND `expire_mode` = ?",
DateTimeFormat::utc("now - 1 hours"),
CACHE_HOUR];
dba::delete('cache', $condition);
Config::set("system", "cache_cleared_hour", time());
}
if (($max_level <= CACHE_HALF_HOUR) && (Config::get("system", "cache_cleared_half_hour")) < time() - self::duration(CACHE_HALF_HOUR)) {
$condition = ["`updated` < ? AND `expire_mode` = ?",
DateTimeFormat::utc("now - 30 minutes"),
CACHE_HALF_HOUR];
dba::delete('cache', $condition);
Config::set("system", "cache_cleared_half_hour", time());
}
if (($max_level <= CACHE_QUARTER_HOUR) && (Config::get("system", "cache_cleared_quarter_hour")) < time() - self::duration(CACHE_QUARTER_HOUR)) {
$condition = ["`updated` < ? AND `expire_mode` = ?",
DateTimeFormat::utc("now - 15 minutes"),
CACHE_QUARTER_HOUR];
dba::delete('cache', $condition);
Config::set("system", "cache_cleared_quarter_hour", time());
}
if (($max_level <= CACHE_FIVE_MINUTES) && (Config::get("system", "cache_cleared_five_minute")) < time() - self::duration(CACHE_FIVE_MINUTES)) {
$condition = ["`updated` < ? AND `expire_mode` = ?",
DateTimeFormat::utc("now - 5 minutes"),
CACHE_FIVE_MINUTES];
dba::delete('cache', $condition);
Config::set("system", "cache_cleared_five_minute", time());
}
if (($max_level <= CACHE_MINUTE) && (Config::get("system", "cache_cleared_minute")) < time() - self::duration(CACHE_MINUTE)) {
$condition = ["`updated` < ? AND `expire_mode` = ?",
DateTimeFormat::utc("now - 1 minutes"),
CACHE_MINUTE];
dba::delete('cache', $condition);
Config::set("system", "cache_cleared_minute", time());
}
} }
} }

View 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` >= ?', $key, DateTimeFormat::utcNow()]);
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()']);
}
}

View file

@ -0,0 +1,50 @@
<?php
namespace Friendica\Core\Cache;
use Friendica\Core\Cache;
/**
* Cache Driver Interface
*
* @author Hypolite Petovan <mrpetovan@gmail.com>
*/
interface ICacheDriver
{
/**
* Fetches cached data according to the key
*
* @param string $key The key to the cached data
*
* @return mixed Cached $value or "null" if not found
*/
public function get($key);
/**
* Stores data in the cache identified by the key. The input $value can have multiple formats.
*
* @param string $key The cache key
* @param mixed $value The value to store
* @param integer $duration The cache lifespan, must be one of the Cache constants
*
* @return bool
*/
public function set($key, $value, $duration = Cache::MONTH);
/**
* Delete a key from the cache
*
* @param string $key
*
* @return bool
*/
public function delete($key);
/**
* Remove outdated data from the cache
*
* @return bool
*/
public function clear();
}

View file

@ -0,0 +1,77 @@
<?php
namespace Friendica\Core\Cache;
use Friendica\BaseObject;
use Friendica\Core\Cache;
/**
* Memcache Cache Driver
*
* @author Hypolite Petovan <mrpetovan@gmail.com>
*/
class MemcacheCacheDriver extends BaseObject implements ICacheDriver
{
/**
* @var Memcache
*/
private $memcache;
public function __construct($memcache_host, $memcache_port)
{
if (!class_exists('Memcache', false)) {
throw new \Exception('Memcache class isn\'t available');
}
$this->memcache = new \Memcache();
if (!$this->memcache->connect($memcache_host, $memcache_port)) {
throw new \Exception('Expected Memcache server at ' . $memcache_host . ':' . $memcache_port . ' isn\'t available');
}
}
public function get($key)
{
$return = null;
// We fetch with the hostname as key to avoid problems with other applications
$cached = $this->memcache->get(self::getApp()->get_hostname() . ':' . $key);
// @see http://php.net/manual/en/memcache.get.php#84275
if (is_bool($cached) || is_double($cached) || is_long($cached)) {
return $return;
}
$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 $return;
}
public function set($key, $value, $duration = Cache::MONTH)
{
// We store with the hostname as key to avoid problems with other applications
return $this->memcache->set(
self::getApp()->get_hostname() . ":" . $key,
serialize($value),
MEMCACHE_COMPRESSED,
Cache::duration($duration)
);
}
public function delete($key)
{
return $this->memcache->delete($key);
}
public function clear()
{
return true;
}
}

View file

@ -0,0 +1,68 @@
<?php
namespace Friendica\Core\Cache;
use Friendica\BaseObject;
use Friendica\Core\Cache;
/**
* Memcached Cache Driver
*
* @author Hypolite Petovan <mrpetovan@gmail.com>
*/
class MemcachedCacheDriver extends BaseObject implements ICacheDriver
{
/**
* @var Memcached
*/
private $memcached;
public function __construct(array $memcached_hosts)
{
if (!class_exists('Memcached', false)) {
throw new \Exception('Memcached class isn\'t available');
}
$this->memcached = new \Memcached();
$this->memcached->addServers($memcached_hosts);
if (count($this->memcached->getServerList()) == 0) {
throw new \Exception('Expected Memcached servers aren\'t available, config:' . var_export($memcached_hosts, true));
}
}
public function get($key)
{
$return = null;
// We fetch with the hostname as key to avoid problems with other applications
$value = $this->memcached->get(self::getApp()->get_hostname() . ':' . $key);
if ($this->memcached->getResultCode() === \Memcached::RES_SUCCESS) {
$return = $value;
}
return $return;
}
public function set($key, $value, $duration = Cache::MONTH)
{
// We store with the hostname as key to avoid problems with other applications
return $this->memcached->set(
self::getApp()->get_hostname() . ":" . $key,
$value,
Cache::duration($duration)
);
}
public function delete($key)
{
return $this->memcached->delete($key);
}
public function clear()
{
return true;
}
}

View file

@ -5,8 +5,8 @@
*/ */
namespace Friendica\Core; namespace Friendica\Core;
use Friendica\Core\Session\CacheSessionHandler;
use Friendica\Core\Session\DatabaseSessionHandler; use Friendica\Core\Session\DatabaseSessionHandler;
use Friendica\Core\Session\MemcacheSessionHandler;
/** /**
* High-level Session service class * High-level Session service class
@ -28,10 +28,10 @@ class Session
ini_set('session.cookie_secure', 1); ini_set('session.cookie_secure', 1);
} }
if (!Config::get('system', 'disable_database_session')) { $session_handler = Config::get('system', 'session_handler', 'database');
$memcache = Cache::memcache(); if ($session_handler != 'native') {
if (is_object($memcache)) { if ($session_handler == 'cache' && Config::get('system', 'cache_driver', 'database') != 'database') {
$SessionHandler = new MemcacheSessionHandler($memcache); $SessionHandler = new CacheSessionHandler();
} else { } else {
$SessionHandler = new DatabaseSessionHandler(); $SessionHandler = new DatabaseSessionHandler();
} }

View file

@ -3,34 +3,20 @@
namespace Friendica\Core\Session; namespace Friendica\Core\Session;
use Friendica\BaseObject; use Friendica\BaseObject;
use Friendica\Core\Cache;
use Friendica\Core\Session; use Friendica\Core\Session;
use SessionHandlerInterface; use SessionHandlerInterface;
use Memcache;
require_once 'boot.php'; require_once 'boot.php';
require_once 'include/text.php'; require_once 'include/text.php';
/** /**
* SessionHandler using Memcache * SessionHandler using Friendica Cache
* *
* @author Hypolite Petovan <mrpetovan@gmail.com> * @author Hypolite Petovan <mrpetovan@gmail.com>
*/ */
class MemcacheSessionHandler extends BaseObject implements SessionHandlerInterface class CacheSessionHandler extends BaseObject implements SessionHandlerInterface
{ {
/**
* @var Memcache
*/
private $memcache = null;
/**
*
* @param Memcache $memcache
*/
public function __construct(Memcache $memcache)
{
$this->memcache = $memcache;
}
public function open($save_path, $session_name) public function open($save_path, $session_name)
{ {
return true; return true;
@ -42,8 +28,8 @@ class MemcacheSessionHandler extends BaseObject implements SessionHandlerInterfa
return ''; return '';
} }
$data = $this->memcache->get(self::getApp()->get_hostname() . ":session:" . $session_id); $data = Cache::get('session:' . $session_id);
if (!is_bool($data)) { if (!empty($data)) {
Session::$exists = true; Session::$exists = true;
return $data; return $data;
} }
@ -72,14 +58,7 @@ class MemcacheSessionHandler extends BaseObject implements SessionHandlerInterfa
return true; return true;
} }
$expire = time() + Session::$expire; Cache::set('session:' . $session_id, $session_data, Session::$expire);
$this->memcache->set(
self::getApp()->get_hostname() . ":session:" . $session_id,
$session_data,
MEMCACHE_COMPRESSED,
$expire
);
return true; return true;
} }
@ -91,7 +70,7 @@ class MemcacheSessionHandler extends BaseObject implements SessionHandlerInterfa
public function destroy($id) public function destroy($id)
{ {
$this->memcache->delete(self::getApp()->get_hostname() . ":session:" . $id); Cache::delete('session:' . $id);
return true; return true;
} }

View file

@ -325,6 +325,8 @@ class Worker
$a->performance["start"] = microtime(true); $a->performance["start"] = microtime(true);
$a->performance["database"] = 0; $a->performance["database"] = 0;
$a->performance["database_write"] = 0; $a->performance["database_write"] = 0;
$a->performance["cache"] = 0;
$a->performance["cache_write"] = 0;
$a->performance["network"] = 0; $a->performance["network"] = 0;
$a->performance["file"] = 0; $a->performance["file"] = 0;
$a->performance["rendering"] = 0; $a->performance["rendering"] = 0;
@ -409,6 +411,24 @@ class Worker
} }
} }
} }
if (isset($a->callstack["dache"])) {
$o .= "\nCache Read:\n";
foreach ($a->callstack["dache"] as $func => $time) {
$time = round($time, 3);
if ($time > 0) {
$o .= $func.": ".$time."\n";
}
}
}
if (isset($a->callstack["dache_write"])) {
$o .= "\nCache Write:\n";
foreach ($a->callstack["dache_write"] as $func => $time) {
$time = round($time, 3);
if ($time > 0) {
$o .= $func.": ".$time."\n";
}
}
}
if (isset($a->callstack["network"])) { if (isset($a->callstack["network"])) {
$o .= "\nNetwork:\n"; $o .= "\nNetwork:\n";
foreach ($a->callstack["network"] as $func => $time) { foreach ($a->callstack["network"] as $func => $time) {
@ -422,12 +442,16 @@ class Worker
logger( logger(
"ID ".$queue["id"].": ".$funcname.": ".sprintf( "ID ".$queue["id"].": ".$funcname.": ".sprintf(
"DB: %s/%s, Net: %s, I/O: %s, Other: %s, Total: %s".$o, "DB: %s/%s, Cache: %s/%s, Net: %s, I/O: %s, Other: %s, Total: %s".$o,
number_format($a->performance["database"] - $a->performance["database_write"], 2), number_format($a->performance["database"] - $a->performance["database_write"], 2),
number_format($a->performance["database_write"], 2), number_format($a->performance["database_write"], 2),
number_format($a->performance["cache"], 2),
number_format($a->performance["cache_write"], 2),
number_format($a->performance["network"], 2), number_format($a->performance["network"], 2),
number_format($a->performance["file"], 2), number_format($a->performance["file"], 2),
number_format($duration - ($a->performance["database"] + $a->performance["network"] + $a->performance["file"]), 2), number_format($duration - ($a->performance["database"]
+ $a->performance["cache"] + $a->performance["cache_write"]
+ $a->performance["network"] + $a->performance["file"]), 2),
number_format($duration, 2) number_format($duration, 2)
), ),
LOGGER_DEBUG LOGGER_DEBUG

View file

@ -712,16 +712,16 @@ class DBStructure
] ]
]; ];
$database["cache"] = [ $database["cache"] = [
"comment" => "Used to store different data that doesn't to be stored for a long time", "comment" => "Stores temporary data",
"fields" => [ "fields" => [
"k" => ["type" => "varbinary(255)", "not null" => "1", "primary" => "1", "comment" => ""], "k" => ["type" => "varbinary(255)", "not null" => "1", "primary" => "1", "comment" => "cache key"],
"v" => ["type" => "mediumtext", "comment" => ""], "v" => ["type" => "mediumtext", "comment" => "cached serialized value"],
"expire_mode" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "0", "comment" => ""], "expires" => ["type" => "datetime", "not null" => "1", "default" => NULL_DATE, "comment" => "datetime of cache expiration"],
"updated" => ["type" => "datetime", "not null" => "1", "default" => NULL_DATE, "comment" => ""], "updated" => ["type" => "datetime", "not null" => "1", "default" => NULL_DATE, "comment" => "datetime of cache insertion"],
], ],
"indexes" => [ "indexes" => [
"PRIMARY" => ["k"], "PRIMARY" => ["k"],
"expire_mode_updated" => ["expire_mode", "updated"], "k_expires" => ["k", "expires"],
] ]
]; ];
$database["challenge"] = [ $database["challenge"] = [