The implementation method of thinkphp custom Redis processing session _php Instance

Source: Internet
Author: User
Tags garbage collection memcached php programming php template redis smarty template redis server

The example of this article describes the implementation of thinkphp custom Redis processing session. Share to everyone for your reference, specific as follows:

Daily we will use the session to save the user login information, commonly used to save the session: File Save (default), database preservation, Redis save, memcached and so on. Here the main record in the use of thinkphp processing session with Redis to save the session usage.

1. Define in Configuration items:

' 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 a way to define a session in a thinkphp/common/functions.php file and read the session-driven judgment around 1179 lines. If we define the configuration item Session_type, we will new a Redis object and invoke the session store function Session_set_save_handler ().

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

The contents of the document are as follows:

<?php namespace 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) { $this->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 closed () {return $this->redis->close ();  /** * Read * @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); }
  /**
   *Destroy * @param string $id/Public function Destroy ($id) {$id = C (' Session_prefix '). $id;
  $this->redis->delete ($id); /** * Garbage Collection * @param type $maxLifeTime * @return Boolean/Public Function GC ($maxLifeTime) {return T
  Rue

 }
}

This completes the Redis to the session processing.

Memcached's approach is almost the same as Redis!

Add: Small knitting here recommend a site for the layout of the PHP format landscaping tools to help you in the future of PHP programming code layout:

PHP Code online Format Landscaping tool:Http://tools.jb51.net/code/phpformat

More interested in thinkphp related content readers can view the site topics: "thinkphp Introductory Course", "thinkphp Common Methods Summary", "Smarty Template Primer" and "PHP template technology Summary."

I hope this article will help you with the PHP program design based on thinkphp framework.

Related Article

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.