sasa1977 / con_cache

ets based key/value cache with row level isolated writes and ttl support
MIT License
910 stars 71 forks source link

An easy way to create global caches #5

Closed volgar1x closed 10 years ago

volgar1x commented 10 years ago

Hello,

I'm a Elixir/OTP very beginner and I'm struggling with workers and supervisors. Even if I'm learning how to use OTP correctly, is there any easy/faster way to create global caches ? Here is my thought translated to code :

defmodule MyApp.Cache do
   use ConCache
end

defmodule MyApp.Supervisor do
  use Supervisor

  def start_link do
    :supervisor.start_link(__MODULE__, [])
  end

  def init([]) do
    children = [
      worker(MyApp.Cache, []),
    ]

    supervise(children, strategy: :one_for_one)
  end
end

MyApp.Supervisor.start_link
value = MyApp.Cache.get :key
sasa1977 commented 10 years ago

Hi. I have to think this through and get back to you when I return home. The basic problem is that con_cache doesn't comply to OTP principles and should be redesigned a bit. I didn't find the time for this yet due to the work on Elixir in Action.

-------- Original message -------- From: Antoine Chauvin notifications@github.com Date:20/07/2014 18:41 (GMT+01:00) To: sasa1977/con_cache con_cache@noreply.github.com Subject: [con_cache] An easy way to create global caches (#5)

Hello,

I'm a Elixir/OTP very beginner and I'm struggling with workers and supervisors. Even if I'm learning how to use OTP correctly, is there any easy/faster way to create global caches ? Here is my thought translated to code :

defmodule MyApp.Cache do use ConCache end

defmodule MyApp.Supervisor do use Supervisor

def start_link do :supervisor.start_link(MODULE, []) end

def init([]) do children = [ worker(MyApp.Cache, []), ]

supervise(children, strategy: :one_for_one)

end end

MyApp.Supervisor.start_link value = MyApp.Cache.get :key — Reply to this email directly or view it on GitHub.

sasa1977 commented 10 years ago

Hi @Blackrush. Thanks for raising this issue. It has reminded me that ConCache needs some cleaning up. I've made an (incomplete) change that turns ConCache into a proper OTP application that can be used from other OTP applications.

Now, it is possible to register the cache under some alias and use that alias to interface with the cache. Moreover, you can insert the corresponding process into your own supervision tree as you please.

The code still needs some polishing up, and I must adapt the readme. However, notable changes are mentioned here. If you feel like it, you can try it out from the branch and let me know what you think.

volgar1x commented 10 years ago

That's really great! It'll save much boilerplate I created myself implementing GenServer :smile:

sasa1977 commented 10 years ago

published on hex as well