ryanb/cancan — Issue #506: caching accessible_by results
Author: gitt
Created: 2011-11-07 18:25:06+00:00
State: open

I'm working on a project manager api, that doesn't really change a whole lot, after the users initial setup. Caching in this situation makes sense, especially with a lot of ajax on the client side, etc... I'm using dalli, and mem_cache_store. I assume I'm doing something wrong, but vast google searching returns a lot of caching objects that aren't really protected, and/or html fragment caching. Neither of which really applies here. Any help is appreciated.

This code works, but obviously doesn't check ability

```ruby
def fetch_by_uid
@projects = Rails.cache.fetch("user:projects:#{current_user.email}") do
Project.accessible_by(current_ability)
end
end
```

This code fails, with an error about the object being frozen

```ruby
def fetch_by_uid
@projects = Rails.cache.fetch("user:projects:#{current_user.email}") do
Project.accessible_by(current_ability)
end
end
```

This code works, but doesn't cache:

```ruby
def fetch_by_uid
projects = Rails.cache.fetch("user:projects:#{current_user.email}") do
Project.accessible_by(current_ability)
end

@projects = projects.dup
end
```