dcsimg
www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: PHP data structure from memory

  1. #1
    Join Date
    Aug 2006
    Posts
    10

    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

  2. #2
    Join Date
    Oct 2005
    Location
    Gold Coast, Australia
    Posts
    2,115
    PHP lacks such structure, the closest equivalent would be a temporary cache: Session. 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?

  3. #3
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    you could use the reference operator.... &

  4. #4
    Join Date
    Aug 2006
    Posts
    10
    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..

  5. #5
    Join Date
    Aug 2006
    Posts
    3

    Best Solution

    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.
    Last edited by Qwick; 08-20-2006 at 09:39 PM.

  6. #6
    Join Date
    Aug 2006
    Posts
    10
    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...

  7. #7
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    if that is your requirement, then php is not the solution for you. i would say take a look at .NET or Java.

  8. #8
    Join Date
    Aug 2006
    Posts
    3

    Switching Languages

    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.

  9. #9
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,604
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  10. #10
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    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.
    Last edited by chazzy; 08-20-2006 at 11:08 PM.

  11. #11
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,604
    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.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  12. #12
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    oh, hmm.

    Either way then, if you are going to try it with shmop, watch out for possible memory overwriting.

  13. #13
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    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.

  14. #14
    Join Date
    Aug 2006
    Posts
    10
    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

  15. #15
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles