Install and use the redis cache program in windows

Source: Internet
Author: User
Tags autoloader

Some time ago, the company needed to use redis to implement the cache function. Therefore, I wrote this article to share the installation and usage skills of redis.

First, download the redisserver and execute redis-server.exe. This process must be enabled all the time. We can also download a redis management tool: phpRedisAdmin

To facilitate the management of redis setting values, you need to pay attention to some specifications when setting their key values. For example, we have three systems running on the same server at the same time, and all of them use redis for caching, if one of the key values in the system is repeated, it will be overwritten by the later settings.

The following is a redis predis usage encapsulation class written by myself.

<? Php/*** implement redis client predis cache function * @ author bilehai@qq.com **/class Redis {private $ redis; // predis get object private $ statue; // predis status private $ time = 1000; // default expiration time public $ redis_start = FALSE; // whether to open an account Cache/*** initialize **/public function _ construct () {}/*** enable cache * @ param string **/public function redis_start ($ param = '') {$ this-> redis_start = TRUE; $ param = empty ($ param )? '': $ Param. ':'; if (file_exists ('predis/lib/predis/Autoloader. php ') {require_once ('predis/lib/predis/Autoloader. php '); Predis \ Autoloader: register (); $ this-> redis = new Predis \ Client ('', array ('prefix' => 'www .example.com :'. $ param) ;}else {return FALSE ;}} /*** common cache data ** @ param key input value parameter * @ param value input cache value parameter **/public function set ($ key, $ value) {if ($ this-> redis_start! = TRUE) {return FALSE;} if (empty ($ key) | empty ($ value) {return FALSE ;} return $ this-> redis-> set ($ key, $ value );} /*** set cache data ** @ param key input value parameter * @ param value input cache value parameter * @ param time set expiration time * @ return string **/public function setex ($ key, $ value, $ time = '') {if ($ this-> redis_start! = TRUE) {return FALSE;} if (empty ($ time) {$ time = $ this-> time;} if (empty ($ key) | empty ($ value) {return FALSE;} return $ this-> redis-> setex ($ key, $ time, $ value );} /*** get cache data ** @ param key cache key parameter * @ return string **/public function get ($ key) {if ($ this-> redis_start! = TRUE) {return FALSE;} if (empty ($ key) {return FALSE;} if ($ this-> redis-> exists ($ key )) {return $ this-> redis-> get ($ key);} else {return FALSE ;}}$ username =$ _ GET ['username']; $ redis = new Redis (); $ redis-> redis_start (); $ get_redis = $ redis-> get ('username'); if ($ get_redis = FALSE) {$ redis-> setex ('username', $ username);} var_dump ($ get_redis);?>

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.