@@ -6,7 +6,18 @@ defmodule CloudCache do
66
77 ## Usage
88
9- Starting `CloudCache` is easy, just choose your adapter and run:
9+ CloudCache automatically starts the configured caches when the application
10+ starts. This means if you're using the default adapter, `CloudCache.Adapters.S3`,
11+ you don't need to do anything else.
12+
13+ If you want to start the caches manually then first set `auto_start` to `false`
14+ in your config:
15+
16+ ```elixir
17+ config :cloud_cache, auto_start: false
18+ ```
19+
20+ Then you can start `CloudCache` manually:
1021
1122 ```elixir
1223 CloudCache.start_link([CloudCache.Adapters.S3])
@@ -71,13 +82,61 @@ defmodule CloudCache do
7182
7283 alias CloudCache.Adapter
7384
85+ @ registry CloudCache.Registry
86+ @ instances :instances
7487 @ default_adapter CloudCache.Adapters.S3
7588 @ default_name __MODULE__
7689
90+ @ doc """
91+ Returns a list pids of all running instances.
92+
93+ ### Examples
94+
95+ iex> CloudCache.instances()
96+ """
97+ def instances do
98+ @ registry
99+ |> Registry . lookup ( @ instances )
100+ |> Enum . map ( fn { pid , _value } -> pid end )
101+ end
102+
103+ @ doc """
104+ Returns the list of children for the given instance.
105+
106+ ### Examples
107+
108+ iex> CloudCache.which_children()
109+ """
110+ def which_children ( name \\ @ default_name ) do
111+ Supervisor . which_children ( name )
112+ end
113+
114+ @ doc """
115+ Returns the pid of the instance with the given name.
116+
117+ ### Examples
118+
119+ iex> CloudCache.whereis()
120+ """
121+ def whereis ( name \\ @ default_name ) do
122+ Process . whereis ( name )
123+ end
124+
125+ @ doc """
126+ Starts a new instance with the given caches and options.
127+
128+ ### Examples
129+
130+ iex> CloudCache.start_link([CloudCache.Adapters.S3])
131+ """
77132 def start_link ( caches , opts \\ [ ] ) do
78- Supervisor . start_link ( __MODULE__ , caches , Keyword . put_new ( opts , :name , @ default_name ) )
133+ { name , opts } = Keyword . pop ( opts , :name , @ default_name )
134+ Supervisor . start_link ( __MODULE__ , { name , caches } , Keyword . put ( opts , :name , name ) )
79135 end
80136
137+ @ doc """
138+ Returns a child specification for the given caches and options.
139+ """
81140 def child_spec ( { caches , opts } ) do
82141 % {
83142 id: { __MODULE__ , opts [ :id ] || opts [ :name ] || opts [ :key ] || opts [ :default ] } ,
@@ -88,14 +147,26 @@ defmodule CloudCache do
88147 }
89148 end
90149
91- def child_spec ( opts ) do
92- opts |> Keyword . pop! ( :caches ) |> child_spec ( )
150+ def child_spec ( list ) do
151+ if Keyword . keyword? ( list ) do
152+ Keyword . pop! ( list , :caches ) |> child_spec ( )
153+ else
154+ child_spec ( { list , [ ] } )
155+ end
93156 end
94157
95158 @ impl true
96- def init ( caches ) do
159+ def init ( { name , caches } ) do
160+ children = collect_children ( caches )
161+
162+ case Registry . register ( @ registry , @ instances , nil ) do
163+ { :ok , _ } -> Supervisor . init ( children , strategy: :one_for_one )
164+ { :error , { :already_registered , _ } } -> raise "instance already started: #{ inspect ( name ) } "
165+ end
166+ end
167+
168+ defp collect_children ( caches ) do
97169 caches
98- |> Kernel . ++ ( CloudCache.Config . caches ( ) )
99170 |> Enum . map ( fn
100171 { adapter , child_spec_args } -> { adapter , child_spec_args }
101172 adapter -> { adapter , [ ] }
@@ -104,7 +175,6 @@ defmodule CloudCache do
104175 [ adapter . child_spec ( child_spec_args ) | acc ]
105176 end )
106177 |> Enum . reverse ( )
107- |> Supervisor . init ( strategy: :one_for_one )
108178 end
109179
110180 # Non-Multipart Upload API
@@ -115,6 +185,12 @@ defmodule CloudCache do
115185 |> Adapter . list_buckets ( opts )
116186 end
117187
188+ def create_bucket ( bucket , region , opts \\ [ ] ) do
189+ opts
190+ |> adapter ( )
191+ |> Adapter . create_bucket ( bucket , region , opts )
192+ end
193+
118194 def list_objects ( bucket , opts \\ [ ] ) do
119195 opts
120196 |> adapter ( )
0 commit comments