新しいブログに移動しました。
9ensanのLifeHack
こちらの記事も必要に応じて新しいブログに移動させる予定です。
今後ともよろしくお願い致します。

HOME > PHP > 

CodeIgniterにmemcacheをsession使用するライブラリを作成しました

CodeIgniterにmemcacheをsession使用するライブラリを作成しました

  • 2009.06.05 (金) 18:08
  • PHP
  • ,

PHPのセッション管理にmemcacheを使用しようとしていて複数台のサーバーでmemcacheを利用しようとした場合に

php.iniの設定だけではどうやってもできなかった・・・。(本当は出来るのかもしれないのだけど・・・

 

なので、ライブラリを作成しました。

今回はCodeIgniter用のライブラリです。

よかったら使ってみてください。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * memcacheを利用したsessionライブラリ
 *
 * memcacheを利用したsession管理処理を行う
 *
 * @access public
 * @varsion 1.00
 * @sinse 2009/06/03
 */
class Memsession {
 
  /**
   * CodeIgniterインスタンス
   */
  var $CI = null;
 
  /**
   * memcacheインスタンス
   */
  var $memcache = null;
 
  /**
   * セッションキー
   */
   var $key = 'memsess_';
 
  /**
   * configファイル名
   */
   var $conf_name = 'memcache_config';
 
  /**
   *
   * コンストラクタ
   *
   * @access public
   * @return void
   */
  function __construct()
  {
    $this->CI = & get_instance();
    $this->CI->config->load($this->conf_name,true,true);
    $this->init_cache();
    session_set_save_handler(
      array(& $this, "open"),
      array(& $this, "close"),
      array(& $this, "read"),
      array(& $this, "write"),
      array(& $this, "destroy"),
      array(& $this, "gc")
    );
    session_start();
  }
 
  /**
   *
   * memcache初期化処理
   *
   */
  function init_cache()
  {
    if (!isset($this->memcache)) {
      $ret = false;
      $servers = $this->CI->config->item('servers', $this->conf_name);
      $this->memcache =& new Memcache();
      foreach ($servers as $server) {
        $parts = explode(':', $server);
        $host  = $parts[0];
        $port  = 11211;
        if (isset($parts[1])) {
          $port = $parts[1];
        }
        if ($this->memcache->addServer($host, $port)) {
          $ret = true;
        }
      }
      return $ret;
    }
    return true;
  }
 
  /**
   *
   * open 関数
   *
   * @param string 保存パス
   * @param string セッション名
   * @return bool
   */
  function open($save_path, $session_name)
  {
    $this->session_path = $save_path;
    $this->session_name = $session_name;
    return true;
  }
 
  /**
   *
   * close 関数
   *
   * @return bool
   */
  function close()
  {
    return true;
  }
 
  /**
   *
   * read 関数
   *
   * @param string セッションID
   * @return string
   */
  function read($id)
  {
    if ($sess_data = $this->memcache->get($this->key.$id)) {
      return $sess_data;
    } else {
      return '';
    }
  }
 
  /**
   *
   * write 関数
   *
   * @param string セッションID
   * @param string データ
   * @retrun bool
   */
  function write($id, $sess_data)
  {
    if (trim($id) == '') return true;
    $compress = $this->CI->config->item('compress', $this->conf_name);
    $expires  = ini_get("session.gc_maxlifetime");
    $ret = $this->memcache->set($this->key.$id, $sess_data, $compress, $expires);
    return $ret;
  }
 
  /**
   *
   * destroy 関数
   *
   * @param string セッションID
   * @return bool
   */
  function destroy($id)
  {
    $ret = $this->memcache->delete($this->key.$id);
    return $ret;
  }
 
  /**
   *
   * ガベージコレクタ
   *
   * @return bool
   */
  function gc($maxlifetime)
  {
    return true;
  }
 
  /**
   *
   * SESSIONへの値の設定
   *
   * @param string キー
   * @param string 値
   * @return void
   */
  function set($key, $value)
  {
    $_SESSION[$key] = $value;
  }
 
  /**
   *
   * SESSIONからの値の取得
   *
   * @param string キー
   * @return mixed
   */
  function get($key)
  {
    $ret = $_SESSION[$key];
    return $ret;
  }
 
}

 

 

コメント:2件

  1. 通りすがり

    ガベージコレクタの箇所は、これでは何も起きない気がしますが。
    ぶくぶく増えませんか?

    • gensan

      memcacheのexpireを設定してあるのでその期限を過ぎたデータに関してはmemcacheの機能で削除されるようにしていますよ。

      なので、増えないかと思っているのですが・・・。

コメント投稿フォーム
通りすがり への返信を入力してください。
ユーザー名(必須)
メールアドレス(必須)(非公開)
ホームページ
コメント

トラックバック:0件

この記事のトラックバックURL
http://blog.veryposi.info/programing/php/codeigniter-memcache-session-php/trackback/
お薦めのレンタルサーバー
広告
お薦めの書籍
HOME > PHP > 

CodeIgniterにmemcacheをsession使用するライブラリを作成しました