can memcached trigger event when object expired?

2,913 views
Skip to first unread message

ron

unread,
Aug 23, 2009, 1:03:53 AM8/23/09
to memcached
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

Brian Moon

unread,
Aug 23, 2009, 11:34:50 AM8/23/09
to memc...@googlegroups.com
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_items_get_deleted_from_the_cache?
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/

ron

unread,
Aug 23, 2009, 11:57:52 AM8/23/09
to memcached
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 <br...@moonspot.net> 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/

Joseph Engo

unread,
Aug 23, 2009, 1:05:54 PM8/23/09
to memc...@googlegroups.com
When an item is expired it will return false to you .. not the expired item.  It sounds to be like you want to use memcache as a datastore and save to object to disk when its expired, but this is the wrong way to handle memcache.  Its just a cache, nothing more.

Henrik Schröder

unread,
Aug 23, 2009, 1:08:04 PM8/23/09
to memc...@googlegroups.com
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

Clint Webb

unread,
Aug 24, 2009, 2:22:27 AM8/24/09
to memc...@googlegroups.com
If your application fails because of an expired item, what would
happen if an entire memcached instance crashed and you lost all the
items it contained? If you cant simply pull the data again from
other sources, then you are using memcached wrong.


--
"Be excellent to each other"

Henrik Schröder

unread,
Aug 24, 2009, 4:28:14 AM8/24/09
to memc...@googlegroups.com
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 <ronal...@gmail.com> 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 <skro...@gmail.com> 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
>

Adam Lee

unread,
Aug 24, 2009, 1:03:40 PM8/24/09
to memc...@googlegroups.com
Or don't store your sessions at all...
--
awl
Reply all
Reply to author
Forward
0 new messages