Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use and encapsulate Redis in ThinkPHP5 Framework

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use and encapsulate Redis in ThinkPHP5 framework". In daily operation, I believe many people have doubts about how to use and encapsulate Redis in ThinkPHP5 framework. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about how to use and encapsulate Redis in ThinkPHP5 framework. Next, please follow the editor to study!

Redis is a commonly used non-relational database, which is mainly used as data cache. The data is saved in the form of key-value, and the keys and values are mapped to each other. Its data storage is different from MySQL, its data is stored in memory, so data reading is relatively fast, which is very good for high concurrency.

ThinkPhP5.0 comes with a Redis extension, so download php_redis.dll before using it. According to my own windows operating system, I choose the appropriate version. I am 64-bit and install VC2012, so I download php_redis-2.2.7-5.6-ts-vc11-x64.zip.

After downloading the compressed package, extract the php_redis.dll to D:\ wamp\ bin\ php\ php5.6.25\ ext (choose according to the disk where your wamp resides), then add extension=php_redis.dll to php.ini and restart apache.

The following is my own test code, can be used, not much encapsulation, can be packaged according to their own needs

Extend is the extended class library directory of thinkPHP5.0, which can be defined by yourself.

Namespace My; / / directory I put it in thinkphp5.0/extend/My class RedisPackage {protected static $handler = null Protected $options = ['host' = >' 127.0.0.1', 'port' = > 6379,' password' = >'', 'select' = > 0,' timeout' = > 0, / / shutdown time 0: does not close 'expire' = > 0,' persistent' = > false, 'prefix' = >',] Public function _ _ construct ($options = []) {if (! extension_loaded ('redis')) {/ / determine whether there is an extension (this exception will be thrown if your apache does not have a reids extension) throw new\ BadFunctionCallException (' not support: redis');} if (! empty ($options)) {$this- > options = array_merge ($this- > options, $options) } $func = $this- > options ['persistent']? 'pconnect':' connect'; / / determine whether persistent connection self::$handler = new\ Redis; self::$handler- > $func ($this- > options ['host'], $this- > options [' port'], $this- > options ['timeout']); if ('! = $this- > options ['password']) {self::$handler- > auth ($this- > options [' password']) } if (0! = $this- > options ['select']) {self::$handler- > select ($this- > options [' select']) }} / * write cache * @ param string $key key name * @ param string $value key * @ param int $exprie expiration time 0: never expire * @ return bool * / public static function set ($key, $value, $exprie = 0) {if ($exprie = = 0) {$set = self::$handler- > set ($key, $value) } else {$set = self::$handler- > setex ($key, $exprie, $value);} return $set;} / * * read cache * @ param string $key key * @ return mixed * / public static function get ($key) {$fun = is_array ($key)? 'Mget':' get'; return self::$handler- > {$fun} ($key);} / * * get the value length * @ param string $key * @ return int * / public static function lLen ($key) {return self::$handler- > lLen ($key) } / * insert one or more values into the header of the list * @ param $key * @ param $value * @ return int * / public static function LPush ($key, $value, $value2 = null, $valueN = null) {return self::$handler- > lPush ($key, $value, $value2, $valueN) } / * remove and get the first element of the list * @ param string $key * @ return string * / public static function lPop ($key) {return self::$handler- > lPop ($key);}} namespace app\ index\ controller;use think\ Controller;use My\ RedisPackage;class Redis extends Controller {function redis () {$redis=new RedisPackage () $redis::set ('dede',' I will smile'); echo $redis::get ('dede');}}

At this point, the study on "how to use and encapsulate Redis in the ThinkPHP5 framework" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report