473,574 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7370
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*******@attgl obal.net
=============== ===

Jun 2 '08 #5
On Thu, 15 May 2008 14:52:18 +0200, Rik Wasmus
<lu************ @hotmail.comwro te:
>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.comwro te:
>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.comwro te:
(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.comwro te:
(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************ *************** *************** @spamyourself.c omwrote:
>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($_SESS ION['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('scoo by-doo');
//apc_delete('sco oby-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
1640
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 Easy......, but then I wanted to take that same data set and create an IMAGE MAP in HTML via PHP so I could click on each data point
2
300
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 on creating a windows service to handle this, but I found out that I cannot directly send/recieve data here, i can only signal it. First off, am I...
1
1293
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
6895
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 don't help me out at all. Delphi had a data module that was shared between an applications forms but I don't see any way to do this in C#. Lets say...
2
301
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. My questions regard the data tier, that is, how I should construct the data objects so as to reduce memory usage while optimizing page performance? ...
4
1806
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 it seems that data cant not be passed between applications. what am I doing wrong here? what are "simple" possible solutions? thanks
3
4246
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 //app and another in the DLL even after sharing car myferrari = getAvailableFerrari();
2
2851
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
1983
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 "settings" form and constantly found myself adjusting my "main" form entities to be static and inevitably just passing in the "main" forms 'this'...
0
7710
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8040
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8225
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7800
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8092
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6437
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5610
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3738
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.