Skip to content

Commit

Permalink
Fix the PSR simple cache mess by using composer to check which versio…
Browse files Browse the repository at this point in the history
…n is installed to maintain backwards compatibility with old php and laravel versions (#3851)

* Attempt to fix the psr mess for now
* Load different cache classes based on simple psr version handled by the cache manager
  • Loading branch information
patrickbrouwers authored Jan 2, 2023
1 parent f700014 commit f3a3f15
Show file tree
Hide file tree
Showing 9 changed files with 346 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}.*" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
composer update --${{ matrix.dependency-version }} --no-interaction
- name: Install legacy factories
run: |
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"php": "^7.0|^8.0",
"phpoffice/phpspreadsheet": "^1.18",
"illuminate/support": "5.8.*|^6.0|^7.0|^8.0|^9.0",
"psr/simple-cache": "^1.0|^2.0"
"psr/simple-cache": "^1.0|^2.0|^3.0",
"composer/semver": "^3.3"
},
"require-dev": {
"orchestra/testbench": "^6.0|^7.0",
Expand Down
16 changes: 8 additions & 8 deletions src/Cache/BatchCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(CacheInterface $cache, MemoryCache $memory)
/**
* {@inheritdoc}
*/
public function get($key, $default = null)
public function get(string $key, mixed $default = null): mixed
{
if ($this->memory->has($key)) {
return $this->memory->get($key);
Expand All @@ -41,7 +41,7 @@ public function get($key, $default = null)
/**
* {@inheritdoc}
*/
public function set($key, $value, $ttl = null)
public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool
{
$this->memory->set($key, $value, $ttl);

Expand All @@ -55,7 +55,7 @@ public function set($key, $value, $ttl = null)
/**
* {@inheritdoc}
*/
public function delete($key)
public function delete(string $key): bool
{
if ($this->memory->has($key)) {
return $this->memory->delete($key);
Expand All @@ -67,7 +67,7 @@ public function delete($key)
/**
* {@inheritdoc}
*/
public function clear()
public function clear(): bool
{
$this->memory->clear();

Expand All @@ -77,7 +77,7 @@ public function clear()
/**
* {@inheritdoc}
*/
public function getMultiple($keys, $default = null)
public function getMultiple(iterable $keys, mixed $default = null): iterable
{
// Check if all keys are still in memory
$memory = $this->memory->getMultiple($keys, $default);
Expand Down Expand Up @@ -105,7 +105,7 @@ public function getMultiple($keys, $default = null)
/**
* {@inheritdoc}
*/
public function setMultiple($values, $ttl = null)
public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool
{
$this->memory->setMultiple($values, $ttl);

Expand All @@ -119,7 +119,7 @@ public function setMultiple($values, $ttl = null)
/**
* {@inheritdoc}
*/
public function deleteMultiple($keys)
public function deleteMultiple(iterable $keys): bool
{
$keys = is_array($keys) ? $keys : iterator_to_array($keys);

Expand All @@ -131,7 +131,7 @@ public function deleteMultiple($keys)
/**
* {@inheritdoc}
*/
public function has($key)
public function has(string $key): bool
{
if ($this->memory->has($key)) {
return true;
Expand Down
142 changes: 142 additions & 0 deletions src/Cache/BatchCacheDeprecated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

namespace Maatwebsite\Excel\Cache;

use Psr\SimpleCache\CacheInterface;

class BatchCacheDeprecated implements CacheInterface
{
/**
* @var CacheInterface
*/
protected $cache;

/**
* @var MemoryCacheDeprecated
*/
protected $memory;

/**
* @param CacheInterface $cache
* @param MemoryCacheDeprecated $memory
*/
public function __construct(CacheInterface $cache, MemoryCacheDeprecated $memory)
{
$this->cache = $cache;
$this->memory = $memory;
}

/**
* {@inheritdoc}
*/
public function get($key, $default = null)
{
if ($this->memory->has($key)) {
return $this->memory->get($key);
}

return $this->cache->get($key, $default);
}

/**
* {@inheritdoc}
*/
public function set($key, $value, $ttl = null)
{
$this->memory->set($key, $value, $ttl);

if ($this->memory->reachedMemoryLimit()) {
return $this->cache->setMultiple($this->memory->flush(), $ttl);
}

return true;
}

/**
* {@inheritdoc}
*/
public function delete($key)
{
if ($this->memory->has($key)) {
return $this->memory->delete($key);
}

return $this->cache->delete($key);
}

/**
* {@inheritdoc}
*/
public function clear()
{
$this->memory->clear();

return $this->cache->clear();
}

/**
* {@inheritdoc}
*/
public function getMultiple($keys, $default = null)
{
// Check if all keys are still in memory
$memory = $this->memory->getMultiple($keys, $default);
$actualItemsInMemory = count(array_filter($memory));

if ($actualItemsInMemory === count($keys)) {
return $memory;
}

// Get all rows from cache if none is hold in memory.
if ($actualItemsInMemory === 0) {
return $this->cache->getMultiple($keys, $default);
}

// Add missing values from cache.
foreach ($this->cache->getMultiple($keys, $default) as $key => $value) {
if (null !== $value) {
$memory[$key] = $value;
}
}

return $memory;
}

/**
* {@inheritdoc}
*/
public function setMultiple($values, $ttl = null)
{
$this->memory->setMultiple($values, $ttl);

if ($this->memory->reachedMemoryLimit()) {
return $this->cache->setMultiple($this->memory->flush(), $ttl);
}

return true;
}

/**
* {@inheritdoc}
*/
public function deleteMultiple($keys)
{
$keys = is_array($keys) ? $keys : iterator_to_array($keys);

$this->memory->deleteMultiple($keys);

return $this->cache->deleteMultiple($keys);
}

/**
* {@inheritdoc}
*/
public function has($key)
{
if ($this->memory->has($key)) {
return true;
}

return $this->cache->has($key);
}
}
15 changes: 15 additions & 0 deletions src/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Maatwebsite\Excel\Cache;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Manager;
use Psr\SimpleCache\CacheInterface;
Expand Down Expand Up @@ -38,6 +40,12 @@ public function getDefaultDriver(): string
*/
public function createMemoryDriver(): CacheInterface
{
if (!InstalledVersions::satisfies(new VersionParser, 'psr/simple-cache', '^3.0')) {
return new MemoryCacheDeprecated(
config('excel.cache.batch.memory_limit', 60000)
);
}

return new MemoryCache(
config('excel.cache.batch.memory_limit', 60000)
);
Expand All @@ -48,6 +56,13 @@ public function createMemoryDriver(): CacheInterface
*/
public function createBatchDriver(): CacheInterface
{
if (!InstalledVersions::satisfies(new VersionParser, 'psr/simple-cache', '^3.0')) {
return new BatchCacheDeprecated(
$this->createIlluminateDriver(),
$this->createMemoryDriver()
);
}

return new BatchCache(
$this->createIlluminateDriver(),
$this->createMemoryDriver()
Expand Down
16 changes: 8 additions & 8 deletions src/Cache/MemoryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(int $memoryLimit = null)
/**
* {@inheritdoc}
*/
public function clear()
public function clear(): bool
{
$this->cache = [];

Expand All @@ -37,7 +37,7 @@ public function clear()
/**
* {@inheritdoc}
*/
public function delete($key)
public function delete(string $key): bool
{
unset($this->cache[$key]);

Expand All @@ -47,7 +47,7 @@ public function delete($key)
/**
* {@inheritdoc}
*/
public function deleteMultiple($keys)
public function deleteMultiple($keys): bool
{
foreach ($keys as $key) {
$this->delete($key);
Expand All @@ -59,7 +59,7 @@ public function deleteMultiple($keys)
/**
* {@inheritdoc}
*/
public function get($key, $default = null)
public function get(string $key, mixed $default = null): mixed
{
if ($this->has($key)) {
return $this->cache[$key];
Expand All @@ -71,7 +71,7 @@ public function get($key, $default = null)
/**
* {@inheritdoc}
*/
public function getMultiple($keys, $default = null)
public function getMultiple(iterable $keys, mixed $default = null): iterable
{
$results = [];
foreach ($keys as $key) {
Expand All @@ -84,15 +84,15 @@ public function getMultiple($keys, $default = null)
/**
* {@inheritdoc}
*/
public function has($key)
public function has($key): bool
{
return isset($this->cache[$key]);
}

/**
* {@inheritdoc}
*/
public function set($key, $value, $ttl = null)
public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool
{
$this->cache[$key] = $value;

Expand All @@ -102,7 +102,7 @@ public function set($key, $value, $ttl = null)
/**
* {@inheritdoc}
*/
public function setMultiple($values, $ttl = null)
public function setMultiple($values, $ttl = null): bool
{
foreach ($values as $key => $value) {
$this->set($key, $value);
Expand Down
Loading

0 comments on commit f3a3f15

Please sign in to comment.