cache_method icon indicating copy to clipboard operation
cache_method copied to clipboard

Feature: cache_method if

Open jvenezia opened this issue 10 years ago • 0 comments

It would be interesting to have an "if" option to cache_method. This way we could enable caching with a condition:

def slow_method
    ...
end

cache_method :slow_method, if: -> { User.count > 100 }

As a workaround I use a second method to test my condition:

def slow_method
    if User.count > 100
        self.cached_slow_method
    else
        # Execute the uncached slow code
    end
end

def cached_slow_method
    ...
end

cache_method :cached_slow_method

What do you think ? I can help on this if you like the idea.

jvenezia avatar Aug 17 '15 15:08 jvenezia