Workerman2.0 Framework adds Redis support

Source: Internet
Author: User

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

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.