views:

451

answers:

6

Hi,

In general, I have the following scenario: - Fetch product and its related data from database - Convert fetched data to php 'product' object - cache product object in session

The cache is readonly, i.e customers viewing products on the site.

But there are calls like getProductIdsByCategory($categoryId) and the productIds from these results are cached too, per user, not using the global cache that I've read about.

A problem is that if someone on the admin side adds a new product and relates it to a category, then customers will not have the new productId come up in their cached getProductIdsByCategory until a new session is started.

Is there a way to clear e.g $_SESSION['x'] from ALL sessions on the server when a new product is added? I don't want to destroy all sessions because customers will then lose their logins etc.

Or should I move these cached productId searches to the global cache?

p.s am using a custom built cache, not memcached or similar.

Thanks

+2  A: 

Sounds to be like you could do with real shared state through a caching system like memcache.

The only other way that prints to mind is have the application check for flags for dirty cache data and delete it itself, or if your cache is in a database in a parsable serialized form write an expensive script to read them all, but that will create nasty lag with requests that have already read the data.

I would go with real shared state than checking for object copies.

Aiden Bell
Is memcached a good 'modern' way to do things? I don't know that much about it.
David Archer
@David, yes. Any concurrently accessible shared state will work. Some ORM systems will allow this. Just remember there will be a gap in clearing with any system because PHP will copy-in the shared state into the application. You need to stop it resaving if the cache is dirty ... or similar constraints for your needs. It is the importation you need to restrict.
Aiden Bell
A: 

to clear a session value use:

unset($_SESSION['x']);

you may loop on sessions for that

Bassel Safadi
That will only clear the session for the current user. I need to clear all $_SESSION['x'] from all users if an admin user adds a new product
David Archer
+1  A: 

Unless you store sessions in a database, clearing any specific bit of data will be tricky.

I would suggest caching in files rather than user sessions. This way you achieve the same benefits, but you get total control over what is cached and when it gets cleared.

Jani Hartikainen
+4  A: 

By default, the session data is just serialized files somewhere in your filesystem, and it is possible to go modify all of them to remove the information in question (respecting locking so that you don't step on any currently open sessions).

I don't really recommend it, though. What I would recommend is making a method of signalling that this cached data should be refreshed, like a database-stored timestamp that gets looked at when session_start() happens, and if the cached data is older than the timestamp, the cache is flushed.

chaos
This sounds like a really good idea. Though I guess I'd have to delete that flag after e.g half an hour or something?
David Archer
Don't see why. It can just go on moving further into the past, and sessions that have obtained their cached data more recently won't be bothered by it.
chaos
You're right, I'm going to give this a try. Thanks
David Archer
Glad to be of service.
chaos
A: 

Yes, you should move it to a global cache. Sessions are not meant to be accessed globally, I hardly think it's possible.

Tamás Szelei
A: 

To disable all existing sessions for a particular application, simply modify your application to change the name of the session using PHP's session_name('new_session_name'). This function needs to be called before each call to session_start().

This won't actually clear the current sessions, but it renders them no longer useful for this application.