Skip to content

Instantly share code, notes, and snippets.

@progmars
Last active December 15, 2015 10:03
Show Gist options
  • Save progmars/960b848170ff4ecd580a to your computer and use it in GitHub Desktop.
Save progmars/960b848170ff4ecd580a to your computer and use it in GitHub Desktop.
For Laravel 5.0.27. Improvement to prevent writing session data even if it has not changed. Improvement to specify session metadata update frequency in session configuration instead of updating on every request. These both improvements will also reduce the risk for race conditions for concurrent requests.
Improvement to prevent writing session data even if it has not changed.
Improvement to specify session metadata update frequency in session configuration instead of updating on every request.
These both improvements will also reduce the risk for race conditions for concurrent requests.
Also, add 'metadata_update_threshold' => 1, to your config/session.php
diff --git a/vendor/laravel/framework/src/Illuminate/Session/EncryptedStore.php b/vendor/laravel/framework/src/Illuminate/Session/EncryptedStore.php
index edf34b8..0b5b763 100644
--- a/vendor/laravel/framework/src/Illuminate/Session/EncryptedStore.php
+++ b/vendor/laravel/framework/src/Illuminate/Session/EncryptedStore.php
@@ -20,13 +20,14 @@
* @param \SessionHandlerInterface $handler
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
* @param string|null $id
+ * @param int|0 $metaUpdateThreshold
* @return void
*/
- public function __construct($name, SessionHandlerInterface $handler, EncrypterContract $encrypter, $id = null)
+ public function __construct($name, SessionHandlerInterface $handler, EncrypterContract $encrypter, $id = null, $metaUpdateThreshold = 0)
{
$this->encrypter = $encrypter;
- parent::__construct($name, $handler, $id);
+ parent::__construct($name, $handler, $id, $metaUpdateThreshold);
}
diff --git a/vendor/laravel/framework/src/Illuminate/Session/SessionManager.php b/vendor/laravel/framework/src/Illuminate/Session/SessionManager.php
index b231598..d2167b2 100644
--- a/vendor/laravel/framework/src/Illuminate/Session/SessionManager.php
+++ b/vendor/laravel/framework/src/Illuminate/Session/SessionManager.php
@@ -162,15 +162,18 @@
*/
protected function buildSession($handler)
{
+ $metaUpdateThreshold =
+ $this->app['config']['session.metadata_update_threshold'] * 60;
+
if ($this->app['config']['session.encrypt'])
{
return new EncryptedStore(
- $this->app['config']['session.cookie'], $handler, $this->app['encrypter']
+ $this->app['config']['session.cookie'], $handler, $this->app['encrypter'], null, $metaUpdateThreshold
);
}
else
{
- return new Store($this->app['config']['session.cookie'], $handler);
+ return new Store($this->app['config']['session.cookie'], $handler, null, $metaUpdateThreshold);
}
}
diff --git a/vendor/laravel/framework/src/Illuminate/Session/Store.php b/vendor/laravel/framework/src/Illuminate/Session/Store.php
index 41a3593..ffbfa6a 100644
--- a/vendor/laravel/framework/src/Illuminate/Session/Store.php
+++ b/vendor/laravel/framework/src/Illuminate/Session/Store.php
@@ -29,6 +29,13 @@
*/
protected $attributes = array();
+ /**
+ * Raw session data to detect changes before storing.
+ *
+ * @var string
+ */
+ protected $rawInitialData;
+
/**
* The session bags.
*
@@ -70,14 +77,15 @@
* @param string $name
* @param \SessionHandlerInterface $handler
* @param string|null $id
+ * @param int|0 $metaUpdateThreshold
* @return void
*/
- public function __construct($name, SessionHandlerInterface $handler, $id = null)
+ public function __construct($name, SessionHandlerInterface $handler, $id = null, $metaUpdateThreshold = 0)
{
$this->setId($id);
$this->name = $name;
$this->handler = $handler;
- $this->metaBag = new MetadataBag;
+ $this->metaBag = new MetadataBag('_sf2_meta', $metaUpdateThreshold);
}
/**
@@ -117,10 +125,13 @@
protected function readFromHandler()
{
$data = $this->handler->read($this->getId());
-
+
if ($data)
- {
- $data = @unserialize($this->prepareForUnserialize($data));
+ {
+ $data = $this->prepareForUnserialize($data);
+ $this->rawInitialData = $data;
+
+ $data = @unserialize($data);
if ($data !== false) return $data;
}
@@ -252,8 +263,12 @@
$this->ageFlashData();
- $this->handler->write($this->getId(), $this->prepareForStorage(serialize($this->attributes)));
-
+ $serialized = serialize($this->attributes);
+
+ if($serialized !== $this->rawInitialData){
+ $this->handler->write($this->getId(), $this->prepareForStorage($serialized));
+ }
+
$this->started = false;
}
@progmars
Copy link
Author

Make sure you add

'metadata_update_threshold' => 1,

to your config/session.php for this patch to become effective.

The patch should be applied to vendor folder each time it gets recreated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment