PHP+Redis caching technology overview [steps]
有否想过PHP使用redis
作为缓存时,如何能:
1.前后台模块共用Model层;
2.但是,不能每个Model类都进行缓存,这样太浪费Redis资源;
3.前后台模块可以自由决定从数据库还是从缓存读数据;
4.没有冗余代码;
5.使用方便。
这里我们先展示实现的最终效果。
最终的代码和使用说明请移步Github:https://github.com/yeszao/php-redis-cache。
马上安装使用命令:
$ composer install yeszao/cache
经过简单配置就可以使用,请参看Github的README说明。
1 最终效果
假设在MVC框架中,model
层有一个Book
类和一个getById
方法,如下:
class Book { public function getById($id) { return $id; } }
加入缓存技术之后,原来方法的调用方式和返回的数据结构都不应该改变。
所以,我们希望,最后的效果应该是这样的:
(new Book)->getById(100); // 原始的、不用缓存的调用方式,还是原来的方式,一般是读取数据库的数据。 (new Book)->getByIdCache(100); // 使用缓存的调用方式,缓存键名为:app_models_book:getbyid: + md5(参数列表) (new Book)->getByIdClear(100); // 删除这个缓存 (new Book)->getByIdFlush(); // 删除 getById() 方法对应的所有缓存,即删除 app_models_book:getbyid:*。这个方法不需要参数。
这样我们可以很清楚的明白自己在做什么,同时又知道数据的来源函数,并且被引用方式完全统一,可谓一箭三雕。
其实实现起来也比较简单,就是使用PHP的魔术方法__call()方法。
2 __call()方法
这里简单说明一下__call
方法的作用。
在PHP中,当我们访问一个不存在的类方法时,就会调用这个类的__call()
方法。
(如果类方法不存在,又没有写__call()
方法,PHP会直接报错)
假设我们有一个Book
类:
class Book { public function __call($name, $arguments) { echo '类Book不存在方法', $name, PHP_EOL; } public function getById($id) { echo '我的ID是', $id, PHP_EOL; } }
当调用存在的getName(50)
方法时,程序打印:我的ID是50。
而如果调用不存在的getAge()
方法时,程序就会执行到A类的__call()
方法里面,这里会打印:类Book不存在方法getAge。
这就是__call
的原理。
3 实现细节
接下来我们就利用__call()
方法的这种特性,来实现缓存策略。
从上面的例子,我们看到,__call()
方法被调用时,会传入两个参数。
$name
:想要调用的方法名$arguments
:参数列表
我们就可以在参数上面做文章。
还是以Book
类为例,我们假设其原本结构如下:
class Book { public function __call($name, $arguments) { // 待填充内容 } public function getById($id) { return ['id' => $id, 'title' => 'PHP缓存技术' . $id]; } }
开始之前,我们还确认Redis的连接,这是缓存必须用到的,这里我们写个简单的单例类:
class Common { private static $redis = null; public static function redis() { if (self::$redis === null) { self::$redis = new \Redis('127.0.0.1'); self::$redis->connect('redis'); } return self::$redis; }
然后,我们开始填充__call()
方法代码,具体说明请看注释:
class Book { public function __call($name, $arguments) { // 因为我们主要是根据方法名的后缀决定具体操作, // 所以如果传入的 $name 长度小于5,可以直接报错 if (strlen($name) < 5) { exit('Method does not exist.'); } // 接着,我们截取 $name,获取原方法和要执行的动作, // 是cache、clear还是flush,这里我们取了个巧,动作 // 的名称都是5个字符,这样截取就非常高效。 $method = substr($name, 0, -5); $action = substr($name, -5); // 当前调用的类名称,包括命名空间的名称 $class = get_class(); // 生成缓存键名,$arguments稍后再加上 $key = sprintf('%s:%s:', str_replace('\\', '_', $class), $method); // 都用小写好看点 $key = strtolower($key); switch ($action) { case 'Cache': // 缓存键名加上$arguments $key = $key . md5(json_encode($arguments)); // 从Redis中读取数据 $data = Common::redis()->get($key); // 如果Redis中有数据 if ($data !== false) { $decodeData = json_decode($data, JSON_UNESCAPED_UNICODE); // 如果不是JSON格式的数据,直接返回,否则返回json解析后的数据 return $decodeData === null ? $data : $decodeData; } // 如果Redis中没有数据则继续往下执行 // 如果原方法不存在 if (method_exists($this, $method) === false) { exit('Method does not exist.'); } // 调用原方法获取数据 $data = call_user_func_array([$this, $method], $arguments); // 保存数据到Redis中以便下次使用 Common::redis()->set($key, json_encode($data), 3600); // 结束执行并返回数据 return $data; break; case 'Clear': // 缓存键名加上$arguments $key = $key . md5(json_encode($arguments)); return Common::redis()->del($key); break; case 'Flush': $key = $key . '*'; // 获取所有符合 $class:$method:* 规则的缓存键名 $keys = Common::redis()->keys($key); return Common::redis()->del($keys); break; default: exit('Method does not exist.'); } } // 其他方法 }
这样就实现了我们开始时的效果。
4 实际使用时
在实际使用中,我们需要做一些改变,把这一段代码归入一个类中,
然后在model层的基类中引用这个类,再传入Redis句柄、类对象、方法名和参数,
这样可以降低代码的耦合,使用起来也更灵活。
完整的代码已经放在Github上,请参考文章开头的参考地址。
推荐学习:《PHP视频教程》
The above is the detailed content of PHP+Redis caching technology overview [steps]. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
