Home >Backend Development >PHP Tutorial >PSR6 rationality discussion

PSR6 rationality discussion

WBOY
WBOYOriginal
2016-08-08 09:06:501008browse

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 CacheItemInterfaceinterface 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

Reply content:

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 CacheItemInterfaceinterface 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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn