8000 remove PhpExportSerialer, add OpcacheTrait · symfony/symfony@a4b045f · GitHub
[go: up one dir, main page]

Skip to content

Commit a4b045f

Browse files
author
Aleksey Prilipko
committed
remove PhpExportSerialer, add OpcacheTrait
1 parent c662c60 commit a4b045f

File tree

7 files changed

+107
-180
lines changed

7 files changed

+107
-180
lines changed

src/Symfony/Component/Cache/Serializer/PhpExportSerializer.php

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/Symfony/Component/Cache/Tests/Adapter/PhpArrayAdapterTest.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -99,26 +99,6 @@ public function testStore()
9999
$this->assertEquals($value, $adapter->getItem($key)->get(), 'Warm up should create a PHP file that OPCache can load in memory');
100100
}
101101
}
102-
103-
public function testStoredFile()
104-
{
105-
$expected = array(
106-
'integer' => 42,
107-
'float' => 42.42,
108-
'boolean' => true,
109-
'array_simple' => array('foo', 'bar'),
110-
'array_associative' => array('foo' => 'bar', 'foo2' => 'bar2'),
111-
);
112-
113-
$adapter = $this->createCachePool();
114-
$adapter->warmUp($expected);
115-
116-
$values = eval(substr(file_get_contents(self::$file), 6));
117-
118-
foreach ($expected as $key => $value) {
119-
$this->assertEquals($value, $adapter->getItem($key)->get(), 'Warm up should create a PHP file that OPCache can load in memory');
120-
}
121-
}
122102
}
123103

124104
class PhpArrayAdapterWrapper extends PhpArrayAdapter

src/Symfony/Component/Cache/Tests/Serializer/PhpExportSerializerTest.php

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Traits;
13+
14+
/**
15+
* @author Alexei Prilipko <palex.fpt@gmail.com>
16+
*
17+
* @internal
18+
*/
19+
trait OpcacheTrait
20+
{
21+
abstract protected function serialize($data);
22+
23+
abstract protected function unserialize($serialized);
24+
25+
/**
26+
* Prepare value to be written into php file. So it can be used by opcache.
27+
* Non-exportable objects and arrays containing non-exportable objects is serialized and stored as [true, $serialized]
28+
* Nulls and arrays is enveloped as [false, $value]
29+
* Scalar values and exportable objects stored as is.
30+
*
31+
* @param $value
32+
* @return string
33+
*/
34+
private function wrapValue($value)
35+
{
36+
if (self::isExportable($value)) {
37+
$exportedValue = var_export($value, true);
38+
if (is_null($value) || is_array($value)) {
39+
$wrappedValue = 'array(false, '.$exportedValue.')';
40+
} else {
41+
$wrappedValue = $exportedValue;
42+
}
43+
} else {
44+
$wrappedValue = 'array(true, '.var_export($this->serialize($value), true).')';
45+
}
46+
47+
return $wrappedValue;
48+
}
49+
50+
/**
51+
* Value loaded from opcache is
52+
* either: [false, $arrayOrNull]
53+
* or: [true, $serializedString]
54+
* or: $nonNullnonArrayValue
55+
*
56+
* On first cache hit put deserialized value directly into array or envelope it.
57+
*
58+
* @return mixed
59+
*/
60+
private function unwrapValue(&$envelope)
61+
{
62+
if (is_array($envelope)) {
63+
if (false === $envelope[0]) {
64+
return $envelope[1];
65+
}
66+
67+
$value = $this->unserialize($envelope[1]);
68+
if (is_null($value) || is_array($value)) {
69+
$envelope = array(false, $value);
70+
} else {
71+
$envelope = $value;
72+
}
73+
74+
return $value;
75+
}
76+
77+
return $envelope;
78+
}
79+
80+
private static function isExportable($data)
81+
{
82+
if (is_object($data)) {
83+
return method_exists(get_class($data), '__set_state');
84+
}
85+
86+
if (is_array($data)) {
87+
$exportable = true;
88+
foreach ($data as $key => $value) {
89+
$exportable = $exportable && self::isExportable($key) && self::isExportable($value);
90+
}
91+
92+
return $exportable;
93+
}
94+
95+
return true;
96+
}
97+
}

src/Symfony/Component/Cache/Traits/PhpArrayTrait.php

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\Cache\CacheItem;
1515
use Symfony\Component\Cache\Exception\InvalidArgumentException;
16-
use Symfony\Component\Cache\Serializer\PhpExportSerializer;
1716

1817
/**
1918
* @author Titouan Galopin <galopintitouan@gmail.com>
@@ -25,6 +24,7 @@ trait PhpArrayTrait
2524
{
2625
use ProxyTrait;
2726
use SerializerTrait;
27+
use OpcacheTrait;
2828

2929
private $file;
3030
/** @var array of value containers */
@@ -68,22 +68,11 @@ public function warmUp(array $values)
6868

