Possible file_put_contents conflict? [duplicate] - php

This question already has answers here:
PHP multiple clients accessing same file
(2 answers)
Multiple users write to the same file at the same time using PHP
(4 answers)
Closed 1 year ago.
I am posting this not about code error but about possible conflict using this piece of code:
$logFile = 'data.log';
$result = (int) file_get_contents($logFile) + 1;
file_put_contents($logFile, $result);
This will read a file called data.log and get it's number and write this number +1. But, I will run this application with many users calling this in realtime (like 100 users per second).
So, I want to know if this will create some conflict, get outupdated number and store wrong data or something like this. Is it possible? Are there an way to I avoid it?
Thank you.

It will not work correctly because the data is not changed "atomically".
You can solve it by locking the file before accessing it - read about the flock function. But this could affect performance and possibly seriously limit your throughput - so you should benchmark it.
Generally speaking, this is not considered a "good" way to implement a counter. Look into using a proper database like MySQL or a NoSQL like MongoDB.

Related

Efficient PHP Caching Strategy? [duplicate]

This question already has answers here:
Caching strategy, when does caching become pointless?
(3 answers)
Closed 9 years ago.
I am kinda new in the application development. I am developing one application at the moment and I want to know your valueable inputs about a problem that I am facing as a newb.
When it comes to caching, which parts of the application I should add in cache? Let me give you an example of what I am doing:
I am developing a picture sharing application for my users and I have created a cache mechanism to cache every user's data, his albums & his pictures. I currently have 1 query per page (only in case that it is not already stored in Cache) for example:
if ( !$ItemNotInCache )
{
$MyData = $DB->Query('blah');
Cache::CacheIt($MyData);
}
This is about every result I am getting, I am trying not to call too many queries every time a user requests a page, is this efficient? The problem is that as you can understand I had to build this huge mechanism to manipulate all this data in arrays, etc..
When a user deletes an image from DB, i have to remove the index from the cached data of his album as well.
When a user adds an image to DB, I use array_merge to insert this item to his cached data and so on.
A friend of mine told me that I shouldn't abuse the cache since my data is dynamic and it will change very often and he also told me this is why MySQL is for. My question is I should use the cache in that way or no? As you can understand the data is dynamic and it should be refreshed often. Is this efficient or i should just stick with MySQL queries?
I think what you are doing is perfectly fine. Retrieving from the cache uses less resources in almost all the cases as opposed to doing a query each time.
However, I wouldn't try to "merge" the added image, just call the database again. Just increment the cache when the album modified. I don't think editing the cache when the album is modified is the right approach. Databases are good at storing and building the data so it's easy to be retrieved. Why do the building yourself?

PHPexcel export what are the best ways to handle large export [duplicate]

This question already has answers here:
Alternative for PHP_excel
(2 answers)
Closed 9 years ago.
Currently I am working on survey related application where the number of participant and number of survey it will vary depends on the organization.
As of now I am doing export through the browsers. Currently the participant and survey data are less so the export works fine. But when the application grows bigger based on the server and php setting it will export or it will hang still.
Please let me know the best possible ways to handle this kind of scenario.
Thanks
Some tips:
You can try to do the export in a cronjob, so you don't have any limits on browser timeout.
Increase the memory and execution time in php.ini (or by ini_set)
Make use of caching tables, first collect data and put them in a temp/caching table. Where the structure of the table is close the structure of the excel. So you have only receive the data from the temp/caching table and write them to a excel

When to use views and when to use stored procedures [duplicate]

This question already has answers here:
MySQL: Views vs Stored Procedures
(4 answers)
Closed 9 years ago.
I am a application where number of reports are more. What i do for each report is that i create a mysql view and a mysql stored procedure. From front end php i give a call to stored procedure with where clause, based on this where clause i fetch results from the particular view. Recently i found out that it was causing performance issue. So i avoided views and wrote the same code in stored procedure and performance improved. So from that poit i am confused as to ideal situation when i should use Stored procedure and when i should use views.
And Does my scenario explained above really cause performance issue or was it problem at my end?
Views in mysql are mainly for readability. They enable you to hide a possibly complex query over multiple tables into something that appears to be a single table.
I would suspect that the most likely cause is not the use of views themselves (although not sure they would help you in any way, while a stored procedure might well be more efficient), rather a view you are using is poorly optimised (maybe ignoring keys).

Speed/Memory Issue with PHP [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Least memory intensive way to read a file in PHP
I have a problem with speed vs. memory usage.
I have a script which needs to be able to run very quickly. All it does is load multiple files from 1-100MB, consisting of a list of values and checks how many of these exist against another list.
My preferred way of doing this is to load the values from the file into an array (explode), and then loop through this array and check whether the value exists or not using isset.
The problem I have is that there are too many values, it uses up >10GB of memory (I don't know why it uses so much). So I have resorted to loading the values from the file into memory a few at a time, instead of just exploding the whole file. This cuts the memory usage right down, but is VERY slow.
Is there a better method?
Code Example:
$check=array('lots','of','values','here');
$check=array_flip($check);
$values=explode('|',file_get_contents('bigfile.txt'));
$matches=0;
foreach($values as $key)
if (isset($check[$key])) $matches++;
Maybe you could code your own C extension of PHP (see e.g. this question), or code a small utility program in C and have PHP run it (perhaps using popen)?
These seems like a classic solution for some form of Key/Value orientated NoSQL datastore (mongodb, couchdb, Riak) (or maybe even just a large memcache instance).
Assuming you can load the large data files into the datastore ahead of when you need to do the searching and that you'll be using the data from the loaded files more than once, you should see some impressive gains (as long your queries, mapreduce, etc aren't awful), judging by the size of your data you may want to look at a data store which doesn't need to hold everything in memory to be quick.
There are plenty of PHP drivers (and tutorials) for each of the datastores I mentioned above.
Open the files and read through them line wise. Maybe use MySQL, for import (LOAD DATA INFILE), for resulting data or both.
It seems you need some improved search engine.
Sphinx search server can be used for searching your values really fast.

MYSQL function or PHP function, which one is better? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Performing simple arithmetic in MySQL statement or in PHP code
Im wondering what is the best to use, from point of view performance:
PHP functions (to calculate a date in the future) and to input the date in the DB query
MYSQL function (to calculate a date in the future) e.g. TIMESTAMPADD(DAY,3,UTC_DATE()) directly inside the query
If you're manipulating dates in a table, it's definitely better to use MySql functions. This is particularly true if the mySql server might be accessed by different hosts (which might not have their clocks all synchronized with each other).

Categories