Merge pull request #8072 from nupplaphil/task/Cache_to_DI

Replace Core\Cache wrapper with DI::cache() method
This commit is contained in:
Hypolite Petovan 2020-01-10 08:46:03 -05:00 committed by GitHub
commit 36190d1e79
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 60 additions and 168 deletions

View file

@ -6,7 +6,7 @@
use Friendica\App;
use Friendica\Content\ForumManager;
use Friendica\Content\Text\BBCode;
use Friendica\Core\Cache;
use Friendica\Core\Cache\Cache;
use Friendica\Core\Config;
use Friendica\Core\Hook;
use Friendica\Core\L10n;
@ -197,7 +197,7 @@ function ping_init(App $a)
}
$cachekey = "ping_init:".local_user();
$ev = Cache::get($cachekey);
$ev = DI::cache()->get($cachekey);
if (is_null($ev)) {
$ev = q(
"SELECT type, start, adjust FROM `event`
@ -208,7 +208,7 @@ function ping_init(App $a)
DBA::escape(DateTimeFormat::utcNow())
);
if (DBA::isResult($ev)) {
Cache::set($cachekey, $ev, Cache::HOUR);
DI::cache()->set($cachekey, $ev, Cache::HOUR);
}
}

View file

@ -6,13 +6,12 @@
use Friendica\App;
use Friendica\Content\Text\BBCode;
use Friendica\Core\Cache;
use Friendica\Core\Config;
use Friendica\Core\Logger;
use Friendica\Core\Protocol;
use Friendica\Core\Renderer;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Protocol\PortableContact;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Strings;
@ -255,10 +254,10 @@ function poco_init(App $a) {
if (isset($contact['account-type'])) {
$contact['contact-type'] = $contact['account-type'];
}
$about = Cache::get("about:" . $contact['updated'] . ":" . $contact['nurl']);
$about = DI::cache()->get("about:" . $contact['updated'] . ":" . $contact['nurl']);
if (is_null($about)) {
$about = BBCode::convert($contact['about'], false);
Cache::set("about:" . $contact['updated'] . ":" . $contact['nurl'], $about);
DI::cache()->set("about:" . $contact['updated'] . ":" . $contact['nurl'], $about);
}
// Non connected persons can only see the keywords of a Diaspora account

View file

@ -10,7 +10,7 @@ use DOMNode;
use DOMText;
use DOMXPath;
use Exception;
use Friendica\Core\Cache;
use Friendica\Core\Cache\Cache;
use Friendica\Core\Config;
use Friendica\Core\Hook;
use Friendica\Core\L10n;
@ -66,7 +66,7 @@ class OEmbed
if (DBA::isResult($oembed_record)) {
$json_string = $oembed_record['content'];
} else {
$json_string = Cache::get($cache_key);
$json_string = DI::cache()->get($cache_key);
}
// These media files should now be caught in bbcode.php
@ -125,7 +125,7 @@ class OEmbed
$cache_ttl = Cache::FIVE_MINUTES;
}
Cache::set($cache_key, $json_string, $cache_ttl);
DI::cache()->set($cache_key, $json_string, $cache_ttl);
}
if ($oembed->type == 'error') {

View file

@ -10,7 +10,6 @@ use DOMXPath;
use Exception;
use Friendica\Content\OEmbed;
use Friendica\Content\Smilies;
use Friendica\Core\Cache;
use Friendica\Core\Config;
use Friendica\Core\Hook;
use Friendica\Core\L10n;
@ -1070,7 +1069,7 @@ class BBCode
private static function removePictureLinksCallback($match)
{
$cache_key = 'remove:' . $match[1];
$text = Cache::get($cache_key);
$text = DI::cache()->get($cache_key);
if (is_null($text)) {
$a = DI::app();
@ -1112,7 +1111,7 @@ class BBCode
}
}
}
Cache::set($cache_key, $text);
DI::cache()->set($cache_key, $text);
}
return $text;
@ -1143,7 +1142,7 @@ class BBCode
}
$cache_key = 'clean:' . $match[1];
$text = Cache::get($cache_key);
$text = DI::cache()->get($cache_key);
if (!is_null($text)) {
return $text;
}
@ -1194,7 +1193,7 @@ class BBCode
}
}
}
Cache::set($cache_key, $text);
DI::cache()->set($cache_key, $text);
return $text;
}

View file

@ -2,10 +2,8 @@
namespace Friendica\Content\Widget;
use Friendica\Core\Cache;
use Friendica\Core\L10n;
use Friendica\Core\Renderer;
use Friendica\Database\DBA;
use Friendica\Model\Term;
/**

View file

@ -1,102 +0,0 @@
<?php
/**
* @file src/Core/Cache.php
*/
namespace Friendica\Core;
use Friendica\Core\Cache\Cache as CacheClass;
use Friendica\DI;
/**
* @brief Class for storing data for a short time
*/
class Cache
{
/** @deprecated Use CacheClass::MONTH */
const MONTH = CacheClass::MONTH;
/** @deprecated Use CacheClass::WEEK */
const WEEK = CacheClass::WEEK;
/** @deprecated Use CacheClass::DAY */
const DAY = CacheClass::DAY;
/** @deprecated Use CacheClass::HOUR */
const HOUR = CacheClass::HOUR;
/** @deprecated Use CacheClass::HALF_HOUR */
const HALF_HOUR = CacheClass::HALF_HOUR;
/** @deprecated Use CacheClass::QUARTER_HOUR */
const QUARTER_HOUR = CacheClass::QUARTER_HOUR;
/** @deprecated Use CacheClass::FIVE_MINUTES */
const FIVE_MINUTES = CacheClass::FIVE_MINUTES;
/** @deprecated Use CacheClass::MINUTE */
const MINUTE = CacheClass::MINUTE;
/** @deprecated Use CacheClass::INFINITE */
const INFINITE = CacheClass::INFINITE;
/**
* @brief Returns all the cache keys sorted alphabetically
*
* @param string $prefix Prefix of the keys (optional)
*
* @return array Empty if the driver doesn't support this feature
* @throws \Exception
*/
public static function getAllKeys($prefix = null)
{
return DI::cache()->getAllKeys($prefix);
}
/**
* @brief Fetch cached data according to the key
*
* @param string $key The key to the cached data
*
* @return mixed Cached $value or "null" if not found
* @throws \Exception
*/
public static function get($key)
{
return DI::cache()->get($key);
}
/**
* @brief Put data in the cache according to the key
*
* The input $value can have multiple formats.
*
* @param string $key The key to the cached data
* @param mixed $value The value that is about to be stored
* @param integer $duration The cache lifespan
*
* @return bool
* @throws \Exception
*/
public static function set($key, $value, $duration = CacheClass::MONTH)
{
return DI::cache()->set($key, $value, $duration);
}
/**
* @brief Delete a value from the cache
*
* @param string $key The key to the cached data
*
* @return bool
* @throws \Exception
*/
public static function delete($key)
{
return DI::cache()->delete($key);
}
/**
* @brief Remove outdated data from the cache
*
* @param boolean $outdated just remove outdated values
*
* @return bool
* @throws \Exception
*/
public static function clear($outdated = true)
{
return DI::cache()->clear($outdated);
}
}

View file

@ -2,7 +2,7 @@
namespace Friendica\Core\Lock;
use Friendica\Core\Cache;
use Friendica\Core\Cache\Cache;
use Friendica\Database\Database;
use Friendica\Util\DateTimeFormat;

View file

