expire without object id

14 views
Skip to first unread message

Matthias Heigl

unread,
Feb 12, 2008, 8:13:20 PM2/12/08
to acts_as...@googlegroups.com
Hi,

i did not catch the fish how i can store and expire queries without the need to specify an object id. If i only store single objects there is everything ok, but in my case i want to store complete Queries, such as hole paginated pages in memcached.

When i store a query through cache_fu it will stored im memcached with information such as

set [projectname]-[environment]:[Classname]:[my cache_key]

If i expire_cache for a given Object it appends an additional object.id to the cache_key such as

delete [projectname]-[environment]:[Classname]:[Object.id]:[my cache_key]

So i have to add the Object.id by my self at get_cached like

def self.cached_get_pagination(category_id, itemorder, page)
get_cache("#{category_id.to_s}:page_#{itemorder}_#{page}") do
Item.get_pagination(category_id, itemorder, page)
end
end

But on one page i didn't have an Object.id because it is displaying all Channel with one depended Item, so i can't set an Object.id for this case.

def self.cached_get_pagination_latest(itemorder, page)
get_cache("page_#{itemorder}_#{page}") do
Item.get_pagination(itemorder, page)
end
end

set [projectname]-[environment]:[Classname]:page_[my item order]_[page number]

So, if one Channel is created or saved i want to clear my cache, but in this case cache_fu sets automatically the id of the given Object and my request to memcached looks like

set [projectname]-[environment]:[Classname]:[Object.id]:page_[my item order]_[page number]

and the cache didn't get expired.

How i'm able to set a sort of id undependent cache?

Regards,

Matthias

groovepack

unread,
Feb 13, 2008, 4:57:55 PM2/13/08
to acts_as_cached
ok ok, the previous thread gave me the hint ...

Eli Miller

unread,
Feb 13, 2008, 5:19:00 PM2/13/08
to acts_as...@googlegroups.com
Good, I was about to reply with similar suggestions. Overriding
cache_key and cache_id allows cache_fu to be used in a very wide
variety of situations.

JBar

unread,
Feb 25, 2008, 12:57:49 AM2/25/08
to acts_as_cached
I'm having a little trouble following the conversation but I believe I
have a similar situation and reading through the thread I wasn't able
to find the answer. Like Matthias, I'm trying to paginate and put the
results in memcache using something very similar to what he's done
(and making the key name dynamic by converting the parameters to
strings just like him). However, I'm not sure how to expire the cache
in such a situation. The "after_save :expire_cache" or
"after_save :reset cache" only seems to expire cached items with an
object ID. In my case, my model is called Product and calls like
Product.get_cache(3) are renewed after the expire/reset cache call but
my custom pagination which has keys like
"paginate_[[:page,_nil],_[:per_page,_5],_[:include,_[:manufacturer,_:user,_:categories]],_[:order,_"products.name"]]"
don't expire. Reading through the groups, I understand that this is
expected behavior (please tell me if it's not).

So, I'm thinking about creating a key that will store all my custom
key names for my Product model (something like "product_custom_keys")
which will be an array that I add to every time something gets
cached. I'll then add an after_save callback (something like
after_save :expire_custom_cache) that then iterates through the values
under product_custom_keys and expire and clears each one.

First of all, is that even a solution (is there a better way to do
this?). Secondly, where would I put the code to add these custom keys
into my product_custom_keys (I assume it would have to go somewhere in
a modified get_cache call to make sure it's only called when the key
is first stored?

Any help is much appreciated. Caching pagination (I'm using
will_paginate) seems to be a common roadblock that I haven't seen a
good blog post, screencast, etc. for yet.

On Feb 13, 2:19 pm, "Eli Miller" <elijah.mil...@gmail.com> wrote:
> Good, I was about to reply with similar suggestions. Overriding
> cache_key and cache_id allows cache_fu to be used in a very wide
> variety of situations.
>

Chris Wanstrath

unread,
Feb 25, 2008, 2:35:26 PM2/25/08
to acts_as...@googlegroups.com
On 2/24/08, JBar <jbar...@gmail.com> wrote:

> Any help is much appreciated. Caching pagination (I'm using
> will_paginate) seems to be a common roadblock that I haven't seen a
> good blog post, screencast, etc. for yet.

I usually do something like this:

http://pastie.caboo.se/157119

Or, even better, just cache the fragments and have `expire_pages`
expire a certain # of cached fragments.

- Chris

JBar

unread,
Feb 26, 2008, 12:09:29 AM2/26/08
to acts_as_cached
That's awesome. Thanks Chris!

One question...why aren't don't the methods cached_page and fetch_page
have "self." before them? Wouldn't I want to call
Product.cached_page(1) or are you recommending I call it in some other
way.

Thanks again for all the help. I love the plug-in :).

On Feb 25, 11:35 am, "Chris Wanstrath" <ch...@ozmm.org> wrote:

Chris Wanstrath

unread,
Feb 26, 2008, 12:42:02 AM2/26/08
to acts_as...@googlegroups.com
On 2/25/08, JBar <jbar...@gmail.com> wrote:

> One question...why aren't don't the methods cached_page and fetch_page
> have "self." before them? Wouldn't I want to call
> Product.cached_page(1) or are you recommending I call it in some other
> way.

You're right, my mistake: http://pastie.caboo.se/157431

- Chris

Reply all
Reply to author
Forward
0 new messages