5

How to create a shared object between different sessions in PHP?

I'm thinking of using file or MySQL memory table. Using file isn't a good option because it doesn't have locking and is slow. Using MySQL memory table is a good option, but how to save class instances (objects) to a table? Serializing an object and put it to table is also slow.

Option 1: MySQL memory table
Option 2: shm_attach,shm_detach,shm_get_var,shm_has_var,shm_put_var,..
Option 3: Memcache

The problem is using MySQL memory table requires querying. Memcache is not included in standard PHP installation. To have shm_* functions on Windows, it's required to get PHP built from source with option "--enable-sysvsem", and this requires setting in php.ini where developer may not be able to access all the time.

Which one of the above is the better? Any other options?

jondinham
  • 8,271
  • 17
  • 80
  • 137
  • 1
    Shared memory is one option: http://php.net/manual/en/function.shm-get-var.php – Marc B Nov 17 '11 at 17:07
  • 2
    What data are you talking about ? what does this "object" look like ? – Manse Nov 17 '11 at 17:08
  • it's an instance of a class which has multi level structure (similar to multi level array) – jondinham Nov 17 '11 at 17:12
  • 1
    json_encode then store in MySQL ? – Manse Nov 17 '11 at 17:16
  • 2
    all the options I know of requires serialization. If that's what you're trying to avoid, lemme know when you find a way to avoid it :P – MicronXD Nov 17 '11 at 17:16
  • 1
    @ManseUK I think json_encoding is slower than php's serialize... but I could be wrong. – MicronXD Nov 17 '11 at 17:17
  • 1
    @MicronXD there are pros and cons of each -> http://stackoverflow.com/questions/804045/preferred-method-to-store-php-arrays-json-encode-vs-serialize – Manse Nov 17 '11 at 17:19
  • @MarcB i see that those php semaphore functions only apply to System V? – jondinham Nov 17 '11 at 17:19
  • 1
    @ManseUK Oh wow! json_encode is actually faster! KyleFarris tested and found 100% increase in speed when using json_encode and 20% increase when using json_decode as opposed to serialize and unserialize respectively. Thanks for enlightening me ManseUK! – MicronXD Nov 17 '11 at 17:27
  • 1
    `json_encode` / `json_decode` can not deal with concrete classes, but only stdClass type objects. And there are some other unsupported variable "features" with json that searialize is taking care of. Probably that's why it's "faster" (I doubt that kind of speed actually matters in your case). – hakre Nov 17 '11 at 18:48

4 Answers4

1

I don't see apc mentioned:

http://www.php.net/manual/en/book.apc.php

Not sure if it's within one session as the manual lacks any info on what it does but in other posts I see at alternative to memcache.

I am looking for a simular solution to storing page templates that might have to be used a lot and storing authenticated users in an in memory table instead of using php session (the second part I'm not so sure of).

HMR
  • 37,593
  • 24
  • 91
  • 160
1

I remember seeing a similar problem with a solution in the development of eyeOS.

I know you are not exactly to exited about using a file, but what if you were to store the variable(s) you want to share in xml format.

If you want to have it specific to certain sessions you could use unique tokens(a password of sorts) for each set of sessions and set up a controller that directs requests to the correct session xml file based on the token.

For security you could store the xml info in a php file and only allow information to be retrieved by POST using the correct token.

This method would allow you to securely access, edit, and delete(destroy) shared sessions.

Shattuck
  • 2,744
  • 4
  • 21
  • 26
1

If you want to share objects (instances of classes) between different processes, you will always be bound to serialize and unserialize regardless which kind of storage layer you use (database, memchace, files, ...).

If you don't want to use serialize and unserialize, then there is not much that you can do.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • Marc B suggested to use php semaphore functions shm_*, but this requires a parameter passing to php.exe to enable it. this is what i can do on my PC but not on my shared hosting server. im quite confusing now – jondinham Nov 17 '11 at 18:53
  • @JonDinham, Even if you use files You need to serialize the data first isn't it? – Pacerier Oct 18 '14 at 16:08
1

Memcache is not in standard PHP installation. Semaphore & shared memory functions are not supported on Windows.

Most likely the only solution is using MySQL memory table with object serialization.

jondinham
  • 8,271
  • 17
  • 80
  • 137