diff --git a/.gitignore b/.gitignore index 655ad4b..1797e77 100644 --- a/.gitignore +++ b/.gitignore @@ -72,4 +72,5 @@ npm-debug.log /assets/node_modules/ # Env files -.env \ No newline at end of file +.env +cache.dets diff --git a/README.md b/README.md index 2a314aa..bd65a59 100644 --- a/README.md +++ b/README.md @@ -1114,8 +1114,11 @@ and can be employed to store large amounts of data Do note that since this engine is in-memory, **all data is lost when the process ends/server is shutdown**. -There is an Elixir wrapper that makes it easy to use `ETS`. -[`ets`](https://github.com/TheFirstAvenger/ets). +There is an Elixir wrapper that makes it easy to use `ETS` and `DETS`, +where the latter is a disk-based variant of the former, +where data is persisted on disk. +We'll be using +[`pockets`](https://github.com/fireproofsocks/pockets). Let's install it. @@ -1124,7 +1127,7 @@ Add it to the `deps` section inside `mix.exs`. ```elixir def deps do [ - {:ets, "~> 0.9.0"} + {:pockets, "~> 0.1.0"} ] end ``` @@ -1140,36 +1143,20 @@ For that, create a file in `lib/app/users.ex`. ```elixir defmodule UsersTable do - alias ETS.Set + alias Pockets @table :users_table def init do - Set.new(name: @table) + Pockets.new(@table, "cache.dets") end def list_users do - @table - |> Set.wrap_existing!() - |> Set.to_list() + Pockets.to_map(@table) end - def create_user(%{:stripe_id => stripe_id, :person_id => person_id}) do - @table - |> Set.wrap_existing!() - |> Set.put_new( {person_id, stripe_id, false}) - end - - def update_payment_status(%{:person_id => person_id, :status => new_status}) do - - set = @table - |> Set.wrap_existing!() - - {person_id, stripe_id, _status} = set - |> Set.get!(person_id) - - set - |> Set.put({person_id, stripe_id, new_status}) + def create_user(%{:stripe_id => stripe_id, :person_id => person_id, :status => status}) do + Pockets.put(@table, person_id, %{stripe_id: stripe_id, status: status}) end end @@ -1186,25 +1173,17 @@ All the functions being used are used according to the [`ets` wrapper documentation](https://github.com/TheFirstAvenger/ets). - the `init/0` function creates the table to store our users. - `list_users` returns the list of users. -- `create_user/2` receives a `stripe_id` and `person_id` +- `create_user/3` receives a `stripe_id`, `person_id` and `status` +(pertaining to whether the payment has been made or not) and creates a user object. -By default, the `status` field is created with `false`. -- `update_payment_status/1` receives a user object -and updates accordingly. Let's make use of some of these functions. -We want to setup the `ETS` table on the process startup. +We want to setup the `DETS` table on the process startup. For this, we are going to initiate the table on the `start/1` function inside `lib/app/application.ex`. This functions is executed when the process is created, so it fits right our needs! -Inside this function, add: - -```elixir -# Creating ETS user table -{:ok, _set} = UsersTable.init() -``` Awesome! diff --git a/lib/app/application.ex b/lib/app/application.ex index 915e267..46d3796 100644 --- a/lib/app/application.ex +++ b/lib/app/application.ex @@ -22,8 +22,10 @@ defmodule App.Application do # {App.Worker, arg} ] - # Creating ETS user table - {:ok, _set} = UsersTable.init() + # Creating DETS user table + UsersTable.init() + #UsersTable.create_user(%{person_id: 1, stripe_id: 1, status: false}) + #dbg(UsersTable.list_users()) # See https://hexdocs.pm/elixir/Supervisor.html # for other strategies and supported options diff --git a/lib/app/users.ex b/lib/app/users.ex index 1b7430f..986a465 100644 --- a/lib/app/users.ex +++ b/lib/app/users.ex @@ -1,35 +1,19 @@ defmodule UsersTable do - alias ETS.Set + alias Pockets @table :users_table def init do - Set.new(name: @table) + Pockets.new(@table, "cache.dets") end def list_users do - @table - |> Set.wrap_existing!() - |> Set.to_list() + Pockets.to_map(@table) end - def create_user(%{:stripe_id => stripe_id, :person_id => person_id}) do - @table - |> Set.wrap_existing!() - |> Set.put_new( {person_id, stripe_id, false}) - end - - def update_payment_status(%{:person_id => person_id, :status => new_status}) do - - set = @table - |> Set.wrap_existing!() - - {person_id, stripe_id, _status} = set - |> Set.get!(person_id) - - set - |> Set.put({person_id, stripe_id, new_status}) + def create_user(%{:stripe_id => stripe_id, :person_id => person_id, :status => status}) do + Pockets.put(@table, person_id, %{stripe_id: stripe_id, status: status}) end end diff --git a/mix.exs b/mix.exs index 84d7185..4d6e80e 100644 --- a/mix.exs +++ b/mix.exs @@ -59,7 +59,7 @@ defmodule App.MixProject do {:stripity_stripe, "~> 2.17"}, # Storage - {:ets, "~> 0.9.0"} + {:pockets, "~> 0.1.0"} ] end diff --git a/mix.lock b/mix.lock index 0c527f7..9ba5240 100644 --- a/mix.lock +++ b/mix.lock @@ -43,6 +43,7 @@ "plug": {:hex, :plug, "1.14.0", "ba4f558468f69cbd9f6b356d25443d0b796fbdc887e03fa89001384a9cac638f", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "bf020432c7d4feb7b3af16a0c2701455cbbbb95e5b6866132cb09eb0c29adc14"}, "plug_cowboy": {:hex, :plug_cowboy, "2.6.0", "d1cf12ff96a1ca4f52207c5271a6c351a4733f413803488d75b70ccf44aebec2", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "073cf20b753ce6682ed72905cd62a2d4bd9bad1bf9f7feb02a1b8e525bd94fa6"}, "plug_crypto": {:hex, :plug_crypto, "1.2.3", "8f77d13aeb32bfd9e654cb68f0af517b371fb34c56c9f2b58fe3df1235c1251a", [:mix], [], "hexpm", "b5672099c6ad5c202c45f5a403f21a3411247f164e4a8fab056e5cd8a290f4a2"}, + "pockets": {:hex, :pockets, "0.1.0", "0841c02a4d9a3d2ecba9e027fdeaa94be7371a52b4b1ab62833675a7d7aa17f3", [:mix], [], "hexpm", "a026f6a73f9cac165113135aeea79b65650174eea65fb013406c2103290ecac0"}, "postgrex": {:hex, :postgrex, "0.16.5", "fcc4035cc90e23933c5d69a9cd686e329469446ef7abba2cf70f08e2c4b69810", [:mix], [{:connection, "~> 1.1", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "edead639dc6e882618c01d8fc891214c481ab9a3788dfe38dd5e37fd1d5fb2e8"}, "ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},