Well as no one replied I guess no one knows.
The merb-cache functionality is pretty sparsely documented, so here is
how I solved part of my problem.
I am only using page caching, and mod-rewrite in Apache to display the
cached pages. Mixing actionstore is not an option as cached pages are
found by Apache and Merb never gets to use any actionstore. I suspect
this is a common usage for page cache.
With a blog when someone adds a comment you need to flush at least the
cached page for that article, in my case I need to flush everything as
the side bar has a list of recent comments, so all the pages need to
be flushed.
I added this to init.rb...
Merb::BootLoader.after_app_loads do
# add delete_all to Merb::Cache::Filestore
Merb::Cache::FileStore.class_eval do
def delete_all!
#puts "FileStore#delete_all! - rm -rf #{Dir.glob( @dir /
'*').inspect}"
FileUtils.rm_rf(Dir.glob( @dir / '*'))
end
end
end
This adds a delete_all! to the filestore, not sure why it isn't there
by default.
Then in my comments.rb controller I call flush_cache whenever a
comment is successfully posted...
def flush_cache
Merb::Cache[:page_store].delete_all!
end
In my posts.rb. controlled I do this for the cases where the cache
needs to be flushed...
after :flush_cache, :only => [:create, :upload, :destroy]
This seems to work well. I have a nagging doubt as to what happens
when the race condition occurs when Apache sees a cached page, and at
the same time someone posts a comment and flushes the cache, as the
two are not atomic. I guess the user will see a not found or
something. However I think that will be a pretty rare occurrence.
One thing I cannot find is how I would flush/delete just a single
page? There is a Merb::Cache[:page_store].delete(index) method which
by the looks of it would delete one page, however I cannot see how you
would specify the index so it deleted the correct page. I'll look into
that in more detail, may have to fire up the debugger to see how index
is converted into a file name.
Any improvements on this method would be greatly appreciated.
On May 21, 2:50 am, Jim Morris <[email protected]> wrote:
> Hi,
>
> I have implemented page caching on my simple blog engine. I use an
> apache mod_rewrite rule that will pick up any page caches, so far so
> good.
>
> Now if someone posts a comment to the blog, it won't show up because
> the page is in the cache, and the request will never reach Merb.
>
> I see that delete_all is not implemented for the page_store, so how
> can I flush the page cache so the new comment will be rendered?
>
> So far it looks like I need to do a rm -rf public/cache/* which is
> nasty.
>
> Ideally though it would be nice to just delete the cached show page,
> but really the index page needs to be flushed too as I have a list of
> recent comments.
>
> To make things harder comments are created in a different controller
> to the posts.
>
> So is there a better way to do this other than...
>
> after :flush_cache
>
> def flush_cache
> FileUtils.rm_r $MERB_ROOT / "public/cache/*"
> end
>
> Thanks for any help here.
>
> PS I could use the action_store instead but that is so much slower,
> and I'm not even sure that can be flushed.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"merb" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/merb?hl=en
-~----------~----~----~----~------~----~------~--~---