]> git.mxchange.org Git - friendica.git/commitdiff
Increase cache lifespan / clear cache upon changes
authorMichael <heluecht@pirati.ca>
Tue, 17 May 2022 12:32:25 +0000 (12:32 +0000)
committerMichael <heluecht@pirati.ca>
Tue, 17 May 2022 12:32:25 +0000 (12:32 +0000)
src/Model/Contact.php
src/Model/Item.php
src/Model/Post/Collection.php
src/Module/Api/Mastodon/Statuses/Pin.php
src/Module/Api/Mastodon/Statuses/Unpin.php
src/Module/Item/Pin.php
src/Protocol/ActivityPub/Transmitter.php

index d0febc8bbaabd36bb389b7dbd2d47b8f9265d4df..eeaf5f81dfa08c0ea66cbc78b30c257487958aa0 100644 (file)
@@ -797,11 +797,13 @@ class Contact
        public static function remove($id)
        {
                // We want just to make sure that we don't delete our "self" contact
-               $contact = DBA::selectFirst('contact', ['uri-id', 'photo', 'thumb', 'micro'], ['id' => $id, 'self' => false]);
+               $contact = DBA::selectFirst('contact', ['uri-id', 'photo', 'thumb', 'micro', 'uid'], ['id' => $id, 'self' => false]);
                if (!DBA::isResult($contact)) {
                        return;
                }
 
+               self::clearFollowerFollowingEndpointCache($contact['uid']);
+
                // Archive the contact
                self::update(['archive' => true, 'network' => Protocol::PHANTOM, 'deleted' => true], ['id' => $id]);
 
@@ -899,6 +901,15 @@ class Contact
                self::remove($contact['id']);
        }
 
+       private static function clearFollowerFollowingEndpointCache(int $uid)
+       {
+               if (empty($uid)) {
+                       return;
+               }
+
+               DI::cache()->delete(ActivityPub\Transmitter::CACHEKEY_CONTACTS . 'followers:' . $uid);
+               DI::cache()->delete(ActivityPub\Transmitter::CACHEKEY_CONTACTS . 'following:' . $uid);
+       }
 
        /**
         * Marks a contact for archival after a communication issue delay
@@ -1764,7 +1775,7 @@ class Contact
                                break;
                        default:
                                /**
-                                * Use a random picture. 
+                                * Use a random picture.
                                 * The service provides random pictures from Unsplash.
                                 * @license https://unsplash.com/license
                                 */
@@ -2321,7 +2332,7 @@ class Contact
                                        Worker::add(PRIORITY_LOW, 'FetchFeaturedPosts', $ret['url']);
                                }
                        }
-       
+
                        $ret['last-item'] = Probe::getLastUpdate($ret);
                        Logger::info('Fetched last item', ['id' => $id, 'probed_url' => $ret['url'], 'last-item' => $ret['last-item'], 'callstack' => System::callstack(20)]);
                }
@@ -2707,6 +2718,8 @@ class Contact
                        $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
                }
 
