473,323 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Sharing data between sessions?

Hello

Some data are common to all user sessions, and to improve
performance/save resources, I'd like to share those data between all
sessions, so that each user doesn't have to hit MySQL for the same
data. I'd rather avoid writing those in a flat file, and keep stuff in
RAM instead.

Someone told me about cache servers like MemCacheD. I was also given
the hints of writing in OO (public class variables) or using
(super)globals.

For those of you who had to do the same thing, what would you
recommend, and why?

Thank you.
Jun 2 '08 #1
9 7357
Gilles Ganault wrote:
Hello

Some data are common to all user sessions, and to improve
performance/save resources, I'd like to share those data between all
sessions, so that each user doesn't have to hit MySQL for the same
data. I'd rather avoid writing those in a flat file, and keep stuff in
RAM instead.

Someone told me about cache servers like MemCacheD. I was also given
the hints of writing in OO (public class variables) or using
(super)globals.

For those of you who had to do the same thing, what would you
recommend, and why?

Thank you.
Zend_Cache will do the trick. It is a generic way to cache data using
different back ends like files, SQLite, memached, APC, etc. I would use
that.

You can also use shared memory with shm_get_var. Any stored data will be
cached across a host. You also have MySQL's query cache available, which
will speed things up without having to change your code much.
Jun 2 '08 #2
Gilles Ganault wrote:
Hello

Some data are common to all user sessions, and to improve
performance/save resources, I'd like to share those data between all
sessions, so that each user doesn't have to hit MySQL for the same
data. I'd rather avoid writing those in a flat file, and keep stuff in
RAM instead.

Someone told me about cache servers like MemCacheD. I was also given
the hints of writing in OO (public class variables) or using
(super)globals.

For those of you who had to do the same thing, what would you
recommend, and why?
First of all: does your script/server hit a limit, and is the
performance a problem? If no, don't worry about optimization right now.
It needlessly complicates things, and more often then not makes things
terribly inflexible.

As the script ends after the request, using public class variables
(unless hardcoded in the PHP source) with a longer lifetime on its own
cannot be done in PHP, the next request will bring the class into a new,
clean state again. The same goes for superglobals.

Also, realize that unless specified otherwise, the default for the built
in session handler is exactly what you wanted to avoid: flat files
(serialized data).

Memcache is one solution that could work, but is for most sites
unnecessary complication. To a limited extent, you could also use shmop.

However, if the database is stressed, I'd begin optimisation there.
Analyze EXPLAIN outputs, set proper indexes, possibly make views of
often accessed resultsets. If in need, ask in comp.databases.mysql what
you can do to optimize your slowest and/or most often performed queries.

If really stressed, I tend to do exactly what you tried to avoid: saving
data in flat files. The bulk of any typical site is not dynamic, save
for a few areas. Keeping a result as a flat file HTML fragment is quite
easy, and updating it on alterations of the date underneath will mean
you only have to generate it once for an alteration instead of on every
request. Also a possibility, which I wouldn't advise unless alterations
will be done without using your application/bypassing PHP, is using
filemtime() and a query to check wether anything has been altered since
then, and only then recreate it.

Personnally, I've never had the pleasure of working on a site hit
enough, or the horror of a server weak enough, to let memcache make an
important difference.
--
Rik Wasmus
[SPAM]
Now looking for some smaller projects to work on to fund a bigger one
with delayed pay. If interested, mail rik at rwasmus.nl
[/SPAM]
Jun 2 '08 #3
On May 15, 1:19 pm, Gilles Ganault <nos...@nospam.comwrote:
Hello

Some data are common to all user sessions, and to improve
performance/save resources, I'd like to share those data between all
sessions, so that each user doesn't have to hit MySQL for the same
data. I'd rather avoid writing those in a flat file, and keep stuff in
RAM instead.

Someone told me about cache servers like MemCacheD. I was also given
the hints of writing in OO (public class variables) or using
(super)globals.

For those of you who had to do the same thing, what would you
recommend, and why?

Thank you.
Your email implies that you already have user session data - in that
case, it would be a lot more sensible to hold the data close to the
user session data and merge when the session is re-instantiated using
a custom session handler. If you go down this route, you do need to
think about how you save the common data - in the case of users
sessions, there is likely to be little contention, but with a common
repository being updated each time a session is saved there could well
be. This is less of a problem with a DBMS than with files because
requests to update can be queued up. Another approach would be to not
update the common repository when the session is saved but implement
updates via another route (typically I'd expect that the common data
has a longer TTL than user session data).

