diff --git a/README.md b/README.md index 6b6c15f8..5ee4ecd8 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,9 @@ $stack->push( ``` ## Flysystem + +For Flysystem 1 + ```php [...] use League\Flysystem\Adapter\Local; @@ -133,6 +136,28 @@ $stack->push( ); ``` +For Flysystem 2 + +```php +[...] +use League\Flysystem\Local\LocalFilesystemAdapter; +use Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy; +use Kevinrob\GuzzleCache\Storage\Flysystem2Storage; + +[...] + +$stack->push( + new CacheMiddleware( + new PrivateCacheStrategy( + new Flysystem2Storage( + new LocalFilesystemAdapter('/path/to/cache') + ) + ) + ), + 'cache' +); +``` + ## WordPress Object Cache ```php [...] diff --git a/src/Storage/Flysystem2Storage.php b/src/Storage/Flysystem2Storage.php new file mode 100644 index 00000000..c2f5d7f6 --- /dev/null +++ b/src/Storage/Flysystem2Storage.php @@ -0,0 +1,61 @@ +filesystem = new Filesystem($adapter); + } + + /** + * @inheritdoc + */ + public function fetch($key) + { + if ($this->filesystem->fileExists($key)) { + // The file exist, read it! + $data = @unserialize( + $this->filesystem->read($key) + ); + + if ($data instanceof CacheEntry) { + return $data; + } + } + + return; + } + + /** + * @inheritdoc + */ + public function save($key, CacheEntry $data) + { + return $this->filesystem->write($key, serialize($data)); + } + + /** + * {@inheritdoc} + */ + public function delete($key) + { + try { + return $this->filesystem->delete($key); + } catch (FileNotFoundException $ex) { + return true; + } + } +} diff --git a/tests/PrivateCacheTest.php b/tests/PrivateCacheTest.php index 5be441fc..510930ff 100644 --- a/tests/PrivateCacheTest.php +++ b/tests/PrivateCacheTest.php @@ -14,11 +14,13 @@ use Kevinrob\GuzzleCache\Storage\CompressedDoctrineCacheStorage; use Kevinrob\GuzzleCache\Storage\DoctrineCacheStorage; use Kevinrob\GuzzleCache\Storage\FlysystemStorage; +use Kevinrob\GuzzleCache\Storage\Flysystem2Storage; use Kevinrob\GuzzleCache\Storage\Psr6CacheStorage; use Kevinrob\GuzzleCache\Storage\Psr16CacheStorage; use Kevinrob\GuzzleCache\Storage\VolatileRuntimeStorage; use Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy; use League\Flysystem\Adapter\Local; +use League\Flysystem\Local\LocalFilesystemAdapter; use PHPUnit\Framework\TestCase; class PrivateCacheTest extends TestCase @@ -33,6 +35,7 @@ public function testCacheProvider() new DoctrineCacheStorage(new FilesystemCache($TMP_DIR)), new DoctrineCacheStorage(new PhpFileCache($TMP_DIR)), new FlysystemStorage(new Local($TMP_DIR)), + new Flysystem2Storage(new LocalFilesystemAdapter($TMP_DIR)), new Psr6CacheStorage(new ArrayCachePool()), new Psr16CacheStorage(new SimpleCacheBridge(new ArrayCachePool())), new CompressedDoctrineCacheStorage(new ArrayCache()), diff --git a/tests/PublicCacheTest.php b/tests/PublicCacheTest.php index b3e92719..982387f5 100644 --- a/tests/PublicCacheTest.php +++ b/tests/PublicCacheTest.php @@ -18,11 +18,13 @@ use Kevinrob\GuzzleCache\Storage\CompressedDoctrineCacheStorage; use Kevinrob\GuzzleCache\Storage\DoctrineCacheStorage; use Kevinrob\GuzzleCache\Storage\FlysystemStorage; +use Kevinrob\GuzzleCache\Storage\Flysystem2Storage; use Kevinrob\GuzzleCache\Storage\Psr6CacheStorage; use Kevinrob\GuzzleCache\Storage\Psr16CacheStorage; use Kevinrob\GuzzleCache\Storage\VolatileRuntimeStorage; use Kevinrob\GuzzleCache\Strategy\PublicCacheStrategy; use League\Flysystem\Adapter\Local; +use League\Flysystem\Local\LocalFilesystemAdapter; use Psr\Http\Message\RequestInterface; use PHPUnit\Framework\TestCase; @@ -101,6 +103,7 @@ public function testCacheProvider() new DoctrineCacheStorage(new FilesystemCache($TMP_DIR)), new DoctrineCacheStorage(new PhpFileCache($TMP_DIR)), new FlysystemStorage(new Local($TMP_DIR)), + new Flysystem2Storage(new LocalFilesystemAdapter($TMP_DIR)), new Psr6CacheStorage(new ArrayCachePool()), new Psr16CacheStorage(new SimpleCacheBridge(new ArrayCachePool())), new CompressedDoctrineCacheStorage(new ArrayCache()),