@ -7,6 +7,7 @@ use Friendica\Database\DBA;
use Friendica\Database\DBStructure;
use Friendica\DI;
use Friendica\Util\Strings;
use Friendica\Core\Cache\Cache;
class Update
{

View file

@ -6,7 +6,7 @@
*/
namespace Friendica\Model;
use Friendica\Core\Cache;
use Friendica\Core\Cache\Cache;
use Friendica\Core\Config;
use Friendica\Core\L10n;
use Friendica\Core\Logger;
@ -540,7 +540,7 @@ class Photo
$sql_extra = Security::getPermissionsSQLByUserId($uid);
$key = "photo_albums:".$uid.":".local_user().":".remote_user();
$albums = Cache::get($key);
$albums = DI::cache()->get($key);
if (is_null($albums) || $update) {
if (!Config::get("system", "no_count", false)) {
/// @todo This query needs to be renewed. It is really slow
@ -563,7 +563,7 @@ class Photo
DBA::escape(L10n::t("Contact Photos"))
);
}
Cache::set($key, $albums, Cache::DAY);
DI::cache()->set($key, $albums, Cache::DAY);
}
return $albums;
}
@ -576,7 +576,7 @@ class Photo
public static function clearAlbumCache($uid)
{
$key = "photo_albums:".$uid.":".local_user().":".remote_user();
Cache::set($key, null, Cache::DAY);
DI::cache()->set($key, null, Cache::DAY);
}
/**

View file

@ -10,7 +10,7 @@ use Friendica\Content\ForumManager;
use Friendica\Content\Text\BBCode;
use Friendica\Content\Text\HTML;
use Friendica\Content\Widget\ContactBlock;
use Friendica\Core\Cache;
use Friendica\Core\Cache\Cache;
use Friendica\Core\Config;
use Friendica\Core\Hook;
use Friendica\Core\L10n;
@ -586,7 +586,7 @@ class Profile
$bd_short = L10n::t('F d');
$cachekey = 'get_birthdays:' . local_user();
$r = Cache::get($cachekey);
$r = DI::cache()->get($cachekey);
if (is_null($r)) {
$s = DBA::p(
"SELECT `event`.*, `event`.`id` AS `eid`, `contact`.* FROM `event`
@ -608,7 +608,7 @@ class Profile
);
if (DBA::isResult($s)) {
$r = DBA::toArray($s);
Cache::set($cachekey, $r, Cache::HOUR);
DI::cache()->set($cachekey, $r, Cache::HOUR);
}
}
@ -1066,11 +1066,11 @@ class Profile
// Avoid endless loops
$cachekey = 'zrlInit:' . $my_url;
if (Cache::get($cachekey)) {
if (DI::cache()->get($cachekey)) {
Logger::log('URL ' . $my_url . ' already tried to authenticate.', Logger::DEBUG);
return;
} else {
Cache::set($cachekey, true, Cache::MINUTE);
DI::cache()->set($cachekey, true, Cache::MINUTE);
}
Logger::log('Not authenticated. Invoking reverse magic-auth for ' . $my_url, Logger::DEBUG);

View file

@ -4,7 +4,7 @@
*/
namespace Friendica\Model;
use Friendica\Core\Cache;
use Friendica\Core\Cache\Cache;
use Friendica\Core\Logger;
use Friendica\Database\DBA;
use Friendica\DI;
@ -57,7 +57,7 @@ class Term
*/
public static function getGlobalTrendingHashtags(int $period, $limit = 10)
{
$tags = Cache::get('global_trending_tags');
$tags = DI::cache()->get('global_trending_tags');
if (!$tags) {
$tagsStmt = DBA::p("SELECT t.`term`, COUNT(*) AS `score`
@ -84,7 +84,7 @@ class Term
if (DBA::isResult($tagsStmt)) {
$tags = DBA::toArray($tagsStmt);
Cache::set('global_trending_tags', $tags, Cache::HOUR);
DI::cache()->set('global_trending_tags', $tags, Cache::HOUR);
}
}
@ -100,7 +100,7 @@ class Term
*/
public static function getLocalTrendingHashtags(int $period, $limit = 10)
{
$tags = Cache::get('local_trending_tags');
$tags = DI::cache()->get('local_trending_tags');
if (!$tags) {
$tagsStmt = DBA::p("SELECT t.`term`, COUNT(*) AS `score`
@ -129,7 +129,7 @@ class Term
if (DBA::isResult($tagsStmt)) {
$tags = DBA::toArray($tagsStmt);
Cache::set('local_trending_tags', $tags, Cache::HOUR);
DI::cache()->set('local_trending_tags', $tags, Cache::HOUR);
}
}

View file

@ -2,13 +2,10 @@
namespace Friendica\Module\Search;
use Friendica\App\Arguments;
use Friendica\App\BaseURL;
use Friendica\Content\Nav;
use Friendica\Content\Pager;
use Friendica\Content\Text\HTML;
use Friendica\Content\Widget;
use Friendica\Core\Cache;
use Friendica\Core\Cache\Cache as CacheClass;
use Friendica\Core\Config;
use Friendica\Core\L10n;
@ -53,15 +50,15 @@ class Index extends BaseSearchModule
$crawl_permit_period = 10;
$remote = $_SERVER['REMOTE_ADDR'];
$result = Cache::get('remote_search:' . $remote);
$result = DI::cache()->get('remote_search:' . $remote);
if (!is_null($result)) {
$resultdata = json_decode($result);
if (($resultdata->time > (time() - $crawl_permit_period)) && ($resultdata->accesses > $free_crawls)) {
throw new HTTPException\TooManyRequestsException(L10n::t('Only one search per minute is permitted for not logged in users.'));
}
Cache::set('remote_search:' . $remote, json_encode(['time' => time(), 'accesses' => $resultdata->accesses + 1]), CacheClass::HOUR);
DI::cache()->set('remote_search:' . $remote, json_encode(['time' => time(), 'accesses' => $resultdata->accesses + 1]), CacheClass::HOUR);
} else {
Cache::set('remote_search:' . $remote, json_encode(['time' => time(), 'accesses' => 1]), CacheClass::HOUR);
DI::cache()->set('remote_search:' . $remote, json_encode(['time' => time(), 'accesses' => 1]), CacheClass::HOUR);
}
}

View file

@ -11,7 +11,7 @@ namespace Friendica\Network;
use DOMDocument;
use DomXPath;
use Friendica\Core\Cache;
use Friendica\Core\Cache\Cache;
use Friendica\Core\Config;
use Friendica\Core\Logger;
use Friendica\Core\Protocol;
@ -332,7 +332,7 @@ class Probe
public static function uri($uri, $network = '', $uid = -1, $cache = true)
{
if ($cache) {
$result = Cache::get('Probe::uri:' . $network . ':' . $uri);
$result = DI::cache()->get('Probe::uri:' . $network . ':' . $uri);
if (!is_null($result)) {
return $result;
}
@ -409,7 +409,7 @@ class Probe
// Only store into the cache if the value seems to be valid
if (!in_array($data['network'], [Protocol::PHANTOM, Protocol::MAIL])) {
Cache::set('Probe::uri:' . $network . ':' . $uri, $data, Cache::DAY);
DI::cache()->set('Probe::uri:' . $network . ':' . $uri, $data, Cache::DAY);
}
return $data;

View file

@ -7,7 +7,7 @@ namespace Friendica\Protocol\ActivityPub;
use Friendica\Content\Feature;
use Friendica\Content\Text\BBCode;
use Friendica\Content\Text\Plaintext;
use Friendica\Core\Cache;
use Friendica\Core\Cache\Cache;
use Friendica\Core\Config;
use Friendica\Core\Logger;
use Friendica\Core\Protocol;
@ -819,7 +819,7 @@ class Transmitter
$cachekey = 'APDelivery:createActivity:' . $item_id;
if (!$force) {
$data = Cache::get($cachekey);
$data = DI::cache()->get($cachekey);
if (!is_null($data)) {
return $data;
}
@ -827,7 +827,7 @@ class Transmitter
$data = ActivityPub\Transmitter::createActivityFromItem($item_id);
Cache::set($cachekey, $data, Cache::QUARTER_HOUR);
DI::cache()->set($cachekey, $data, Cache::QUARTER_HOUR);
return $data;
}

View file

@ -13,7 +13,7 @@ namespace Friendica\Protocol;
use Friendica\Content\Feature;
use Friendica\Content\Text\BBCode;
use Friendica\Content\Text\Markdown;
use Friendica\Core\Cache;
use Friendica\Core\Cache\Cache;
use Friendica\Core\Config;
use Friendica\Core\L10n;
use Friendica\Core\Logger;
@ -3246,7 +3246,7 @@ class Diaspora
$cachekey = "diaspora:sendParticipation:".$item['guid'];
$result = Cache::get($cachekey);
$result = DI::cache()->get($cachekey);
if (!is_null($result)) {
return;
}
@ -3272,7 +3272,7 @@ class Diaspora
Logger::log("Send participation for ".$item["guid"]." by ".$author, Logger::DEBUG);
// It doesn't matter what we store, we only want to avoid sending repeated notifications for the same item
Cache::set($cachekey, $item["guid"], Cache::QUARTER_HOUR);
DI::cache()->set($cachekey, $item["guid"], Cache::QUARTER_HOUR);
return self::buildAndTransmit($owner, $contact, "participation", $message);
}
@ -3524,7 +3524,7 @@ class Diaspora
{
$cachekey = "diaspora:buildStatus:".$item['guid'];
$result = Cache::get($cachekey);
$result = DI::cache()->get($cachekey);
if (!is_null($result)) {
return $result;
}
@ -3628,7 +3628,7 @@ class Diaspora
$msg = ["type" => $type, "message" => $message];
Cache::set($cachekey, $msg, Cache::QUARTER_HOUR);
DI::cache()->set($cachekey, $msg, Cache::QUARTER_HOUR);
return $msg;
}
@ -3749,7 +3749,7 @@ class Diaspora
{
$cachekey = "diaspora:constructComment:".$item['guid'];
$result = Cache::get($cachekey);
$result = DI::cache()->get($cachekey);
if (!is_null($result)) {
return $result;
}
@ -3798,7 +3798,7 @@ class Diaspora
$comment['thread_parent_guid'] = $thread_parent_item['guid'];
}
Cache::set($cachekey, $comment, Cache::QUARTER_HOUR);
DI::cache()->set($cachekey, $comment, Cache::QUARTER_HOUR);
return($comment);
}

View file

@ -8,7 +8,7 @@ use DOMDocument;
use DOMXPath;
use Friendica\Content\Text\BBCode;
use Friendica\Content\Text\HTML;
use Friendica\Core\Cache;
use Friendica\Core\Cache\Cache;
use Friendica\Core\Config;
use Friendica\Core\L10n;
use Friendica\Core\Lock;
@ -2185,7 +2185,7 @@ class OStatus
// Don't cache when the last item was posted less then 15 minutes ago (Cache duration)
if ((time() - strtotime($owner['last-item'])) < 15*60) {
$result = Cache::get($cachekey);
$result = DI::cache()->get($cachekey);
if (!$nocache && !is_null($result)) {
Logger::log('Feed duration: ' . number_format(microtime(true) - $stamp, 3) . ' - ' . $owner_nick . ' - ' . $filter . ' - ' . $previous_created . ' (cached)', Logger::DEBUG);
$last_update = $result['last_update'];
@ -2246,7 +2246,7 @@ class OStatus
$feeddata = trim($doc->saveXML());
$msg = ['feed' => $feeddata, 'last_update' => $last_update];
Cache::set($cachekey, $msg, Cache::QUARTER_HOUR);
DI::cache()->set($cachekey, $msg, Cache::QUARTER_HOUR);
Logger::log('Feed duration: ' . number_format(microtime(true) - $stamp, 3) . ' - ' . $owner_nick . ' - ' . $filter . ' - ' . $previous_created, Logger::DEBUG);

View file

@ -2,7 +2,6 @@
namespace Friendica\Util;
use Friendica\Core\Cache;
use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\DI;
@ -125,12 +124,12 @@ class Images
return $data;
}
$data = Cache::get($url);
$data = DI::cache()->get($url);
if (empty($data) || !is_array($data)) {
$data = self::getInfoFromURL($url);
Cache::set($url, $data);
DI::cache()->set($url, $data);
}
return $data;

View file

@ -4,9 +4,10 @@
*/
namespace Friendica\Util;
use Friendica\Core\Cache;
use Friendica\Core\Cache\Cache;
use Friendica\Core\Logger;
use Exception;
use Friendica\DI;
/**
* @brief This class contain methods to work with JsonLD data
@ -39,13 +40,13 @@ class JsonLD
exit();
}
$result = Cache::get('documentLoader:' . $url);
$result = DI::cache()->get('documentLoader:' . $url);
if (!is_null($result)) {
return $result;
}
$data = jsonld_default_document_loader($url);
Cache::set('documentLoader:' . $url, $data, Cache::DAY);
DI::cache()->set('documentLoader:' . $url, $data, Cache::DAY);
return $data;
}

View file

@ -5,7 +5,6 @@
namespace Friendica\Worker;
use Friendica\App;
use Friendica\Core\Cache;
use Friendica\Core\Config;
use Friendica\Core\Logger;
use Friendica\Core\Protocol;
@ -154,7 +153,7 @@ class CronJobs
}
// clear old cache
Cache::clear();
DI::cache()->clear();
// clear old item cache files
clear_cache();

View file

@ -4,12 +4,13 @@
*/
namespace Friendica\Worker;
use Friendica\Core\Cache;
use Friendica\Core\Cache\Cache;
use Friendica\Core\Config;
use Friendica\Core\Logger;
use Friendica\Core\Protocol;
use Friendica\Core\Search;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\GContact;
use Friendica\Model\GServer;
use Friendica\Network\Probe;
@ -26,7 +27,7 @@ class SearchDirectory
return;
}
$data = Cache::get('SearchDirectory:' . $search);
$data = DI::cache()->get('SearchDirectory:' . $search);
if (!is_null($data)) {
// Only search for the same item every 24 hours
if (time() < $data + (60 * 60 * 24)) {
@ -80,6 +81,6 @@ class SearchDirectory
}
}
}
Cache::set('SearchDirectory:' . $search, time(), Cache::DAY);
DI::cache()->set('SearchDirectory:' . $search, time(), Cache::DAY);
}
}

View file

@ -2,7 +2,7 @@
namespace Friendica\Test\Util;
use Friendica\Core\Cache;
use Friendica\Core\Cache\Cache;
use Friendica\Core\Lock\DatabaseLock;
trait DbaLockMockTrait