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

Skip to content

Commit defdc50

Browse files
author
Aleksey Prilipko
committed
remove PhpExportSerialzer, add OpcacheTrait
1 parent c662c60 commit defdc50

File tree

8 files changed

+107
-202
lines changed

8 files changed

+107
-202
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.

src/Symfony/Component/Cache/Tests/Simple/PhpArrayCacheTest.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Symfony\Component\Cache\Simple\NullCache;
1515
use Symfony\Component\Cache\Simple\PhpArrayCache;
1616
use Symfony\Component\Cache\Tests\Adapter\FilesystemAdapterTest;
17-
use Symfony\Component\Cache\Tests\Fixtures\Exportable;
1817

1918
/**
2019
* @group time-sensitive
@@ -93,27 +92,6 @@ public function testStore()
9392
$this->assertEquals($value, $cache->get($key), 'Warm up should create a PHP file that OPCache can load in memory');
9493
}
9594
}
96-
97-
public function testStoredFile()
98-
{
99-
$expected = array(
100-
'integer' => 42,
101-
'float' => 42.42,
102-
'boolean' => true,
103-
'array_simple' => array('foo', 'bar'),
104-
'array_associative' => array('foo' => 'bar', 'foo2' => 'bar2'),
105-
'exportable_object' => new Exportable('baz'),
106-
);
107-
108-
$cache = new PhpArrayCache(self::$file, new NullCache());
109-
$cache->warmUp($expected);
110-
111-
$values = eval(substr(file_get_contents(self::$file), 6));
112-
113-
foreach ($expected as $key => $value) {
114-
$this->assertEquals($value, $cache->get($key), 'Warm up should create a PHP file that OPCache can load in memory');
115-
}
116-
}
11795
}
11896

11997
class PhpArrayCacheWrapper extends PhpArrayCache
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
*/

0 commit comments

Comments
 (0)
0