Thinkphp Customizing the implementation of Redis processing session, thinkphpredis_php tutorial

Source: Internet
Author: User
Tags php template smarty template redis server

Thinkphp the implementation of a custom Redis processing session, Thinkphpredis


This paper describes the implementation of thinkphp custom Redis processing session. Share to everyone for your reference, as follows:

Everyday we will use to the session to save the user login information, commonly used to save the session: File saving (default), database storage, redis preservation, memcached and so on. Here is the main record of using thinkphp processing session with Redis to save the session usage.

1. Defined in the configuration item:

' Session_type ' = ' Redis ',//session save type ' session_prefix ' = ' sess_ ',//session prefix ' redis_host ' = ' 127.0.0.1 '// REDIS server address ' redis_port ' = 6379,//redis connection port number ' session_expire ' = 3600,//session expiration time

You can find the method that defines the session in the thinkphp/common/functions.php file, reading the session driver's judgment about 1179 rows or so. If we define the configuration item Session_type, a Redis object is new and the session store function Session_set_save_handler () is called.

2. Create a new Redis.class.php file in the Thinkphp\library\think\session\driver directory

The contents of the file are as follows:

<?phpnamespace think\session\driver;class Redis {//Redis Connection object private $redis;//Session Expiration time private $expire; /** * Open method * @param type $path * @param type $name * @return type */Public function open ($path, $name) {$thi S->expire = C (' Session_expire ')?  C (' Session_expire '): Ini_get (' session.gc_maxlifetime ');  $this->redis = new Redis ();  return $this->redis->connect (c (' Redis_host '), C (' Redis_port '));  }/** * Close * @return type */Public function close () {return $this->redis->close ();  }/** * Reads * @param string $id * @return Type */Public function read ($id) {$id = C (' Session_prefix '). $id;  $data = $this->redis->get ($id); Return $data?  $data: "; }/** * Write * @param string $id * @param type $data * @return Type * * Public function write ($id, $data) {$id = C (' Session_prefix ').  $id;  return $this->redis->set ($id, $data, $this->expire); }/** * destroys * @param string $id */Public functionDestroy ($id) {$id = C (' Session_prefix '). $id;  $this->redis->delete ($id);  }/** * Garbage collection * @param type $maxLifeTime * @return Boolean */Public Function GC ($MAXLIFETIME) {return true; }}

This completes the processing of the session by Redis.

Memcached is almost the same way as Redis!

Add: small make up here to recommend a site of the PHP format beautification of the layout tools to help you in the future of PHP programming code layout:

PHP Code online format beautification tool:Http://tools.jb51.net/code/phpformat

More interested in thinkphp related content readers can view this site topic: "thinkphp Introductory Tutorial", "thinkphp Common Methods Summary", "Smarty Template Primer Basic Tutorial" and "PHP template technology Summary."

It is hoped that this article is helpful to the PHP program design based on thinkphp framework.

http://www.bkjia.com/PHPjc/1127890.html www.bkjia.com true http://www.bkjia.com/PHPjc/1127890.html techarticle thinkphp The implementation method of the custom Redis processing session, Thinkphpredis the implementation method of thinkphp custom Redis processing session is described in this paper. Share to everyone for your reference, specific as ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.