The correct way to do this is to store your sessions in your database and
use memcached as a cache to speed it up. If you want to do something on
session end, you make a script that goes through the stored sessions in your
db, finds the expired ones, and does whatever it is you want to do.

So, on every pageview you do a memcached get for the current user's session
data.
If it does not exist, you check the db.
If it exists, you load it from db and store in memcached.
If it does not exist in the db, it is a new session, so you create it.
If it does exist in memcached, well, there you have it.

Then you process your page, and perform any changes to the session object.

And when your page is done, you store the session object back into memcached
and your db.
It is a good idea to attach a timestamp to your session object so that you
only do db writes every N minutes instead of every pageview. This way, users
may lose a minute or two of session data if a memcached server crashes or
the data expires, but you will get a significant speedup by not having to
write to the db on every pageview.


/Henrik Schröder

On Mon, Aug 24, 2009 at 00:22, ron <[email protected]> wrote:

> I simply want to use memcached to store user sessions for multiple web
> app servers, instead of using the tomcat's local session.
> Tomcat supports something call HttpSessionListener which has a
> listener method "sessionDestroyed" that allows a last chance clean up
> before the session object is destroyed.
> I know tomcat has ways to replicate sessions over multiple servers but
> i am thinking memcached is an easier solution for that.
>
> If you know some other better option please let me know.. thx.
>
> On Aug 23, 10:08 am, Henrik Schröder <[email protected]> wrote:
> > Basically, no, but there are ways you can do that yourself in your
> > application. However, it still sounds like you're using memcached for
> > something it was not designed to do. It is a cache, not a datastore. You
> are
> > never guaranteed to get back an item that you have stored, and you have
> no
> > way of knowing if a cache miss is because the item expired, or because it
> > was never stored in the first place.
> >
> > But what you can do is to store a timestamp yourself together with the
> item.
> > Then, whenever you retrieve the item, you look at your timestamp and
> treat
> > it as the actual expiry, so if it's passed, you perform your cleanup and
> > return null to the caller. And whenever you store an item you tag on this
> > timestamp and set the actual expiry to sometime far in the future.
> However,
> > note that you may not always get back items and your cleanup may not run
> for
> > all items.
> >
> > Perhaps if you told us what you wanted to achieve with your application
> we
> > could help you do it properly with memcached or point you in the
> direction
> > of other technologies if memcached is the wrong one for the job?
> >
> > /Henrik Schröder
> >
> > On Sun, Aug 23, 2009 at 17:57, ron <[email protected]> wrote:
> >
> > > thanks.
> >
> > > So can memcached at least return an expiring object for the last time
> > > to my application, with a flag set saying that it has expired in
> > > memecached and the next time you request the object it will return
> > > null?
> > > Doing this will give me a chance to do my other application clean up
> > > which needs the data in that expiring cache object.
> >
> > > On Aug 23, 8:34 am, Brian Moon <[email protected]> wrote:
> > > > 1. No, memcached does not do that.
> >
> > > > 2. You don't want it to.
> > >http://code.google.com/p/memcached/wiki/FAQ#When_do_expired_cached_it.
> ..
> > > > Having to have a garbage collector that watched items for expiration
> > > > would increase the load that memcached put on a system a great deal.
> >
> > > > Brian.
> > > > --------http://brian.moonspot.net/
> >
> > > > On 8/23/09 12:03 AM, ron wrote:
> >
> > > > > Hi,
> >
> > > > > Can memached trigger some kind of event to notify client that a
> > > > > particular cached object is expired??
> >
> > > > > If it doesn't support, does anyone know what other technology out
> > > > > there there is also a distributed cache but also it can send out
> > > > > events when an object is about to expired??
> >
> > > > > thx.
> > > > > Ron
>

Reply via email to