Skip to content

Commit 212e0a4

Browse files
StephenClouseNyholm
authored andcommitted
Fix issues with binary data being stored (#188)
* Ignore binary integration tests for Void adapter * Add common trait for armoring binary data in JSON * Armor binary data in MongoDB adapter * Armor binary data in encryption layer * Update IntegrationSimpleCacheTest.php
1 parent 775046c commit 212e0a4

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

MongoDBCachePool.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Cache\Adapter\MongoDB;
1313

1414
use Cache\Adapter\Common\AbstractCachePool;
15+
use Cache\Adapter\Common\JsonBinaryArmoring;
1516
use Cache\Adapter\Common\PhpCacheItem;
1617
use Cache\Adapter\Common\TagSupportWithArray;
1718
use MongoDB\Collection;
@@ -23,6 +24,7 @@
2324
*/
2425
class MongoDBCachePool extends AbstractCachePool
2526
{
27+
use JsonBinaryArmoring;
2628
use TagSupportWithArray;
2729

2830
/**
@@ -70,7 +72,12 @@ protected function fetchObjectFromCache($key)
7072
}
7173
}
7274

73-
return [true, unserialize($object->data), unserialize($object->tags), $object->expirationTimestamp];
75+
return [
76+
true,
77+
$this->thawValue($object->data),
78+
$this->thawValue($object->tags),
79+
$object->expirationTimestamp,
80+
];
7481
}
7582

7683
/**
@@ -100,8 +107,8 @@ protected function storeItemInCache(PhpCacheItem $item, $ttl)
100107
{
101108
$object = [
102109
'_id' => $item->getKey(),
103-
'data' => serialize($item->get()),
104-
'tags' => serialize($item->getTags()),
110+
'data' => $this->freezeValue($item->get()),
111+
'tags' => $this->freezeValue($item->getTags()),
105112
'expirationTimestamp' => $item->getExpirationTimestamp(),
106113
];
107114

@@ -124,7 +131,7 @@ public function getDirectValue($name)
124131
return;
125132
}
126133

127-
return unserialize($object->data);
134+
return $this->thawValue($object->data);
128135
}
129136

130137
/**
@@ -134,9 +141,19 @@ public function setDirectValue($name, $value)
134141
{
135142
$object = [
136143
'_id' => $name,
137-
'data' => serialize($value),
144+
'data' => $this->freezeValue($value),
138145
];
139146

140147
$this->collection->updateOne(['_id' => $name], ['$set' => $object], ['upsert' => true]);
141148
}
149+
150+
private function freezeValue($value)
151+
{
152+
return static::jsonArmor(serialize($value));
153+
}
154+
155+
private function thawValue($value)
156+
{
157+
return unserialize(static::jsonDeArmor($value));
158+
}
142159
}

0 commit comments

Comments
 (0)