synchronize different user sessions when events happened in PHP - php

I’m writing a social networking application and I want to synchronize different user sessions.
My context is following:
userA is logged and has an array of his contacts in session.
userB is logged and has an array of his contacts in session.
userA adds userB as a contact.
So, Contact table (DB) sees a new record
And userA has an update of his array of contacts (easy because the event happens on userA navigation side)
My need is following:
I want now to synchronize automatically userB’s session so that his array of contacts is refreshed
without doing a sql query to check the contacts table (db). I want that in order to avoid making too many sql queries to check that.
Do you have any suggestions?
I’m using CodeIgniter (PHP) and the session is managed with database.
Thanks a lot

This is not possible without doing a sql query to check the contacts table. Even if you hold your sessions in sql table you still have to query for the data

You want to update a copy of the data (userAs session) without refering to the master set of data. This is just not going to work.
With it's shared nothing architecture, this is going to be hard to implement in PHP.
The only sensible way to achieve this in anything approaching realtime is to maintain a semaphore for each current user session - and set that semaphore when the session data should be re-loaded. And at that point you must read the data from the database.
Much of this could be done within the session handler - which is probably the most sensible place to handle it - but you need to be aware that the semaphore is therefore volatile and code it appropriately (e.g. using optimistic locking around reading/writing the semaphore).

Related

Store Data in a PHP Session to prevent too many MySQL-Connections

I recently started saving data that I use several times into a PHP session.
For example, I have to use the active countries for each load of the website. Instead of creating a database connection every time, I have now saved this data in a session. I do that with all the data that I use several times. I also created banners in a multi-level array and saved this array in a session.
On first Loading of the Website, all the data that i need, are collected and stored in several php session.
Now I have about 10 different PHP sessions at the start of the website but practically no database connection anymore.
Is this procedure workable in this way?
Do you see a risk? For me it's all about the performance of the site. I try to create as few database connections as possible for recurring tasks
Am I making a reasoning mistake?
There are no sensitive data in the session. For example, All Country for a select field with all (necessary) countries. The array is then Key => Country. And so I create the Select field via "foreach".

Server side caching vs client side caching to maintain a key-value map

I have a web app written with extjs and a php backend. The backend has access to databases A and B. The app contains various grids which display a lot of user information; Whats important for this problem is the userID which is displayed in most grids as a separate column.
The problem
For some reasons, the userID in DB B has to be replaced by a user's accountID. But the front-end still has to display the user's userID. Which means I have to have some form of a mapping between userID - accountID to maintain the expected functionality.
Please note that the userID -> accountID mapping is available in the database A
Potential Solutions
Server-side cache -
There are approximately 10000 users. One option I have considered is a server-side cache such as a redis to maintain this mapping. But this would still need query results to be parsed in the backend and each userID replaced with the corresponding accountID and sent to the front-end. So for example, 500 resultant rows from a query would mean 500 redis calls to obtain the corresponding userID (?) Performance might be an issue here.
Client-side cache -
Use a client-side storage option such as LocalStorage, LocalForage or even a simple hashmap and load the mapping details from the redis to the front-end. Then when the grid data is being rendered, use this map to obtain the corresponding userID.
The mapping has to work the other way around as well, when sending data from the app to the database.
There are also issues like keeping the redis updated with the DB as there can be scenarios where a user information could change. Then the client-side cache will also need to be updated. All these things need to be considered as a part of the solution.
I would appreciate some feedback and any other potential solutions I could look at to fix this issue.

Return User Specific Data from Couchdb

background: I have a mysql database that I'm going to move over into couchdb, in that database are some tables, "books", and "users". I login to the web application via a login form and currently post username and password at a php script that then logs the user in and writes a php session to the users table for that user. Now, when the user requests books, I a) make sure that the php session is valid for a user, and then use the logged in userid to get books that user can view.
question: how would you replicate this sort user specific record query in couchdb? I'm assuming the whole point of couchdb is to be able to say, http://somesite.com/couchdb/getBooks/ and that db end point returns books.
maybe i just need to pass a sessionid at the end? http://somesite.com/couchdb/getBooks/?s=123123 BUT then where do I get the session? hmmm.
Any direction or articles that talk about this would be great.
Handling user-specific data in CouchDB is actually a pretty complicated topic, but I tried to summarize some of the different strategies here.
Long story short: if you want to have user-private data in CouchDB (and it sounds like you do), the best way is to have a server-side daemon that allocates one database per user, and the user has full privileges in that database. (This can scale up to > 100k users, so no worries.) Then you just sync that database with PouchDB.
The link above has some third-party projects that implement exactly such a daemon. There's also Janus, which is not ready yet, but once it is, it'll do all of the above as well as integrate nicely with PouchDB.

Temporary Table not always available in Code Igniter?

Code Igniter - PHP - Mysql.
I am working on a Shopping Website, and due to the large number of Products and Client Pricing Options for each Product (inclusion of Discounts at a different rate for each user etc), have created a temporary table when a Client Logs in.
The table is creating successfully, however as I navigate thru the site it doesnt always remain persistent.
For eg, If i have a query saying "Select * From Temp_Products" running on each page of the site, it returns an empty set or says Temp_Products doesnt exist on some pages.
I am unable to figure out where the problem is, there seems to be no specific pattern or trigger causing this.
Also, how do e-commerce sites usually implement catalogues & inventry? Any info would be helpful!!
Thanks !
Create a table in the db
es: id - id_user - id_table
after, put in the session the field that permit you to retrive the item of table.
Create a library that permit you to dialog with the data in the db es: Mytable, and in this library, create some function that get automaticaly get the items on the db, starting from the field in the session.
If you use temporary tables there are a few pitfalls.
First of all, if you use persistent connections (which you never ever should), the temporary table will be shared among Ci-sessions since persistent connections are shared among CI-sessions. This is probably not what you want.
If you don't use persistent connections (as you should), the temporary table only lasts for the current request. Since when the CI-instance stops at the end of every request, it cleans up its database connection, which drops all temporary tables for that connection.
In your case you probably need a 'normal' table using the session-id as a key. Don't forget to clean up the sessions specific entries after the session dies. This is not easy, since codeigniter uses some delayed garbage collection mechanism for this. So you will probably need a cronjob that periodically checks whether sessions do or do not exist anymore and act upon this.

do i use ci_sessions to store session data

Im developing site for a travel site, in the process when a user searches I do manipulation on the database result then store it in session. Which stores the data in the ci_session database. By following this method I'm able to easily filter the data from
$this->session->userdata();
A normal search which results in 4 hotels the session data goes more than 20kb, in actual server environment it is possible that the result will be more than 200 hotels, i.e. 4000kb data for a single user on each row, in ci_session table.
this process is making the app very slow, What other approach i can follow to make the search result much faster.
You can store the session data in your database. CI Sessions have the ability to handle that. No need to do it yourself.
Read the chapter in the manual how to configure it properly. Choose wisely what you write into the "session" since lots of sessiondata in the database could make the requests slow if you need them on every site.

Categories