0

On redmine 1.2/rails 2.3.11 I'm rendering a repository markdown file as html (as redmine_markdown_extra_viewer does), and now I'am trying to cache the result, which should be updated on each commit.

So I have a git hook that fetch the repo changes, and i'd like it to also clear the corresponding cache entries.

Cache generation (in a RepositoriesController::entry override):

cache_key =['repositories_md', @project.id.to_s, @path.to_s].join('/')
puts cache_key
@content = cache_store.fetch cache_key do
   Kramdown::Document.new(@repository.cat(@path, @rev)).to_html
end
render :action => "entry_markdown"

The hook that should clear the cache, but has no effect:

# This is ok
ruby script/runner "Repository.fetch_changesets"

# This not
ruby script/runner "Rails.cache.delete_matched(/repositories_md\/.*/)"

So it doesn't work and i don't even know if i've taken the right direction to implement that. Any input much appreciated!

chocoposh
  • 3
  • 1
  • 3

2 Answers2

4

Which cache backend are you using?

If it's memcached or anything other than the FileStore or the MemoryStore, the delete_matched method is not supported.

You're probably better off letting them expire and just replace their cached contents as they get updated.

leonardoborges
  • 5,579
  • 1
  • 24
  • 26
  • I didn't thought about that, i dont'know how to figure it out yet, but i guess it's what you said. Thanks for pointing that out. Maybe i should not care about the cache, redmine would do it on the production environment and update it has the repository changes. Anyway, i need to learn a lot... – chocoposh Dec 23 '11 at 11:53
  • Yeah that's the normal approach. Ideally we don't care about the cache. We set the value, with an optional expiration time, and when the value needs to be refreshed, we just replace the contents. So much so that memcached doesn't even give you a way to delete a cache key. You can only replace it with another value or null. Oh, since the answer helped you, consider accepting it :) – leonardoborges Dec 23 '11 at 12:16
  • BTW, `ActiveSupport::Cache::RedisStore` supports `delete_matched` as well. Best to avoid it if using on large Redis caches though. – djb Dec 17 '18 at 17:48
0

The problem is when using a Regular Expression as a fragment name, try using a String as a fragment name. Maybe get verbose. I had a similar problem with Dalli(with Memcached), and that was the reason.

Matias
  • 575
  • 5
  • 17