+               self::clearFollowerFollowingEndpointCache($importer['uid']);
+
                if (!empty($contact)) {
                        if (!empty($contact['pending'])) {
                                Logger::info('Pending contact request already exists.', ['url' => $url, 'uid' => $importer['uid']]);
@@ -2830,6 +2843,8 @@ class Contact
                        return;
                }
 
+               self::clearFollowerFollowingEndpointCache($contact['uid']);
+
                $cdata = self::getPublicAndUserContactID($contact['id'], $contact['uid']);
 
                DI::notification()->deleteForUserByVerb($contact['uid'], Activity::FOLLOW, ['actor-id' => $cdata['public']]);
@@ -2844,6 +2859,8 @@ class Contact
         */
        public static function removeSharer(array $contact)
        {
+               self::clearFollowerFollowingEndpointCache($contact['uid']);
+
                if ($contact['rel'] == self::SHARING || in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
                        self::remove($contact['id']);
                } else {
index 8b2a4651dc3dc7fa30575d1b3886dacb47d7afe2..822580aa20cf367375303cbb4e5695e976c3e564 100644 (file)
@@ -1223,6 +1223,10 @@ class Item
                        self::updateDisplayCache($posted_item['uri-id']);
                }
 
+               if ($posted_item['origin'] && ($posted_item['uid'] != 0) && in_array($posted_item['gravity'], [GRAVITY_PARENT, GRAVITY_COMMENT])) {
+                       DI::cache()->delete(ActivityPub\Transmitter::CACHEKEY_OUTBOX . $posted_item['uid']);
+               }
+
                return $post_user_id;
        }
 
index 476e9d3b336802f68cf5d31a2bc32b6397de8965..34b5dc6dee7a2ae83b3e22075fbb0833083b5043 100644 (file)
@@ -24,6 +24,8 @@ namespace Friendica\Model\Post;
 use Friendica\Database\DBA;
 use BadMethodCallException;
 use Friendica\Database\Database;
+use Friendica\DI;
+use Friendica\Protocol\ActivityPub;
 
 class Collection
 {
@@ -34,14 +36,19 @@ class Collection
         *
         * @param integer $uri_id
         * @param integer $type
+        * @param integer $cache_uid If set to a non zero value, the featured cache is cleared
         */
-       public static function add(int $uri_id, int $type)
+       public static function add(int $uri_id, int $type, int $cache_uid = 0)
        {
                if (empty($uri_id)) {
                        throw new BadMethodCallException('Empty URI_id');
                }
 
                DBA::insert('post-collection', ['uri-id' => $uri_id, 'type' => $type], Database::INSERT_IGNORE);
+
+               if (!empty($cache_uid) && ($type == self::FEATURED)) {
+                       DI::cache()->delete(ActivityPub\Transmitter::CACHEKEY_FEATURED . $cache_uid);
+               }
        }
 
        /**
@@ -49,14 +56,19 @@ class Collection
         *
         * @param integer $uri_id
         * @param integer $type
+        * @param integer $cache_uid If set to a non zero value, the featured cache is cleared
         */
-       public static function remove(int $uri_id, int $type)
+       public static function remove(int $uri_id, int $type, int $cache_uid = 0)
        {
                if (empty($uri_id)) {
                        throw new BadMethodCallException('Empty URI_id');
                }
 
                DBA::delete('post-collection', ['uri-id' => $uri_id, 'type' => $type]);
+
+               if (!empty($cache_uid) && ($type == self::FEATURED)) {
+                       DI::cache()->delete(ActivityPub\Transmitter::CACHEKEY_FEATURED . $cache_uid);
+               }
        }
 
        /**
index 61a675b6120512ad5eb6d3188f89a850abeec54c..6bf8a67396224af454ffae19ec0dc637cec851ac 100644 (file)
@@ -46,7 +46,7 @@ class Pin extends BaseApi
                        DI::mstdnError()->RecordNotFound();
                }
 
-               Post\Collection::add($this->parameters['id'], Post\Collection::FEATURED);
+               Post\Collection::add($this->parameters['id'], Post\Collection::FEATURED, $uid);
 
                System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid)->toArray());
        }
index e54df31ebd15753c26984c399f3a5d261590a6b7..ee6763ff285772ab5f111aee4f91f31824575f05 100644 (file)
@@ -46,7 +46,7 @@ class Unpin extends BaseApi
                        DI::mstdnError()->RecordNotFound();
                }
 
-               Post\Collection::remove($this->parameters['id'], Post\Collection::FEATURED);
+               Post\Collection::remove($this->parameters['id'], Post\Collection::FEATURED, $uid);
 
                System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid)->toArray());
        }
index 3fdd783c6db752f8942be1f598044d694093cd50..9735e495c3f4d55b2666ae18c459710959f0e601 100644 (file)
@@ -60,9 +60,9 @@ class Pin extends BaseModule
                $pinned = !$item['featured'];
 
                if ($pinned) {
-                       Post\Collection::add($item['uri-id'], Post\Collection::FEATURED);
+                       Post\Collection::add($item['uri-id'], Post\Collection::FEATURED, local_user());
                } else {
-                       Post\Collection::remove($item['uri-id'], Post\Collection::FEATURED);
+                       Post\Collection::remove($item['uri-id'], Post\Collection::FEATURED, local_user());
                }
 
                // See if we've been passed a return path to redirect to
index 4651d6778fd2277f57500fbaaadd80bfb26e0acc..023bd5c83cce179c8c6cd16d2f9408b099f41f72 100644 (file)
@@ -205,7 +205,7 @@ class Transmitter
 
                if (!$show_contacts) {
                        if (!empty($cachekey)) {
-                               DI::cache()->set($cachekey, $data, Duration::QUARTER_HOUR);
+                               DI::cache()->set($cachekey, $data, Duration::DAY);
                        }
 
                        return $data;
@@ -233,7 +233,7 @@ class Transmitter
                }
 
                if (!empty($cachekey)) {
-                       DI::cache()->set($cachekey, $data, Duration::QUARTER_HOUR);
+                       DI::cache()->set($cachekey, $data, Duration::DAY);
                }
 
                return $data;
@@ -323,7 +323,7 @@ class Transmitter
                }
 
                if (!empty($cachekey)) {
-                       DI::cache()->set($cachekey, $data, Duration::QUARTER_HOUR);
+                       DI::cache()->set($cachekey, $data, Duration::DAY);
                }
 
                return $data;
@@ -407,7 +407,7 @@ class Transmitter
                $data['orderedItems'] = $list;
 
                if (!empty($cachekey)) {
-                       DI::cache()->set($cachekey, $data, Duration::QUARTER_HOUR);
+                       DI::cache()->set($cachekey, $data, Duration::DAY);
                }
 
                return $data;