dkubb / memoizable

Memoize method return values
MIT License
111 stars 10 forks source link

Cacheable capability? #3

Closed BiggerNoise closed 10 years ago

BiggerNoise commented 10 years ago

One thing that got lost in the hubbub over memoizing methods with arguments was the caching capability in forget-me-not which has a similar interface (even if the intent is wildly different).

I know this is a somewhat odd fit in a project called memoizable, but the two ideas occupy a similar region of headspace, so I thought it made sense to combine them in a single gem.

There's a pretty decent write-up of the intent and capabilities of cacheable on the forget-me-not project page. Is it possible that this idea can find a home here?

dkubb commented 10 years ago

@BiggerNoise Can you clarify exactly what caching means? Is it that you want a way to define a method that is memoized, but the cache uses something other than an in-memory store. Specifically, something that can be shared between processes, and even physical servers?

BiggerNoise commented 10 years ago

@dkubb Yes. The caching that I am proposing is shared between processes and servers.

I would consider memoization to be a special case (single instance scope) of the general software engineering concept of caching. Unfortunately, I am not aware of a similarly specific term to describe caching that is scoped across a server farm.

As I said, the interface (and a fair bit of the implementation) of memoization and caching (the forget me not flavor), are similar, and I think the concepts do occupy a similar head space even if their intents are orthogonal.

I hope that answered your question.

dkubb commented 10 years ago

@BiggerNoise I think this would be relatively simple. All someone would have to do is override Memoizable::InstanceMethods#memoized_method_cache to return an instance that implements the following interface:

#[](name)
#[]=(name, value)
#fetch(name, &block)  # caches the block return value
#key?(name)

All-in-all, it's pretty simple to add a new backend. A really thin wrapper around a moneta instance that adds the "compute if absent" behaviour to #fetch would give someone the ability to use dozens of different backends as caches. I would think this could be done in just a few lines of code.

I think for now I don think I want to add this to memoizable "core" though, at least not yet. I want to keep the core part of memoizable as small and single purpose as possible. I'm not against refactoring the code to make it more extensible so that you and others can make this kind of thing for your own purposes. I would like to see how the library is used in the wild before adding more features.

For now I'm going to mark this as closed, but I'm willing to reopen it if we get a significant number of people asking for this, or some real-world extensions become more well used.