I have got to learn about Memcache recently. I have a project in PHP Codeigniter. So, what I have understood memcache does is, it stores the result of database query, and if the result is needed again, it provides the result immediately instead of running the query again which makes the application fast. So I tried to implement memcache. Then I explored that I have to manually store a cache for every query. My application has hundreds of query function, it is almost impossible for me to implement memcache for each on of them.
Is there any way by which all the queries will be stored to memcache and when a result will be needed, it will provide data from memcache and if not available in memcache then run the database query and store it to memcache for further use?
I don't know if I have been able to make you understand what I exactly want, please ask me in reply if anything seems unclear.
Thanks in advance 😊
You mean you want a middleware for applying to specific route sets? If so, CodeIgniter provides hooks system https://www.codeigniter.com/user_guide/general/hooks.html (Similar to Laravel's middleware). After performing your query functions, you can easily implement a caching mechanism via hooks. But you need to find a middleware/hook(or build from scratch) for your solving specific problem. Hope I could help you.
Related
Within a Laravel application I have a messenger polling system. With this I'm guessing that every time a user makes a request all dependencies get loaded with every request. Not really a feature I'm very fond of seeing the load it potentially creates.
Is there a way to circumvent most of Laravel requirements to run.
I really only want to insert a message in the database and select a return response and return it as JSON. So I'm not in need of any views, controllers or modules and want to keep those as clean as possible. I actually don't even want to use the PDO and definitely not the query builder.
I'm guessing it saves a lot of server power just to go from the
Route::post(... function({}));
Can anybody confirm?
Alot of Laravel components are "lazy loaded" - so they only actually load when they are needed.
Is there a way to circumvent most of Laravel requirements to run. I really only want to insert a message in the database and select a return response and return it as JSON. So I'm not in need of any views, controllers or modules and want to keep those as clean as possible. I actually don't even want to use the PDO and definitely not the query builder.
To me it seems you want to not use most of the Laravel framework. So if I was you - dont use the Laravel framework. Just use the Request component by itself, or even use the underlying Symphony component.
However, as a side point, it seems like you are trying to optimise something where you dont even know you need to optimise it in the first place. Focus on writing good quality, testable, maitainable code.
The other option is a 'light' framework like Silex
I recently started working with Yii PHP MVC Framework. I'm looking for advice on how should I continue working with the database through the framework: should I use framework's base class CActiveRecord which deals with the DB, or should I go with the classic SQL query functions (in my case mssql)?
Obviously or not, for me it seems easier to deal with the DB through classic SQL queries, but, at some point, I imagine there has to be an advantage in using framework's way.
Some SQL queries will get pretty complex pretty often. I just can't comprehend how the framework could help me and not make things more complicated than they actually are.
Very General rule from my experience with Yii and massive databases:
Use Yii Active Record when:
You want to retrieve and post single to a few rows in the database (e.g. user changing his/her settings, updating users balance, adding a vote, getting a count of users online, getting the number of posts under a topic, checking if a model exists)
You want to rapidly design a hierarchical model structure between your tables, (e.g. $user->info->email,$user->settings->currency) allowing you to quickly adjust displayed currency/settings per use.
Stay away from Yii Active Record when:
You want to update several 100 records at a time. (too much overhead for the model)
Yii::app()->db->command()
allows you to avoid the heavy objects and retrieves data in simple arrays.
You want to do advanced joins and queries that involve multiple tables.
Any batch job!! (e.g. checking a payments table to see which customers are overdue on their payments, updating database values etc.)
I love Yii Active Record, but I interchange between the Active Record Model and plain SQL (using Yii::app()->db) based on the requirement in the application.
At the end I have the option whether I want to update a single users currency
$user->info->currency = 'USD';
$user->info->save();
or if I want to update all users currencies:
Yii::app()->db->command('UPDATE ..... SET Currency="USD" where ...');
In any language when dealing with the database a framework can help you by providing an abstraction over the database.
Here is a scenario I know I found myself in many times during my earlier development days:
I have an application that needs a database.
I write a ton of code.
I put the SQL statements in the code along with everything else.
The database changes somehow.
I'm stuck with having to go back and make 100 changes to all my SQL statements.
It's very frustrating.
Another scenario I found:
I write a ton of code against a database.
Bugs come in. Lots of bugs. I can't figure them all out.
I'm asked to write tests for my code.
This is impossible because all my code relies on a direct implementation of the database. How do you test SQL statements when they're with the actual code?
So my advice is to use the framework because it can provide an abstraction over the database. This gives you two really big advantages:
You can potentially swap out the database later and your code stays the same! If you're using interfaces/some framework, then most likely you're dealing with objects and not SQL statements directly. A given implementation might know how to write to MySQL or SQL Server, but in general your code just says "Write this object", "Read that list."
You can test your code! A good framework that deals with data will let you mock the database so you can test it easily.
Try to avoid writing SQL statements directly in the application. It'll save you pain later.
I'm unfamiliar with the database system bundled with Yii, but would advise you to use it a little bit to start with. My experience is with Propel, a popular PHP ORM. In general, ORM systems have a class per table (Propel has three per table).
Now, there'll probably be a syntax to do lookups and joins etc, but the first thing to do is to work out how to use raw SQL in your queries (for any of the CRUD operations). Put methods to do these queries in your model classes, so at least you will be benefitting from centralisation of code.
Once you've got that working, you can migrate to the recommended approach at a later time, without getting overwhelmed with the amount of material you have to learn in one go. Learning Yii (especially how to share code amongst controllers, and to write maintainable view templates) takes a while, so it may be sensible not to over-complicate it with many other things as well.
Why to use Yii:
Just imagine that you have many modules and for each module you have to write a pagination code; writing in old fashion style, will need a lot of time;
Why not use Yii ClistView widget? Oh, and this widget comes with a bonus: the data provider and the auto checking for the existance of the article that is about to be printed;
When using Yii CListView with results from ... Sphinx search engine, the widget will check if the article do really exists, because the result may not be correct
How long will it take for you to write a detection code for non existing registration?
And when you have different types of projects will you addapt the methods?
NO! Yii does this for you.
How long would it take for you to write the code in crud style ? create, read, update, delete ?
Are you going to adapt the old code from another project ?
Yii has a miracle module, called Gii, that generates models, modules, forms, controllers, the crud ... and many more
at first it might seem hard, but when you get experienced, it's easy
I would suggest you should use CActiveRecord.It will give many advantages -
You can use many widgets within yii directly as mentioned above.(For paginations,grids etc)
The queries which are generated by the Yii ORM are highly optimized.
You dont need to put the results extracted from SQLs in your VO objects.
If the tables for some reason modified(addition/deletion of column,changing data type), you just need to regenerate the models using the tool provided by yii.Just make sure you try to avoid doing any code changes in the models generated by yii, that will save your merging efforts.
If you plan to change the DB from MYSQL to other vendor in futur, it would be just config change for you.
Also you and your team would save your precious development time.
So i am starting to make this new app that will be a software as a service (SAAS), I have been looking at MongoDB but after reading some posts i get the impression that its not stable where you have to run the repair command alot and its easy to lose data.
So with the release of 2.0 are these issues still about?
To note: the app is not a forum but it will do simler sort of things that the database is needed for like users, posts, and others sorts of information, is a NoSQL the right type of DB for me or should i just go with MySQL? its also coded in PHP.
I think you should write "simple" "helloworld" application(blog with user registration) with Mongo.
It's very different from relational DB, so you will see the difference yourself.
I have two servers:
Server A: MySql
Table A
key-a
foreign-key-b
Server B: MsSql
Table B
key-b
foreign-key-a
Presumably I have two objects with methods that handle the relationships:
TableA->getRelatedTableB();
TableB->getRelatedTableA();
This is easy to implement in most ORMs. But what if I want to get a large set of objects with only one query per database server? Ideally the framework would abstract this and do the logical join so that the developer can pretend he doesn't know anything about the database(s). Something like:
FinderObject->getAlotOfTableAObjectsWithTableBAlreadyLoaded()
and it would perform a query on each database and logically join the results in some efficient manner.
Does anyone know of a way to implement this in Doctrine or some other php ORM framework?
Doctrine doesn't explicitly support cross-database joins, but there is a way to do it:
http://www.doctrine-project.org/blog/cross-database-joins
One solution for this is to use federated tables, but I've read that this hasn't a good performance. All depends in how you need to use it.
I don't know of any that do... BUT maybe you could use Propel, Memcached, and MySQL together.
Setup a distributed memory cache using Memcached, and see if there's a way to store some MySQL data in there. Because Memcached is distributed, both of your MySQL servers could store data there. Then you'd have to find a way to access that memory (via Memory Tables?).
Seems a very tricky situation.
Perhaps the problem is being approached from the wrong direction. Could you tell us what problem you're trying to solve? There might be a completely different (and simpler!) solution just around the corner.
I'm creating a movies website, IMDB.com like.. I'm really new to PHP and programming at all but I have some books and StackOverflow of course :)
I have already done lot of the work, but now I have more than 600 lines of code (PHO only) per page and more than 20 database tables only for storing and geting the movie data (many-to-many relationships)
Most of the code are MySQLi queries (prepared statements) to insert/get data and loops. Nothing fancy, just basic PHP/MySQL.
I have some questions:
It is recommended to use MySQLi prepared statements for every SQL query or is better just to use old MySQL for internal PHP/database queries and leave prepared statements for user input only?
To get all the movie data and show it I need to get the data from more than 16 different tables. I use one MySQL query per table (somethimes more) and lots of loops. This is the correct way a website need to work?? I mean.. this is normal???
How can I simplify the code to simplify the reading? Can I separete all those queries to external files?? Can I create functions to handle each table query??
Hope you can give me a hand and maybe you have some more suggestions for correctly building such a website.
Thanks!!
As mentioned in the anwsers above I would also point you to using a framework that impliments the MVC design pattern. Along with that most of the frameworks have an ORM built in but if they do not you can look at Symphony or EZPDO is another good ORM to tie into your Model in (M)VC.
CodeIgniter is a very fast and light weight MVC framework that would allow you to bootstrap pretty quickly but you may also want to look into ZF (ZendFramework). ZF has a great framework feature set and is pretty flexible overall.
Beyond that be sure to seperate your reads and your writes in either or Model or your calls to the ORM of choice. This will allow you to slave data to multiple MySQL Boxes for bigger performance but allows you to use one database engine to start out.
Add in the ability to use Memcached so that you can cache your data/objects vs hitting the database.
When using cache put some thought into how you would expire cache for a database update. In other words if your selecting data from a database to display in a view and that data is has not changed you should not need to hit the database every time but rather pull it from memory. Once the data actually does change you'd want to invalidate that cache so it's not stale and then re-cache the new data.
memcached: http://www.danga.com/memcached/'
-facebook also has a version
CodeIgniter - http://codeigniter.com/
EZPDO - http://www.ezpdo.net/blog/?p=2
ZendFramework -http://framework.zend.com/
Consider looking at or using a web framework for your website.
symfony
CakePHP
CodeIgniter
Are some of the more mainstream ones. Learn from them if anything.
Prepared statements are just fine for your own internal queries to. You'll have a strutured approach for all queries.
Well it depends on what you're showing. But I would say that you normally could use joins to get the data you need from more tables. You'll have a lot less quering to get the correct data, and it sounds like all your data is connected somehow to the one movie you're showing.
As Peter D comments, I would recommend using a web framework to learn how to seperate out the database handling from the view. Are you using an object oriented approach now? Look at the MVC pattern that some of these frameworks implement, that'll get you going.
Like Peter D mention before add this one to the list of framework to use. Zend Framework http://framework.zend.com
Open source and free.
It is recommended to use MySQLi ...
Definitely MySQLi, but it's a big question by itself, if you start coding you'll need to grasp the basic of T-SQL to understand the difference.
To get all the movie data ...
It depends on alot of things. Size of database. Wanted results i.e. the information that need to be displayed, response time of the queries vs displaying in user view. Do you know about JOIN, UNION?
How can I simplify the code to ...
Yes to all theses questions. www.w3schools.com/php/ if it can be of any help and learn the MVC pattern. Useful to alot of programming language these days. Maybe a framework would help you here
To this questions:
"To get all the movie data and show it
I need to get the data from more than
16 different tables. I use one MySQL
query per table (somethimes more) and
lots of loops. This is the correct way
a website need to work?? I mean.. this
is normal???"
No. If I understand you correctly, you should be using some type of JOIN depending on the data you're retrieving from the database. Getting results for huge amounts of data, and then picking out only the pieces you want in PHP is much slower than letting the database do the work of sorting/retrieving only the records/info you want to show.
I highly recommend a somewhat dated but very easy to grasp book that covers PHP and MySQL/Databases in general: http://www.dmcinsights.com/phpmysql2/ - It covers a lot of "in practice" techniques along with the code, so it'd be great to learn from.
There is a apparently a third edition with updated info, but I have not looked at it.