プロジェクト

全般

プロフィール

0001-refs-3835-Prepared-the-preset-configuration-for-usin.patch

Shinichi Urabe, 2015-09-02 20:04

ダウンロード (7.68 KB)

差分を表示:

config/OpenPNE.yml.sample
59 59
######################################
60 60

  
61 61
# セッションストレージ設定 (Configure Session storage)
62
#   name の値には、 file, database, memcache の他、任意のセッションストレージクラス名が使用できます
63
#   ("file", "database", "memcached" and any class names of session storage class as value of "name")
62
#   name の値には、 file, database, memcache, redis の他、任意のセッションストレージクラス名が使用できます
63
#   ("file", "database", "memcached", "redis" and any class names of session storage class as value of "name")
64 64
#   database を選択した場合、扱えるセッション ID の文字列長 128 までとなります。必要に応じて session.hash_function と session.hash_bits_per_character の値を調節してください
65 65
#   (If you use "database", you can handle 128 length session id. Tune session.hash_function and session.hash_bits_per_character for your needs)
66 66
session_storage:
lib/config/opProjectConfiguration.class.php
89 89
        'db_time_col' => 'time',
90 90
      ), (array)$options, $params));
91 91
    }
92
    elseif ('redis' === $name)
93
    {
94
      sfConfig::set('sf_factory_storage', 'opRedisSessionStorage');
95
      sfConfig::set('sf_factory_storage_parameters', array_merge((array)$options, $params));
96
    }
92 97
    elseif ('file' !== $name)
93 98
    {
94 99
      sfConfig::set('sf_factory_storage', $name);
lib/user/opRedisSessionStorage.class.php
1
<?php
2

  
3
/**
4
 * This file is part of the OpenPNE package.
5
 * (c) OpenPNE Project (http://www.openpne.jp/)
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file and the NOTICE file that were distributed with this source code.
9
 */
10

  
11
/**
12
 * opRedisSessionStorage
13
 *
14
 * @package    OpenPNE
15
 * @subpackage user
16
 * @author     Shinichi Urabe <urabe@tejimaya.com>
17
 */
18
class opRedisSessionStorage extends sfSessionStorage
19
{
20
  protected $redis = null;
21

  
22
  protected $defaults = array(
23
    'persistent' => true,
24
    'host'       => 'localhost',
25
    'port'       => 6379,
26
    'database'   => 0,
27
    'timeout'    => 60,
28
  );
29

  
30
  /**
31
   * Available options:
32
   *
33
   * - host:       The default host (default to localhost)
34
   * - port:       The port for the default server (default to 6379)
35
   * - database:   The default database (default to 0)
36
   * - persistent: true if the connection must be persistent, false otherwise (true by default)
37
   *
38
   * @param  array $options  An associative array of options
39
   *
40
   * @see sfSessionStorage
41
   */
42
  public function initialize($options = array())
43
  {
44
    $isAutoStart = true;
45
    if (isset($options['auto_start']))
46
    {
47
      $isAutoStart = $options['auto_start'];
48
    }
49
    // disable auto_start
50
    $options['auto_start'] = false;
51

  
52
    // initialize the parent
53
    parent::initialize($options);
54

  
55
    if (!class_exists('Redis'))
56
    {
57
      throw new sfInitializationException('You must have redis installed.');
58
    }
59

  
60
    $this->redis = new Redis;
61

  
62
    // use this object as the session handler
63
    session_set_save_handler(
64
      array($this, 'sessionOpen'),
65
      array($this, 'sessionClose'),
66
      array($this, 'sessionRead'),
67
      array($this, 'sessionWrite'),
68
      array($this, 'sessionDestroy'),
69
      array($this, 'sessionGC')
70
    );
71

  
72
    if ($isAutoStart && !parent::$sessionStarted)
73
    {
74
      // start our session
75
      session_start();
76
      parent::$sessionStarted = true;
77
    }
78
  }
79

  
80
  /**
81
   * Closes a session.
82
   *
83
   * @return boolean true, if the session was closed, otherwise false
84
   */
85
  public function sessionClose()
86
  {
87
    // do nothing
88
    return true;
89
  }
90

  
91
  /**
92
   * Opens a session.
93
   *
94
   * @param  string $path  (ignored)
95
   * @param  string $name  (ignored)
96
   * @throws sfInitializationException If unable to connect to the redis server or select to redis database.
97
   *
98
   * @return boolean true, if the session was opened, otherwise an exception is thrown
99
   */
100
  public function sessionOpen($path = null, $name = null)
101
  {
102
    $options = array_merge($this->defaults, $this->options);
103

  
104
    $method = $options['persistent'] ? 'pconnect' : 'connect';
105
    $host = $options['host'];
106
    $port = (int) $options['port'];
107
    $database = (int) $options['database'];
108
    $timeout = (int) $options['timeout'];
109

  
110
    if (!$this->redis->$method($host, $port, $timeout))
111
    {
112
      throw new sfInitializationException(sprintf('Unable to connect to the redis server (%s:%s).', $host, $port));
113
    }
114

  
115
    if (!$this->redis->select($database))
116
    {
117
      throw new sfInitializationException(sprintf('Unable to select to the redis database (number:%d).', $database));
118
    }
119

  
120
    return true;
121
  }
122

  
123
  /**
124
   * Destroys a session.
125
   *
126
   * @param  string $id  A session ID
127
   *
128
   * @return bool true, if the session was destroyed, otherwise an exception is thrown
129
   */
130
  public function sessionDestroy($id)
131
  {
132
    return $this->redis->delete($id);
133
  }
134

  
135
  /**
136
   * Cleans up old sessions.
137
   *
138
   * @param  int $lifetime  The lifetime of a session
139
   *
140
   * @return bool true, if old sessions have been cleaned, otherwise an exception is thrown
141
   */
142
  public function sessionGC($lifetime)
143
  {
144
    // do nothing
145
    return true;
146
  }
147

  
148
  /**
149
   * Reads a session.
150
   *
151
   * @param  string $id  A session ID
152
   *
153
   * @return string The session data if the session was read or created
154
   */
155
  public function sessionRead($id)
156
  {
157
    return (string)$this->redis->get($id);
158
  }
159

  
160
  /**
161
   * Writes session data.
162
   *
163
   * @param  string $id    A session ID
164
   * @param  string $data  A serialized chunk of session data
165
   *
166
   * @return bool true, if the session was written, otherwise an exception is thrown
167
   */
168
  public function sessionWrite($id, $data)
169
  {
170
    if (!$id || !$data)
171
    {
172
      return false;
173
    }
174

  
175
    $lifetime = ini_get('session.gc_maxlifetime');
176

  
177
    return $this->redis->setex($id, (int) $lifetime, $data);
178
  }
179

  
180
  /**
181
   * Regenerates id that represents this storage.
182
   *
183
   * @param  boolean $destroy Destroy session when regenerating?
184
   */
185
  public function regenerate($destroy = true)
186
  {
187
    if (self::$sessionIdRegenerated)
188
    {
189
      return false;
190
    }
191

  
192
    $currentId = session_id();
193

  
194
    parent::regenerate($destroy);
195

  
196
    $newId = session_id();
197
    $this->sessionRead($newId);
198

  
199
    return $this->sessionWrite($newId, $this->sessionRead($currentId));
200
  }
201

  
202
  /**
203
   * Executes the shutdown procedure.
204
   */
205
  public function shutdown()
206
  {
207
    parent::shutdown();
208
  }
209
}
0
-