Browse Source

Add pool() method to Cache

This method exposes the new adapter layer to applications. The internals
of Cache have not been updated yet as I want to get feedback on the
approach first.
Mark Story 7 years ago
parent
commit
c259c5507f
2 changed files with 37 additions and 0 deletions
  1. 11 0
      src/Cache/Cache.php
  2. 26 0
      tests/TestCase/Cache/CacheTest.php

+ 11 - 0
src/Cache/Cache.php

@@ -235,6 +235,17 @@ class Cache
     }
 
     /**
+     * Get a SimpleCacheEngine object for the named cache pool.
+     *
+     * @param string $pool The name of the configured cache backend.
+     * @return \Cake\Cache\SimpleCacheEngine
+     */
+    public static function pool($config)
+    {
+        return new SimpleCacheEngine(static::engine($config));
+    }
+
+    /**
      * Garbage collection
      *
      * Permanently remove all expired and deleted data

+ 26 - 0
tests/TestCase/Cache/CacheTest.php

@@ -22,6 +22,7 @@ use Cake\Core\Plugin;
 use Cake\TestSuite\TestCase;
 use InvalidArgumentException;
 use PHPUnit\Framework\Error\Error;
+use Psr\SimpleCache\CacheInterface as SimpleCacheInterface;
 
 /**
  * CacheTest class
@@ -907,4 +908,29 @@ class CacheTest extends TestCase
 
         $this->assertSame($registry, Cache::getRegistry());
     }
+
+    /**
+     * Test getting instances with pool
+     *
+     * @return void
+     */
+    public function testPool()
+    {
+        $this->_configCache();
+
+        $pool = Cache::pool('tests');
+        $this->assertInstanceOf(SimpleCacheInterface::class, $pool);
+    }
+
+    /**
+     * Test getting instances with pool
+     *
+     * @return void
+     */
+    public function testPoolCacheDisabled()
+    {
+        Cache::disable();
+        $pool = Cache::pool('tests');
+        $this->assertInstanceOf(SimpleCacheInterface::class, $pool);
+    }
 }