Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A case study of using Redis to save user session Session

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail the case study of using Redis to save user session Session. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Preface

PHP's built-in painting storage mechanism is to save all session data in text files on the server, usually in a temporary directory on the server. If the concurrency is large, the reading and writing efficiency is very low. Redis supports high concurrency very well, so you can use redis instead of file storage session.

Download the source code

Session_set_save_handler function

PHP's session_set_save_handler function is used to define user-level session save functions (such as open, close, write, etc.). The prototype is as follows:

Bool session_set_save_hanler (callback open,callback close,callback read,callback write,callback destory,callback gc)

The parameter describes open to call this function when session is open. Two parameters are received, the first is the path that holds the session, and the second is the name of the session. This callback function operation successfully returns TRUE, otherwise it returns FALSE. Close calls this function when the session operation is complete. Parameters are not received. This callback function operation successfully returns TRUE, otherwise it returns FALSE. Read takes session ID as a parameter. Get the data from the data store through session ID and return this data. If the data is empty, you can return an empty string. This function is triggered by write before calling session_start when the data is stored. There are two parameters, one is session ID, and the other is the data destroy of session. The destroy function is triggered when the session_destroy function is called. There is only one parameter session ID, and this callback function operation successfully returns TRUE, and vice versa, FALSE. Gc is triggered when php executes the session garbage collection mechanism. This callback function operation successfully returns TRUE and vice versa.

Session management class

Find the configuration file php.ini for PHP, modify it to the following, and save and restart the php-fpm service.

Session.save_handler = redis

Let's write a class to manage session:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report