objectcache: remove inheritence from CachedBagOStuff
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 10 Jul 2019 21:32:08 +0000 (14:32 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Wed, 10 Jul 2019 21:32:51 +0000 (14:32 -0700)
Change-Id: I24e5b6a95d74730c1679bdc7b7b394adcb6bd794

includes/libs/objectcache/CachedBagOStuff.php
includes/libs/objectcache/HashBagOStuff.php

index e193497..50ee1e3 100644 (file)
  *   up going to the HashBagOStuff used for the in-memory cache).
  *
  * @ingroup Cache
- * @TODO: Make this class use composition instead of calling super
  */
-class CachedBagOStuff extends HashBagOStuff {
+class CachedBagOStuff extends BagOStuff {
        /** @var BagOStuff */
        protected $backend;
+       /** @var HashBagOStuff */
+       protected $procCache;
 
        /**
         * @param BagOStuff $backend Permanent backend to use
         * @param array $params Parameters for HashBagOStuff
         */
-       function __construct( BagOStuff $backend, $params = [] ) {
+       public function __construct( BagOStuff $backend, $params = [] ) {
                unset( $params['reportDupes'] ); // useless here
 
                parent::__construct( $params );
 
                $this->backend = $backend;
+               $this->procCache = new HashBagOStuff( $params );
                $this->attrMap = $backend->attrMap;
        }
 
-       public function get( $key, $flags = 0 ) {
-               $ret = parent::get( $key, $flags );
-               if ( $ret === false && !$this->hasKey( $key ) ) {
+       protected function doGet( $key, $flags = 0, &$casToken = null ) {
+               $ret = $this->procCache->get( $key, $flags );
+               if ( $ret === false && !$this->procCache->hasKey( $key ) ) {
                        $ret = $this->backend->get( $key, $flags );
-                       $this->set( $key, $ret, 0, self::WRITE_CACHE_ONLY );
+                       $this->set( $key, $ret, self::TTL_INDEFINITE, self::WRITE_CACHE_ONLY );
                }
+
                return $ret;
        }
 
-       public function set( $key, $value, $exptime = 0, $flags = 0 ) {
-               parent::set( $key, $value, $exptime, $flags );
-               if ( !( $flags & self::WRITE_CACHE_ONLY ) ) {
-                       $this->backend->set( $key, $value, $exptime, $flags & ~self::WRITE_CACHE_ONLY );
+       protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
+               $this->procCache->set( $key, $value, $exptime, $flags );
+               if ( ( $flags & self::WRITE_CACHE_ONLY ) != self::WRITE_CACHE_ONLY ) {
+                       $this->backend->set( $key, $value, $exptime, $flags );
                }
+
                return true;
        }
 
-       public function delete( $key, $flags = 0 ) {
-               parent::delete( $key, $flags );
-               if ( !( $flags & self::WRITE_CACHE_ONLY ) ) {
-                       $this->backend->delete( $key );
+       protected function doDelete( $key, $flags = 0 ) {
+               $this->procCache->delete( $key, $flags );
+               if ( ( $flags & self::WRITE_CACHE_ONLY ) != self::WRITE_CACHE_ONLY ) {
+                       $this->backend->delete( $key, $flags );
                }
 
                return true;
        }
 
-       public function setDebug( $bool ) {
-               parent::setDebug( $bool );
-               $this->backend->setDebug( $bool );
-       }
-
        public function deleteObjectsExpiringBefore(
                $timestamp,
                callable $progressCallback = null,
                $limit = INF
        ) {
-               parent::deleteObjectsExpiringBefore( $timestamp, $progressCallback, $limit );
+               $this->procCache->deleteObjectsExpiringBefore( $timestamp, $progressCallback, $limit );
 
                return $this->backend->deleteObjectsExpiringBefore(
                        $timestamp,
@@ -96,18 +95,6 @@ class CachedBagOStuff extends HashBagOStuff {
                );
        }
 
-       public function makeKeyInternal( $keyspace, $args ) {
-               return $this->backend->makeKeyInternal( ...func_get_args() );
-       }
-
-       public function makeKey( $class, $component = null ) {
-               return $this->backend->makeKey( ...func_get_args() );
-       }
-
-       public function makeGlobalKey( $class, $component = null ) {
-               return $this->backend->makeGlobalKey( ...func_get_args() );
-       }
-
        // These just call the backend (tested elsewhere)
        // @codeCoverageIgnoreStart
 
@@ -121,7 +108,8 @@ class CachedBagOStuff extends HashBagOStuff {
 
        public function incr( $key, $value = 1 ) {
                $n = $this->backend->incr( $key, $value );
-               parent::delete( $key );
+
+               $this->procCache->delete( $key );
 
                return $n;
        }
@@ -134,6 +122,23 @@ class CachedBagOStuff extends HashBagOStuff {
                return $this->backend->unlock( $key );
        }
 
+       public function makeKeyInternal( $keyspace, $args ) {
+               return $this->backend->makeKeyInternal( ...func_get_args() );
+       }
+
+       public function makeKey( $class, $component = null ) {
+               return $this->backend->makeKey( ...func_get_args() );
+       }
+
+       public function makeGlobalKey( $class, $component = null ) {
+               return $this->backend->makeGlobalKey( ...func_get_args() );
+       }
+
+       public function setDebug( $bool ) {
+               parent::setDebug( $bool );
+               $this->backend->setDebug( $bool );
+       }
+
        public function getLastError() {
                return $this->backend->getLastError();
        }
index 016bdfe..c74bb6e 100644 (file)
@@ -149,7 +149,7 @@ class HashBagOStuff extends BagOStuff {
         * @return bool
         * @since 1.27
         */
-       protected function hasKey( $key ) {
+       public function hasKey( $key ) {
                return isset( $this->bag[$key] );
        }
 }