/    Sign up×
Community /Pin to ProfileBookmark

PHP data structure from memory

hi

i want to use a data structure in PHP as static for different client calls.

i’ll explain this:

lets say i have an array A. Now what i want is that A gets updated for every client side call as static.
If User1 has sent some data i want to store it in A , next time another user User2 sends some data, i want to append it to A and not like a fresh call. So, essentially, i want to make use of a data strucutre that sticks in the server memory and used as global, universal contant for any call from any browser

another explanation to throw light on what i want is this:
lets say i have this piece of code in my server PHP script:
$count;
$count++
echo $count;

now i want that everytime this script is called my output should be
1 and then 2 and then 3 and so on
and not 1 and 1 and 1 and so on..

PS: I dont want to use a database to implement above issue, I WANT TO USE SERVER MEMORY like say a C++ server… pls pitch in with your comments asap… thx in advance

to post a comment
PHP

18 Comments(s)

Copy linkTweet thisAlerts:
@pcthugAug 20.2006 — PHP lacks such structure, the closest equivalent would be a temporary cache: [url=http://www.php.net/session]Session[/url]. I believe sessions could be used as a substitute, however they lack scalability as they are a restricted and impermanent. Therefore, unless all calls to this data incrementation were to be made by one user/browser within one session, you would need to use a database surrogate to store the collated data?
Copy linkTweet thisAlerts:
@chazzyAug 20.2006 — you could use the reference operator.... &
Copy linkTweet thisAlerts:
@gupta_vishaalauthorAug 21.2006 — how about using 'shared memory' for this.... i mean , i can write code inside the script for putting that data structure in the shared memory segment cos i believe shared memory is used in case of IPC and out-of-process cases..

what do you think ??

PS: btw, how can '&' be used... reference will still be in the process space..
Copy linkTweet thisAlerts:
@QwickAug 21.2006 — gupta,

The best solution would be to use a database. If you do not have database hosting, SQLite is a very lightweight file-based database that is easilly accessed in PHP.

There is no significant benefit to using server memory over a database.. it will not affect performance and you will spend a lot more development time on writing locking schemes. If you require such high performance, I would suggest storing the database on a ram drive or moving to a language that supports such a feature (such as Java).

Using the reference operator will not solve your problem as you do not have shared memory between scripts.

Session ids have to be known by all clients who wish to access the data source so this may not be what you are looking for. (also: they are simply a serialization of data in files -- so they are a very inefficient and poor locking database)

It is possible to create memory from an extension and reference it.. but this only works for Apache running PHP in threads rather than processes (default in Apache2). This is a lot more work than it's worth. Since you will have to store the pointer to the data structure in a file to access it across scripts -- and it's simply not safe. This is very true in the case where the webserver gets restarted as you will likely crash from trying to reference a pointer to memory you do not have access to read.
Copy linkTweet thisAlerts:
@gupta_vishaalauthorAug 21.2006 — thanks...

but im concerned about the performance and turnaround time...

i want to manage a custom data structure for a Web2.0 site which shows its data on the client side in real time thru AJAX. Also, in real time , this is updated on and off and shud show data quickly on client as the user types some stuff. In that case, would it be advisable to use a database....

is there no way in PHP whereby you can attain server like functionality across multiple sessions without using a database...
Copy linkTweet thisAlerts:
@chazzyAug 21.2006 — if that is your requirement, then php is not the solution for you. i would say take a look at .NET or Java.
Copy linkTweet thisAlerts:
@QwickAug 21.2006 — Switching languages is one option but I would try using a database. The SQLite database engine is compiled in your code so it runs very fast. If this is still too slow, then the AJAX portion of the web application may have to be moved to Java or .NET... but that application can query the database you have created and have results in-memory and ready for AJAX.

Other database engines such as MySQL have pooled connections and store most data in-memory for optimized access times. They can index columns for improved search times and know about complex sorting and joining operations. Remember databases were used with real time systems long before web applications existed.

If you properly use a database, you should be able to get in and out in way under a 1/4 second. It may actually be faster than trying to manage your own data structures. The very first time someone accesses the database may take a second or two. This is the same for starting up a Java web application for the first time after deployment.

Only optimize if there really is an issue.
Copy linkTweet thisAlerts:
@NogDogAug 21.2006 — [url=http://www.php.net/manual/en/ref.shmop.php]Shared Memory Functions[/url]
Copy linkTweet thisAlerts:
@chazzyAug 21.2006 — NogDog:

My understanding was that shmop would only work as long the memory being accessed was instantiated somewhere. if no references to it existed, it could be overwritten... is this right or wrong?

Qwick:

Typically, issues like this have to do with cache content. if you're constantly going to a database, it's a big drag. if you have data that can change, but not too often, then it's senseless to go to the database each time and parse the data. it is much smarter to use memory functions (java - rmi, .net has built in memory support, C/C++ via references, and PHP via shmop..sort of..) in this case than it is to go to a database each and everytime.
Copy linkTweet thisAlerts:
@NogDogAug 21.2006 — I've never worked with it, just noticed those functions while looking for something else so figured I'd point them out in case they helped.
Copy linkTweet thisAlerts:
@chazzyAug 21.2006 — oh, hmm.

Either way then, if you are going to try it with shmop, watch out for possible memory overwriting.
Copy linkTweet thisAlerts:
@bokehAug 21.2006 — Two things:

You will loose the data if the server machine is restarted and

these function must specifically be enabled when building PHP which probably means they are not.

Maybe it will cost you a fraction of a second but I think storing the data in a file would be a better plan.
Copy linkTweet thisAlerts:
@gupta_vishaalauthorAug 22.2006 — thx folks

i think the best bet will be to write a benchmark test to compare shared memory vs database turnaround times for a small scenario... And then decide whats best...anyhow database will come into picture b'cos memory data needs a backup .. like bokeh says for the case what if web server goes down.


meanwhile, ill get back to writing this test suite ?
Copy linkTweet thisAlerts:
@chazzyAug 22.2006 — you should have a daemon of sorts running...something that puts the data in memory, this way if the server goes down, you just start up the daemon again and have it read in the data.
Copy linkTweet thisAlerts:
@bokehAug 22.2006 — i think the best bet will be to write a benchmark test to compare shared memory vs database turnaround times for a small scenario...[/QUOTE]The access time for a typical 7,200 rpm drive is in the 10ms region. Is this really too slow for your script?
Copy linkTweet thisAlerts:
@chazzyAug 22.2006 — i'm not sure how you can say that for his case, bokeh, as you don't know what his query would return. it might be more than 1 row, it could be spanning 100 tables, etc.
Copy linkTweet thisAlerts:
@bokehAug 22.2006 — i'm not sure how you can say that for his case, bokeh, as you don't know what his query would return. it might be more than 1 row, it could be spanning 100 tables, etc.[/QUOTE]True but...i have this piece of code in my server PHP script:

$count;

$count++

echo $count;

now i want that everytime this script is called my output should be

1 and then 2 and then 3 and so on

and not 1 and 1 and 1 and so on.[/QUOTE]
Based on that I can't see anything complex that would need a database. If this is merely to advance an integer as in this example I think using a file would be the best all round option.
Copy linkTweet thisAlerts:
@chazzyAug 22.2006 — always the pessimist i suppose...

i was considering his $count++; example as a "this is a simple example, my real one is much more difficult involving numerous hosts and none of my own systems" like what I deal with on a daily basis.
×

Success!

Help @gupta_vishaal spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...