Thinkphp5 framework extension redis class method example


In this paper, an example is given to show how the thinkphp5 framework extends the redis class method. To share with you for your reference, as follows:

During the development, the author found that the redis class method of thinkphp5 has only a simple basic method of reading and writing cache, which is far from meeting our business needs. Redis itself supports five data types: string, hash, list, set and Zset. There is no need to introduce redis. Next, let me expand the redis method of thinkphp5!

First, we find the,handler()Method, which returns the instance object of redis class.

This is the instantiation object, including options configuration item, handler resource handle, tag tag tag. Actually, the handler here isnew Redis()Object instance of. As long as you get the handler resource handle externally, you can extend the redis operation.

Add a gethandler method to the cache class.

Then we can call the

This is the redis object printed

Here we need to pay attention to:

One is to adjust redis of thinkphp5, which is how to use it.

One is if you want to call the native redis method

The basic usage is over.

Let’s start to extend redis method happily.

Find this class first

There is no list related operation method in this class. Let’s extend the list related method.

Here’s an example.

Call in the controller

The write is successful.

For more information about ThinkPHP, readers who are interested in it can see the following topics: introduction to ThinkPHP, summary of operation skills of ThinkPHP template, summary of common methods of ThinkPHP, introduction to CodeIgniter, advanced tutorial of CI (CodeIgniter) framework, introduction to Zend framework and technical summary of PHP template.

I hope that this article will be helpful for you to design your PHP program based on the think PHP framework.