在实际项目中,Redis大部分情况下应该放在控制器还是模型?
目前只做过两种方案:
控制器中:
大致的逻辑如下(不要纠结于方法名称):
class UserController extends Controller { public function index() { $userRedis = new UserRedis(); if (!$userInfo = $userRedis->find(['id' => 1000])) { $user = new User(); $userInfo = $user->find(['id' => 1000]); $userRedis->save($userInfo); } return $userInfo; } }
在这种情况下,模型还是独立存在的,数据库模型依然直接读取数据库,Redis模型读取Redis,两者互不影响,控制器从中协调。
模型:
class UserModel extends Model { public function find($conditions) { $userRedis = new UserRedis(); if (!$userInfo = $userRedis->find($conditions)) { $userInfo = $this->find($conditions); $userRedis->save($userInfo); } return $userInfo; } }
在这种情况下,控制器只需要调用一次接口方案,而无需关心内部实现,整个数据逻辑交给模型来处理。
在项目中实际用过Redis的朋友路过顺便分享一下自己的经验,大家集思广益一下。^_^
回复内容:
在实际项目中,Redis大部分情况下应该放在控制器还是模型?
目前只做过两种方案:
控制器中:
大致的逻辑如下(不要纠结于方法名称):
class UserController extends Controller { public function index() { $userRedis = new UserRedis(); if (!$userInfo = $userRedis->find(['id' => 1000])) { $user = new User(); $userInfo = $user->find(['id' => 1000]); $userRedis->save($userInfo); } return $userInfo; } }
在这种情况下,模型还是独立存在的,数据库模型依然直接读取数据库,Redis模型读取Redis,两者互不影响,控制器从中协调。
模型:
class UserModel extends Model { public function find($conditions) { $userRedis = new UserRedis(); if (!$userInfo = $userRedis->find($conditions)) { $userInfo = $this->find($conditions); $userRedis->save($userInfo); } return $userInfo; } }
在这种情况下,控制器只需要调用一次接口方案,而无需关心内部实现,整个数据逻辑交给模型来处理。
在项目中实际用过Redis的朋友路过顺便分享一下自己的经验,大家集思广益一下。^_^
优先模型层个人觉得似乎是比较好一点的方法,不论是维护还是逻辑
一般都放数据模型层吧
一般还是说放到模型层,然后封装一个方法,业务层在调用的时候,直接去调取方法就好了,而不用想着再去做缓存的事情。因为缓存在模型层都帮忙给做了。
只要跟数据读写有关的,还是放模型层比较合适
是我我就放模型层了,控制层控制逻辑流程和返回响应,逻辑复杂了看上去不会乱
放 Model 层。
原则:尽量屏蔽具体存储介质的差异。
Mysql、redis 对于项目都只是存储数据的,在代码里面应该不要钱解决存哪,提供统一的调用方式, UserMysql.find()
、UserRedis.find()
;
或者 User.find()
,User.findFromRedis()
默认调用从 mysql 读取,redis 操作折提供其他的函数
这个也是 ORM 的思想。
这取决于你的Model是否就等于数据库。
因为从你的$this->find来看,似乎是直接把Model的$this当做了数据库?
有两种选择:
不要把Model本身直接定义为数据库,而在Model里用redis和db类分别操作。如果redis和db里的操作都很少很简单,可以选择这样。
把Model定位为数据操作层,叫UserDbModel(对应你现在的UserModel),再定义另一个Model叫UserRedisModel(对应你现在的$userRedis),让两者地位平等,同时在Model层上加一层Logic层,处理缓存与数据库的关系,Controller变为只能调用Logic层。这种方式是我现在在用的,对复杂的逻辑而言可以显得更清晰。

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment