Home >Backend Development >PHP Tutorial >PSR6 rationality discussion
I am now trying to write an implementation of PSR-6
When I was implementing the CacheItemPoolInterface
interface, I was confused about the save
method. The save
method only received one parameter of type CacheItemInterface
, but the CacheItemInterface
interface did not provide a method similar to getExpireTime
. This As a result, CacheItemPoolInterface
cannot obtain the expiration time of the cache item, and therefore cannot correctly write data to the cache storage.
I have seen several well-known PSR-6
implementations, such as symfony/cache php-cache/cache tedious/Stash. However, these implementation versions are not very elegant when implementing the save
method.
For examplesymfony/cache
<code>public function save(CacheItemInterface $item) { if (!$item instanceof CacheItem) { return false; } if ($this->deferred) { $this->commit(); } $this->deferred[$item->getKey()] = $item; return $this->commit(); }</code>
This save
method can only pass in SymfonyComponentCacheCacheItem
type parameters. Passing in other types will return false
.
The current situation is that if you want to write a class library that uses caching, you must specify a specific PSR-6
implementation as a dependency, not just psr/cache
. On the other hand, looking at the log interface PSR-3
, if you want to write a class library that requires logging functionality, you only need to introduce psr/log
, and no specific implementation is required.
In my opinion, PSR-6
does not need the CacheItemInterface
interface. You only need to modify the save
method to save($key, $value, $expire_at)
.
I searched for cache on packagist. Some other well-known cache libraries, such as doctrine/cache
sonata-project/cache
illuminate/cache
, etc., have not chosen to follow PSR-6
, which should be the same. Consider it
I am now trying to write an implementation of PSR-6
When I was implementing the CacheItemPoolInterface
interface, I was confused about the save
method. The save
method only received a parameter of type CacheItemInterface
, but the CacheItemInterface
interface did not provide a method similar to getExpireTime
. This As a result, CacheItemPoolInterface
cannot obtain the expiration time of the cache item, and therefore cannot correctly write data to the cache storage.
I have seen several well-known PSR-6
implementations, such as symfony/cache php-cache/cache tedious/Stash. However, these implementations are not very elegant when implementing the save
method.
For examplesymfony/cache
<code>public function save(CacheItemInterface $item) { if (!$item instanceof CacheItem) { return false; } if ($this->deferred) { $this->commit(); } $this->deferred[$item->getKey()] = $item; return $this->commit(); }</code>
This save
method can only pass in SymfonyComponentCacheCacheItem
type parameters. Passing in other types will return false
.
The current situation is that if you want to write a class library that uses caching, you must specify a specific PSR-6
implementation as a dependency, not just psr/cache
. On the other hand, looking at the log interface PSR-3
, if you want to write a class library that requires logging functionality, you only need to introduce psr/log
, and no specific implementation is required.
In my opinion, PSR-6
does not need the CacheItemInterface
interface. You only need to modify the save
method to save($key, $value, $expire_at)
.
I searched for cache on packagist, and some other well-known cache libraries, such as doctrine/cache
sonata-project/cache
illuminate/cache
, etc., did not choose to follow PSR-6
, which should be the same. Consider it