Переглянути джерело

Implement Predis\Session\SessionHandler.

This class makes it easy to use Predis to store PHP sessions on Redis.

NOTE: requires PHP >= 5.4.0 or a polyfill for PHP's SessionHandlerInterface.
Daniele Alessandri 12 роки тому
батько
коміт
3e125c964c
3 змінених файлів з 184 додано та 0 видалено
  1. 4 0
      CHANGELOG.md
  2. 39 0
      examples/SessionHandler.php
  3. 141 0
      lib/Predis/Session/SessionHandler.php

+ 4 - 0
CHANGELOG.md

@@ -1,6 +1,10 @@
 v0.8.2 (2013-xx-xx)
 ===============================================================================
 
+- Added `Predis\Session\SessionHandler` to make it easy to store PHP sessions
+  on Redis using Predis. Please note that this class needs either PHP >= 5.4.0
+  or a polyfill for PHP's `SessionHandlerInterface`.
+
 - Added the ability to get the default value of a client option directly from
   `Predis\Option\ClientOption` using the `getDefault()` method by passing the
   option name or its instance.

+ 39 - 0
examples/SessionHandler.php

@@ -0,0 +1,39 @@
+<?php
+
+require 'SharedConfigurations.php';
+
+// This example demonstrates how to leverage Predis to save PHP sessions on Redis.
+//
+// The value of `session.gc_maxlifetime` in `php.ini` will be used by default as the
+// the TTL for keys holding session data on Redis, but this value can be overridden
+// when creating the session handler instance with the `gc_maxlifetime` option.
+//
+// Note that this class needs PHP >= 5.4 but can be used on PHP 5.3 if a polyfill for
+// SessionHandlerInterface (see http://www.php.net/class.sessionhandlerinterface.php)
+// is provided either by you or an external package like `symfony/http-foundation`.
+
+if (!interface_exists('SessionHandlerInterface')) {
+    die("ATTENTION: the session handler implemented by Predis needs PHP >= 5.4.0 or a polyfill ".
+        "for \SessionHandlerInterface either provided by you or an external package.\n");
+}
+
+// Instantiate a new client just like you would normally do. We'll prefix our session keys here.
+$client = new Predis\Client($single_server, array('prefix' => 'sessions:'));
+
+// Set `gc_maxlifetime` so that a session will be expired after 5 seconds since last access.
+$handler = new Predis\Session\SessionHandler($client, array('gc_maxlifetime' => 5));
+
+// Register our session handler (it uses `session_set_save_handler()` internally).
+$handler->register();
+
+// Set a fixed session ID just for the sake of our example.
+session_id('example_session_id');
+
+session_start();
+
+if (isset($_SESSION['foo'])) {
+    echo "Session has `foo` set to {$_SESSION['foo']}\n";
+} else {
+    $_SESSION['foo'] = $value = mt_rand();
+    echo "Empty session, `foo` has been set with $value\n";
+}

+ 141 - 0
lib/Predis/Session/SessionHandler.php

@@ -0,0 +1,141 @@
+<?php
+
+/*
+ * This file is part of the Predis package.
+ *
+ * (c) Daniele Alessandri <suppakilla@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Predis\Session;
+
+use SessionHandlerInterface;
+use Predis\ClientInterface;
+
+/**
+ * Session handler class that relies on Predis\Client to store PHP's sessions
+ * data into one or multiple Redis servers.
+ *
+ * This class is mostly intended for PHP 5.4 but it can be used under PHP 5.3 provided
+ * that a polyfill for `SessionHandlerInterface` is defined by either you or an external
+ * package such as `symfony/http-foundation`.
+ *
+ * @author Daniele Alessandri <suppakilla@gmail.com>
+ */
+class SessionHandler implements SessionHandlerInterface
+{
+    protected $client;
+    protected $ttl;
+
+    /**
+     * @param ClientInterface $client Fully initialized client instance.
+     * @param array $options Session handler options.
+     */
+    public function __construct(ClientInterface $client, Array $options = array())
+    {
+        $this->client = $client;
+        $this->ttl = (int) (isset($options['gc_maxlifetime']) ? $options['gc_maxlifetime'] : ini_get('session.gc_maxlifetime'));
+    }
+
+    /**
+     * Registers the handler instance as the current session handler.
+     */
+    public function register()
+    {
+        if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
+            session_set_save_handler($this, true);
+        } else {
+            session_set_save_handler(
+                array($this, 'open'),
+                array($this, 'close'),
+                array($this, 'read'),
+                array($this, 'write'),
+                array($this, 'destroy'),
+                array($this, 'gc')
+            );
+        }
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function open($save_path, $session_id)
+    {
+        // NOOP
+
+        return true;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function close()
+    {
+        // NOOP
+
+        return true;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function gc($maxlifetime)
+    {
+        // NOOP
+
+        return true;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function read($session_id)
+    {
+        if ($data = $this->client->get($session_id)) {
+            return $data;
+        }
+
+        return '';
+    }
+    /**
+     * {@inheritdoc}
+     */
+    public function write($session_id, $session_data)
+    {
+        $this->client->setex($session_id, $this->ttl, $session_data);
+
+        return true;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function destroy($session_id)
+    {
+        $this->client->del($session_id);
+
+        return true;
+    }
+
+    /**
+     * Returns the underlying client instance.
+     *
+     * @return ClientInterface
+     */
+    public function getClient()
+    {
+        return $this->client;
+    }
+
+    /**
+     * Returns the session max lifetime value.
+     *
+     * @return int
+     */
+    public function getMaxLifeTime()
+    {
+        return $this->ttl;
+    }
+}