Skip to content

Commit 079239d

Browse files
committed
Merge branch 'release/0.2.0'
2 parents 1f91995 + b6f7f97 commit 079239d

File tree

8 files changed

+108
-12
lines changed

8 files changed

+108
-12
lines changed

RELEASE_NOTES.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,24 @@ All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

55
## [Unreleased]
6-
No changes yet
6+
No unreleased changes yet
7+
8+
## [0.2.0]
9+
### Added
10+
- Interface for extracting raw cache value `ExtractableCacheValueInterface`
11+
12+
### Changed
13+
- `CacheItem::get()` method now ALWAYS returns `null` when `isHit()` returns false.
14+
- `CacheItemPool::save()` and `CacheItemPool::saveDeferred()` now supports additionally CacheItems that implement `ExtractableCacheValueInterface` with a fallback to previous implementation, that was relying on `CacheItem::get()`
15+
16+
## Fixed
17+
- Fixed version in `module.xml` file, as it was not matching one in composer.json.
718

819
## 0.1.0
920
### Added
1021
- PSR-6 Implementation
1122
- DI auto-wiring for CacheItemPool
1223
- DI auto-wiring for [EcomDev\CacheKey](https://github.com/EcomDev/CacheKey) library
1324

14-
[Unreleased]: https://github.com/EcomDev/magento-psr6-bridge/compare/0.1.0...HEAD
25+
[Unreleased]: https://github.com/EcomDev/magento-psr6-bridge/compare/0.2.0...HEAD
26+
[0.2.0]: https://github.com/EcomDev/magento-psr6-bridge/compare/0.1.0...0.2.0

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"ecomdev/phpspec-magento-di-adapter": "~0.1",
1919
"magento/zendframework1": "*"
2020
},
21-
"version": "0.1.0",
21+
"version": "0.2.0",
2222
"license": "OSL-3.0",
2323
"authors": [
2424
{

spec/Model/CacheItemPoolSpec.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,10 @@ function it_saves_item_without_expiration_extraction_interface(CacheItemInterfac
202202
$this->save($cacheItem)->shouldReturn(true);
203203
}
204204

205-
function it_saves_item_with_expiration_time(CacheItem $cacheItem)
205+
function it_saves_item_with_expiration_time_and_value_provided_from_another_method(CacheItem $cacheItem)
206206
{
207-
$cacheItem->get()->willReturn([1, 2, 3])->shouldBeCalled();
207+
$cacheItem->get()->shouldNotBeCalled();
208+
$cacheItem->getCacheValue()->willReturn([1, 2, 3])->shouldBeCalled();
208209
$cacheItem->getKey()->willReturn('key1')->shouldBeCalled();
209210
$cacheItem->getCacheLifetime()->willReturn(3600)->shouldBeCalled();
210211

spec/Model/CacheItemSpec.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
namespace spec\EcomDev\MagentoPsr6Bridge\Model;
1818

19+
use EcomDev\MagentoPsr6Bridge\ExtractableCacheLifetimeInterface;
20+
use EcomDev\MagentoPsr6Bridge\ExtractableCacheValueInterface;
1921
use Psr\Cache\CacheItemInterface;
2022
use PhpSpec\ObjectBehavior;
2123
use Prophecy\Argument;
@@ -32,6 +34,16 @@ function it_should_implement_cache_item_interface()
3234
$this->shouldImplement(CacheItemInterface::class);
3335
}
3436

37+
function it_should_implement_extractable_cache_lifetime_interface()
38+
{
39+
$this->shouldImplement(ExtractableCacheLifetimeInterface::class);
40+
}
41+
42+
function it_should_implement_extractable_cache_value_interface()
43+
{
44+
$this->shouldImplement(ExtractableCacheValueInterface::class);
45+
}
46+
3547
function it_returns_key_that_was_passed_in_constructor()
3648
{
3749
$this->getKey()->shouldReturn('dummy_key');
@@ -65,6 +77,26 @@ function it_is_possible_to_set_cache_value()
6577
$this->get()->shouldReturn('some_cache_value');
6678
}
6779

80+
function it_should_return_null_if_it_is_not_hit()
81+
{
82+
$this->beConstructedWith('key1', false);
83+
$this->set('some_cache_value')->shouldReturn($this);
84+
$this->get()->shouldReturn(null);
85+
}
86+
87+
function it_returns_same_value_that_was_set_before_via_special_method()
88+
{
89+
$this->beConstructedWith('key1', false);
90+
$this->set('some_cache_value')->shouldReturn($this);
91+
$this->getCacheValue()->shouldReturn('some_cache_value');
92+
}
93+
94+
function it_returns_same_value_that_was_passed_in_constructor_even_on_cache_miss()
95+
{
96+
$this->beConstructedWith('key1', false, 'some_cache_value');
97+
$this->getCacheValue()->shouldReturn('some_cache_value');
98+
}
99+
68100
function it_does_not_have_any_cache_lifetime_set_by_default()
69101
{
70102
$this->getCacheLifetime()->shouldReturn(null);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Magento PSR-6 Bridge
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Open Software License (OSL 3.0)
8+
* that is bundled with this package in the file LICENSE.txt.
9+
* It is also available through the world-wide-web at this URL:
10+
* https://opensource.org/licenses/osl-3.0.php
11+
*
12+
* @copyright Copyright (c) 2016 EcomDev BV (http://www.ecomdev.org)
13+
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
14+
* @author Ivan Chepurnyi <[email protected]>
15+
*/
16+
17+
namespace EcomDev\MagentoPsr6Bridge;
18+
19+
/**
20+
* Interface provides a possibility
21+
* to extract cache value from cache item
22+
*
23+
* In default PSR-6 implementation, there are some setters
24+
* but no retrieval of value after it was set
25+
*/
26+
interface ExtractableCacheValueInterface
27+
{
28+
/**
29+
* Returns cache value, that was set or loaded before
30+
*
31+
* @return mixed|null
32+
*/
33+
public function getCacheValue();
34+
}

src/Model/CacheItem.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
namespace EcomDev\MagentoPsr6Bridge\Model;
1818

19+
use EcomDev\MagentoPsr6Bridge\ExtractableCacheValueInterface;
1920
use Psr\Cache\CacheItemInterface;
2021
use EcomDev\MagentoPsr6Bridge\ExtractableCacheLifetimeInterface;
2122

@@ -28,7 +29,10 @@
2829
* you must explicitly call cache pool save()
2930
* or saveDeferred() on a cache pool
3031
*/
31-
class CacheItem implements CacheItemInterface, ExtractableCacheLifetimeInterface
32+
class CacheItem implements
33+
CacheItemInterface,
34+
ExtractableCacheLifetimeInterface,
35+
ExtractableCacheValueInterface
3236
{
3337
/**
3438
* Cache life time of the entry
@@ -69,9 +73,7 @@ public function __construct($key, $isHit, $value = null)
6973
{
7074
$this->key = $key;
7175
$this->isHit = $isHit;
72-
if ($this->isHit) {
73-
$this->value = $value;
74-
}
76+
$this->value = $value;
7577
}
7678

7779
/**
@@ -102,7 +104,7 @@ public function getKey()
102104
*/
103105
public function get()
104106
{
105-
return $this->value;
107+
return $this->isHit() ? $this->value : null;
106108
}
107109

108110
/**
@@ -196,4 +198,14 @@ public function getCacheLifetime()
196198
{
197199
return $this->cacheLifetime;
198200
}
201+
202+
/**
203+
* Returns cache value, that was set or loaded before
204+
*
205+
* @return mixed|null
206+
*/
207+
public function getCacheValue()
208+
{
209+
return $this->value;
210+
}
199211
}

src/Model/CacheItemPool.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
namespace EcomDev\MagentoPsr6Bridge\Model;
1818

19+
use EcomDev\MagentoPsr6Bridge\ExtractableCacheValueInterface;
1920
use Magento\Framework\Cache\FrontendInterface;
2021
use Psr\Cache\CacheItemInterface;
2122
use Psr\Cache\CacheItemPoolInterface;
@@ -255,7 +256,11 @@ public function save(CacheItemInterface $item)
255256
}
256257

257258
return $this->cacheFrontend->save(
258-
serialize($item->get()),
259+
serialize((
260+
$item instanceof ExtractableCacheValueInterface ?
261+
$item->getCacheValue() :
262+
$item->get()
263+
)),
259264
$this->prepareKey($item->getKey()),
260265
$this->tags,
261266
$expirationTime

src/etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
*/
1717
-->
1818
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
19-
<module name="EcomDev_MagentoPsr6Bridge" setup_version="1.0.0" />
19+
<module name="EcomDev_MagentoPsr6Bridge" setup_version="0.2.0" />
2020
</config>

0 commit comments

Comments
 (0)