This article is written for the WM2.1.4 version of a redis extension to use! If the basic knowledge of the WM framework is not clear coder, please visit the official website http://www.workerman.net/study;
We modify the application of the chat room, we only focus on the event.php file, we first introduce our own Redis class, the class file is available for download in the attachment.
In the event.php file, we introduce a well-written Redis extension class, and the code is as follows:
.... use \lib\rediscluster;require_once root_dir. '/lib/redis.php ';//define a global Redis resource handle $redis = NULL;
How are we going to use it?
In the OnMessage function, we initialize;
/** * When there is a message * @param int $client _id * @param string $message */public static function OnMessage ($client _id, $message) {global $redis; if (! ( $redis instanceof Redis) {$redis = Rediscluster::getinstance (); } .....
After doing this, you can use the $redis handle in your code business!
How to use:
Global $redis; $redis->get (' key ');
This approach, may not be the best solution, if you need a more optimized scheme, you can initialize the Redis connection at the start of Workerman, here I do not toss, if there is something wrong, please correct me.
This article is from the "happy Programming _ Endless" blog, please be sure to keep this source http://happyliu.blog.51cto.com/501986/1622254
Workerman2.0 Framework adds Redis support