diff options
Diffstat (limited to 'shard/lib/app/pagestore.ex')
-rw-r--r-- | shard/lib/app/pagestore.ex | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/shard/lib/app/pagestore.ex b/shard/lib/app/pagestore.ex index 5165b84..3cda51d 100644 --- a/shard/lib/app/pagestore.ex +++ b/shard/lib/app/pagestore.ex @@ -25,7 +25,7 @@ defmodule SApp.PageStore do @max_failures 4 # Maximum of peers that reply not_found before we abandon defmodule State do - defstruct [:shard_id, :path, :netgroup, :store, :reqs, :retries] + defstruct [:shard_id, :path, :netgroup, :store, :reqs, :retries, :store_path] end @@ -36,15 +36,21 @@ defmodule SApp.PageStore do def init([shard_id, path, netgroup]) do Shard.Manager.dispatch_to(shard_id, path, self()) - store_path = [Application.get_env(:shard, :data_path), "#{shard_id|>Base.encode16}.#{path}"] |> Path.join |> String.to_atom - {:ok, store} = :dets.open_file store_path, [type: :set] + store_path = [Application.get_env(:shard, :data_path), "#{shard_id|>Base.encode16}.#{path}"] |> Path.join + {:ok, store} = :dets.open_file(String.to_atom(store_path), [type: :set]) Process.send_after(self(), :clean_cache, 1000) - {:ok, %State{shard_id: shard_id, path: path, netgroup: netgroup, store: store, reqs: %{}, retries: %{}}} + {:ok, %State{shard_id: shard_id, path: path, netgroup: netgroup, store: store, reqs: %{}, retries: %{}, store_path: store_path}} end + def handle_call(:delete_store, _from, state) do + :dets.close state.store + File.rm state.store_path + {:stop, :normal, :ok, state} + end + def handle_call({:get, key, prefer_ask}, from, state) do case :dets.lookup state.store, key do [{_, _, bin}] -> @@ -84,9 +90,11 @@ defmodule SApp.PageStore do case :dets.lookup state.store, hash do [] -> :dets.insert state.store, {hash, {:cached, System.os_time(:seconds) + @cache_ttl}, bin} + :dets.sync state.store nil [{_, why}] -> :dets.insert state.store, {hash, why, bin} + :dets.sync state.store why [{_, _, _}] -> nil |