duanchui1251 2012-09-29 09:15
浏览 66
已采纳

将php会话移动到redis。 有可能不会丢失现有的会话数据吗?

We're considering moving our php session data to redis.

The setup looks simple. Just set the following in php.ini and restart apache. It should be all set.:

session.save_handler = redis
session.save_path = "tcp://host1:6379"

If possible I would like our users not to notice the migration. Is it possible to move the session data to redis without losing any existing session data?

  • 写回答

5条回答 默认 最新

  • doushe2513 2012-10-03 15:01
    关注

    There is no out-of-the-box solution available right now for what you are asking, but writing a custom script for this task can actually be fairly simple.

    Esentially, phpredis stores session data in redis as strings with the following key name format: PHPREDIS_SESSION:$sessionid, where $sessionid is the php id of the session, the one retrievable via session_id(). The session data is "encoded" as a php-session serialized variable (which is a slightly different format to the common php serialize/unserialize, see session_encode).

    Now that we know this, there are two possibilities to migrate session data stored in files:

    • Iterate through every session file (the actual path is set at session.save_path in your php.ini), read the data and write it back to redis. The files themselves store the php-session serialized representation of the session data, which means the content can be copied as it is directly to redis, and the filenames have the following pattern: sess_$sessionid, where $sessionid is, you guessed it, the id you'll want to use for your redis keys.

    • Migrate the data progressively by staying with file based sessions for now, but populating redis in real time as session data is being used, until the amount of sessions stored in redis looks good enough to do the switch. This could be achieved by doing something like:

      $redis->set("PHPREDIS_SESSION:".session_id(), session_encode());

      Right before each script ends. This method may add a little bit of overhead depending on the amount of data in session and how session_encode works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