Prune old edit stash entries from users as they create more
authorAaron Schulz <aschulz@wikimedia.org>
Tue, 26 Mar 2019 05:26:13 +0000 (22:26 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Tue, 26 Mar 2019 18:24:56 +0000 (18:24 +0000)
This should reduce pressure on certain medium-large sized memcached
slabs. Pre-1.5 memcached versions have a harder time pruning expired
entries in time to avoid evictions, so it will be most useful that
scenario.

Bug: T203786
Change-Id: Ic357dbfcd9abd525b02e8d631d1344db3745d24c

includes/api/ApiStashEdit.php

index 455ff45..fe5f6c4 100644 (file)
@@ -46,6 +46,8 @@ class ApiStashEdit extends ApiBase {
        const MAX_CACHE_TTL = 300; // 5 minutes
        const MAX_SIGNATURE_TTL = 60;
 
+       const MAX_CACHE_RECENT = 2;
+
        public function execute() {
                $user = $this->getUser();
                $params = $this->extractRequestParams();
@@ -461,9 +463,32 @@ class ApiStashEdit extends ApiBase {
                        );
                }
 
+               if ( $ok ) {
+                       // These blobs can waste slots in low cardinality memcached slabs
+                       self::pruneExcessStashedEntries( $cache, $user, $key );
+               }
+
                return $ok ? true : 'store_error';
        }
 
+       /**
+        * @param BagOStuff $cache
+        * @param User $user
+        * @param string $newKey
+        */
+       private static function pruneExcessStashedEntries( BagOStuff $cache, User $user, $newKey ) {
+               $key = $cache->makeKey( 'stash-edit-recent', $user->getId() );
+
+               $keyList = $cache->get( $key ) ?: [];
+               if ( count( $keyList ) >= self::MAX_CACHE_RECENT ) {
+                       $oldestKey = array_shift( $keyList );
+                       $cache->delete( $oldestKey );
+               }
+
+               $keyList[] = $newKey;
+               $cache->set( $key, $keyList, 2 * self::MAX_CACHE_TTL );
+       }
+
        public function getAllowedParams() {
                return [
                        'title' => [