Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can I use for quick temporary storage in PHP

Tags:

php

storage

I am creating a small plugin that process some images. Now to report the progress back to the user I have a small ajax script that will long poll the results back.

Now intern I need an object that keeps track of what is processed. Now the options I am aware of are the following.

Using the PHP session object. I cannot use this in this specific case, because the initial process is also done by ajax. So the main process is a ajax call, and the long poll ajax is another ajax call. They have 2 different session id's, so they do not communicate well.

Second option is to use the database as the storage. I do not know if this is so good because there will be around 40 read / writes on an average job. I know that this is no problem, but it seems a little much for something so simple.

What I actual look for is a sort of memory object if that is possible. Create a small object in the memory that is rapidly updated with the progress and deleted when we are done.

I do not know if that is possible, exists such a thing in PHP, and can I make use of this. Note that this will be a public plugin, so I need to work with methods that are available on all kind of systems, nothing special.

like image 980
Saif Bechan Avatar asked Dec 03 '11 21:12

Saif Bechan


People also ask

How can store temporary data in PHP?

The tmpfile() function creates a temporary file with a unique name in read-write (w+) mode. Note: The file is automatically removed when closed, with fclose() or when the script ends. Tip: See also the tempnam() function.

What is storage of data in PHP?

A data can be stored in a cookie that the user's browser will silently save. You can read a data in a session variable, which will allow us to identify a particular user at a given time. You can read the data written within a text file (txt, XML, etc.) existing on the server.


2 Answers

I think database is not the worse solution. If you think write in disk, maybe can be worse.

Memcache is good, but you need a "external plugins free" small plugin who runs easily on win, linux, mac, and so on... is not a good option.

If you use Mysql, you can use Memory engine tables, witch is fast, and truncate it or clean it periodically, with a simple garbage collector algoritm. And if memory table is not a option, innodb is good enough.

like image 161
Paulo H. Avatar answered Sep 21 '22 02:09

Paulo H.


You can use memcache for this. http://php.net/manual/en/book.memcache.php As key you can use a md5 hash of the image file plus the users ip.

like image 45
Oliver A. Avatar answered Sep 20 '22 02:09

Oliver A.