Browse Source

Backport of PR #13472 of 4.x, issue #13471

bancer 6 years ago
parent
commit
b8ff03fbd8

+ 8 - 1
src/Database/Connection.php

@@ -107,7 +107,14 @@ class Connection implements ConnectionInterface
     /**
      * Constructor.
      *
-     * @param array $config configuration for connecting to database
+     * @param array $config Configuration array.
+     * ### Available options:
+     * - `driver` Sort name or FCQN for driver.
+     * - `log` Boolean indicating whether to use query logging.
+     * - `name` Connection name.
+     * - `cacheMetaData` Boolean indicating whether metadata (datasource schemas) should be cached.
+     *    If set to a string it will be used as the name of cache config to use.
+     * - `cacheKeyPrefix` Custom prefix to use when generation cache keys. Defaults to connection name.
      */
     public function __construct($config)
     {

+ 6 - 1
src/Database/Schema/CachedCollection.php

@@ -77,7 +77,12 @@ class CachedCollection extends Collection
      */
     public function cacheKey($name)
     {
-        return $this->_connection->configName() . '_' . $name;
+        $cachePrefix = $this->_connection->configName();
+        $config = $this->_connection->config();
+        if (isset($config['cacheKeyPrefix'])) {
+            $cachePrefix = $config['cacheKeyPrefix'];
+        }
+        return $cachePrefix . '_' . $name;
     }
 
     /**

+ 38 - 0
tests/TestCase/Database/ConnectionTest.php

@@ -21,6 +21,7 @@ use Cake\Database\Exception\MissingConnectionException;
 use Cake\Database\Exception\NestedTransactionRollbackException;
 use Cake\Database\Log\LoggingStatement;
 use Cake\Database\Log\QueryLogger;
+use Cake\Database\Schema\CachedCollection;
 use Cake\Database\StatementInterface;
 use Cake\Database\Statement\BufferedStatement;
 use Cake\Datasource\ConnectionManager;
@@ -1168,6 +1169,43 @@ class ConnectionTest extends TestCase
     }
 
     /**
+     * Test CachedCollection creation with default and custom cache key prefix.
+     *
+     * @return void
+     */
+    public function testGetCachedCollection()
+    {
+        $driver = $this->getMockFormDriver();
+        $connection = $this->getMockBuilder(Connection::class)
+            ->setMethods(['connect'])
+            ->setConstructorArgs([[
+                'driver' => $driver,
+                'name' => 'default',
+                'cacheMetadata' => true,
+            ]])
+            ->getMock();
+
+        $schema = $connection->getSchemaCollection();
+        $this->assertInstanceOf(CachedCollection::class, $schema);
+        $this->assertSame('default_key', $schema->cacheKey('key'));
+
+        $driver = $this->getMockFormDriver();
+        $connection = $this->getMockBuilder(Connection::class)
+            ->setMethods(['connect'])
+            ->setConstructorArgs([[
+                'driver' => $driver,
+                'name' => 'default',
+                'cacheMetadata' => true,
+                'cacheKeyPrefix' => 'foo',
+            ]])
+            ->getMock();
+
+        $schema = $connection->getSchemaCollection();
+        $this->assertInstanceOf(CachedCollection::class, $schema);
+        $this->assertSame('foo_key', $schema->cacheKey('key'));
+    }
+
+    /**
      * Tests that allowed nesting of commit/rollback operations doesn't
      * throw any exceptions.
      *