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.