I've not seen benchmarks, but would expect a DBMS session handler
(particularly one using MySQL) to be only slightly slower than a file
based one (and most of the difference being the mysql conection
overhead - which you may already have elsewhere in your code). However
the difference between fetching a single serialized array from the
database, and fetching two using a union then carrying out an
array_merge I'd expect to be negligible.

C.

Jun 2 '08 #4
Gilles Ganault wrote:
Hello

Some data are common to all user sessions, and to improve
performance/save resources, I'd like to share those data between all
sessions, so that each user doesn't have to hit MySQL for the same
data. I'd rather avoid writing those in a flat file, and keep stuff in
RAM instead.

Someone told me about cache servers like MemCacheD. I was also given
the hints of writing in OO (public class variables) or using
(super)globals.

For those of you who had to do the same thing, what would you
recommend, and why?

Thank you.
Public class variables won't do you any good. They are still related to
the current script and can't cross scripts.

MemCachD can help. You can also use the shared memory functions in PHP.

However, I really wonder if you need this. If the data are that
currently accessed, they are probably already cached by MySQL, and
accessing the data will be very quick.

Are you actually seeing a performance problem? Or are you falling into
the trap of premature optimization?
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jun 2 '08 #5
On Thu, 15 May 2008 14:52:18 +0200, Rik Wasmus
<lu************@hotmail.comwrote:
>First of all: does your script/server hit a limit, and is the
performance a problem?
Yes. Since most data fetched from MySQL is common to all logged-on
users, each session is using a lot of RAM and fetching the same data
from MySQL needlessly. AFAICT, MySQL is as optimized as it could be at
this point. I need to find how to share common query output with every
session.
>Also, realize that unless specified otherwise, the default for the built
in session handler is exactly what you wanted to avoid: flat files
(serialized data).
If it's possible to have session data live in RAM instead, should I
expect significant performance?

So, possible solutions are:
- ZendCache + MemCacheD/APC
- shared memory

I'll check this out and see how it goes.
Jun 2 '08 #6
On Thu, 15 May 2008 23:11:19 +0200, Gilles Ganault <no****@nospam.com>
wrote:
On Thu, 15 May 2008 14:52:18 +0200, Rik Wasmus
<lu************@hotmail.comwrote:
>First of all: does your script/server hit a limit, and is the
performance a problem?

Yes. Since most data fetched from MySQL is common to all logged-on
users, each session is using a lot of RAM and fetching the same data
from MySQL needlessly. AFAICT, MySQL is as optimized as it could be at
this point. I need to find how to share common query output with every
session.
Make very sure this is so. There are instances were lowering some settings
for MySQL can acctually speed up things. And running EXPLAINS on just
about every query that goes in also helps a lot.
>Also, realize that unless specified otherwise, the default for the built
in session handler is exactly what you wanted to avoid: flat files
(serialized data).

If it's possible to have session data live in RAM instead, should I
expect significant performance?
It is possible, however, it will only affect performance noticably if you
have HUGE sessions (i.e. a terrible amount of data stored in a $_SESSION).
So, possible solutions are:
- ZendCache + MemCacheD/APC
- shared memory
And don't forget, profile & trace your current code. Xdebug works great,
even more so with xdebug.show_mem_delta on.
--
Rik Wasmus
....spamrun finished
Jun 2 '08 #7
On Fri, 16 May 2008 00:44:08 +0200, "Rik Wasmus"
<lu************@hotmail.comwrote:
(snip)

I'll go over MySQL fine-tuning again, check what happens when session
data live in RAM instead of in files under /var/tmp/, and play with
Xdebug more.

BTW, for those of you using APC, how do you keep data in sync with
MySQL? Does it work only if all changes to MySQL go through my PHP +
APC scripts, ie. all INSERT/UPDATE/DELETE imply running SELECT right
after and updating the ad hoc variable in APC?

Thank you.
Jun 2 '08 #8
Gilles Ganault schreef:
On Fri, 16 May 2008 00:44:08 +0200, "Rik Wasmus"
<lu************@hotmail.comwrote:
(snip)