6969
EOF;
7070

71-
$exportSerializer = new PhpExportSerializer();
72-
7371
foreach ($values as $key => $value) {
7472
CacheItem::validateKey(\is_int($key) ? (string) $key : $key);
7573

7674
try {
77-
try {
78-
$exportedValue = $exportSerializer->serialize($value);
79-
if (is_null($value) || is_array($value)) {
80-
$valuePart = 'array(false, '.$exportedValue.')';
81-
} else {
82-
$valuePart = $exportedValue;
83-
}
84-
} catch (InvalidArgumentException $e) {
85-
$valuePart = 'array(true, '.var_export($this->serialize($value), true).')';
86-
}
75+
$valuePart = $this->wrapValue($value);
8776
} catch (\Exception $e) {
8877
throw new InvalidArgumentException(sprintf('Cache key "%s" %s', $key, $e->getMessage()), 0, $e);
8978
}
@@ -99,6 +88,7 @@ public function warmUp(array $values)
9988
$tmpFile = uniqid($this->file, true);
10089

10190
file_put_contents($tmpFile, $dump);
91+
@touch($tmpFile, time() - 10);
10292
@chmod($tmpFile, 0666 & ~umask());
10393
unset($serialized, $unserialized, $value, $dump);
10494

@@ -107,36 +97,6 @@ public function warmUp(array $values)
10797
$this->initialize();
10898
}
10999

110-
/**
111-
* Value loaded from opcache is
112-
* either: [false, $arrayOrNull]
113-
* or: [true, $serializedString]
114-
* or: $nonNullnonArrayValue
115-
*
116-
* On first cache hit put deserialized value directly into array or envelope it.
117-
*
118-
* @return array|mixed
119-
*/
120-
private function unwrapValue(&$envelope)
121-
{
122-
if (is_array($envelope)) {
123-
if (false === $envelope[0]) {
124-
return $envelope[1];
125-
}
126-
127-
$value = $this->unserialize($envelope[1]);
128-
if (is_null($value) || is_array($value)) {
129-
$envelope = array(false, $value);
130-
} else {
131-
$envelope = $value;
132-
}
133-
134-
return $value;
135-
}
136-
137-
return $envelope;
138-
}
139-
140100
/**
141101
* {@inheritdoc}
142102
*/

src/Symfony/Component/Cache/Traits/PhpFilesTrait.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
trait PhpFilesTrait
2424
{
2525
use FilesystemCommonTrait;
26+
use OpcacheTrait;
2627

2728
private $includeHandler;
2829
private $zendDetectUnicode;
@@ -77,7 +78,8 @@ protected function doFetch(array $ids)
7778
foreach ($ids as $id) {
7879
try {
7980
$file = $this->getFile($id);
80-
list($expiresAt, $values[$id]) = include $file;
81+
list($expiresAt, $envelope) = include $file;
82+
$values[$id] = $this->unwrapValue($envelope);
8183
if ($now >= $expiresAt) {
8284
unset($values[$id]);
8385
}
@@ -92,10 +94,6 @@ protected function doFetch(array $ids)
9294
}
9395
}
9496

95-
foreach ($values as $id => $value) {
96-
$values[$id] = $this->unserialize($value);
97-
}
98-
9997
return $values;
10098
}
10199

@@ -113,13 +111,13 @@ protected function doHave($id)
113111
protected function doSave(array $values, $lifetime)
114112
{
115113
$ok = true;
116-
$data = array($lifetime ? time() + $lifetime : PHP_INT_MAX, '');
114+
$dataLifetime = $lifetime ? time() + $lifetime : PHP_INT_MAX;
117115
$allowCompile = 'cli' !== PHP_SAPI || ini_get('opcache.enable_cli');
118116

119117
foreach ($values as $key => $value) {
120-
$data[1] = $this->serialize($value);
118+
$valuePart = $this->wrapValue($value);
121119
$file = $this->getFile($key, true);
122-
$ok = $this->write($file, '<?php return '.var_export($data, true).';') && $ok;
120+
$ok = $this->write($file, '<?php return array('.var_export($dataLifetime, true).','.$valuePart.');') && $ok;
123121

124122
if ($allowCompile) {
125123
@opcache_invalidate($file, true);

src/Symfony/Component/Cache/Traits/SerializerTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ trait SerializerTrait
2323
/** @var SerializerInterface */
2424
private $serializer;
2525

26-
protected function setSerializer(SerializerInterface $serializer)
26+
public function setSerializer(SerializerInterface $serializer)
2727
{
2828
$this->serializer = $serializer;
2929
}

0 commit comments

Comments
 (0)
0