I'll go over MySQL fine-tuning again, check what happens when session
data live in RAM instead of in files under /var/tmp/, and play with
Xdebug more.

BTW, for those of you using APC, how do you keep data in sync with
MySQL? Does it work only if all changes to MySQL go through my PHP +
APC scripts, ie. all INSERT/UPDATE/DELETE imply running SELECT right
after and updating the ad hoc variable in APC?

Thank you.
Personally I would do it simple: Just use a file that contains the
'shared content', as Rik suggested also.
I have used that solution a few times now, and it never failed me.
(increasing the responsetime for certain pages from 30 secs to 0.5 secs)
I did this both in Java (J2EE) and PHP and VB/ASP. The approach always
works (for me).

Understand that using a 'file' doesn't mean the Harddisk is read every
time to get it for each invocation of your PHP script: It is cached in
memory on almost every OS I saw.
Hence it is bloody fast.
Using a file also keeps things simple: You update the file only when
need arrises. I did it in 2 ways untill now:

1) Update such a file immediately if underlying data is changed.
This involves of course a good understanding which actions can possibly
change the underlying data, and then rebuild the file.

2) Create a cronjob that updates the file every 30 minutes or so.
This is only possible if the data doesn't have to be 100% up-to-date.
(I used that for top-10 lists of complex stuff, and stuff like that)

Using a file for shared content is also a very general solution. If you
switch databases later for some reason, this file logic stays the same.
If you finetune your database... Well, you'll have to do that again if
you switch database. But maybe this is not relevant because you won't
switch databases.

just my 2 cent.

Regards,
Erwin Moller
Jun 2 '08 #9
On Fri, 16 May 2008 10:51:37 +0200, Erwin Moller
<Si******************************************@spam yourself.comwrote:
>Personally I would do it simple: Just use a file that contains the
'shared content', as Rik suggested also. [...] Understand that using
a 'file' doesn't mean the Harddisk is read every time to get it
for each invocation of your PHP script: It is cached in memory
on almost every OS I saw.
Thanks for the idea. Another solution is to keep user-specific data in
sessions, and store data common to all users in a cache:

//Fetch customer-specific data from session file
session_start();
if(isset($_SESSION['myprivatevalue'])) {
print $_SESSION['myprivatevalue'] . "<p>\n";
} else {
$_SESSION['myprivatevalue'] = "verysecret";
}

//Fetch common data from cache
//apc_add('scooby-doo', 'daphne');
print "Scooby-do=" . apc_fetch('scooby-doo');
//apc_delete('scooby-doo');

Thanks guys.
Jun 2 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Steve W | last post by:
I tried searching first, but couldn't find an answer My first shot at PHP (what a cool language!) Wrote a quick script to query a server for xml data and plot it as points on a map ...
2
by: Jason Schneekloth | last post by:
Hey all.... I'm writing an app that needs to send/recieve data from other applications running, I guess you could think of it as a data relay broker between running applications. I had planned...
1
by: chemdawg | last post by:
I"m Having a problem Sharing data between access Microsoft® Excel. I need to populate the databases in a flash by linking and updating the excel sheets.
2
by: Red Green | last post by:
I am trying to switch to C# from Delphi and I am having some trouble finding some information. What I need to do should be simple but I have been through a dozen books and done web searches that...
2
by: Mervin Williams | last post by:
I am using Infragistics UltraWebTab (a tab folder control for ASP.NET). My tab folder control will include five tab pages with a separate web form on each, and these web forms will share data. ...
4
by: radiax | last post by:
Iam trying to find a simple solution for sharing data between windows applications( apart of using file system or remoting or MMF) . I tried using class library by making data members "shared" but...
3
by: Dax | last post by:
//////////////// Inside the APPLICATION/ game //how do i get the same instance running in the //dll into my app? i.e the pointer ferrari //does not work ! it creates a new instance in the ...
2
by: =?Utf-8?B?RWl0YW4=?= | last post by:
Hello, My application, winform, need to share data between multiple dialog boxes? What would be the best way to do it? Thanks, EitanB
9
by: kirk | last post by:
I have program.cs, my "main" form and then a "settings" form. My "main" form existed for awhile and I had constants, instantiations, properties, etc within it created. I went to create my...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.