fireproofsocks
I’m new to Elixir and Phoenix and I’m trying to implement Cachex. I’m struggling to get this working. I added the dependency to my mix.exs and I modified my application function so it references the Cachex app:
def application do
[
mod: {MyApp.Application, []},
extra_applications: [:logger, :runtime_tools],
applications: [:cachex]
]
end
deps.get downloaded the package and that all seems to be working.
In my application.ex, I have tried to add the appropriate block to the Supervisor.start_link:
def start(_type, _args) do
import Supervisor.Spec
# Define workers and child supervisors to be supervised
children = [
supervisor(MyApp.Repo, []),
supervisor(MyApp.Endpoint, []),
worker(Cachex, [:my_cache, []]),
]
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end
As soon as I try to run the server, I get an error:
mix phx.server
Compiling 16 files (.ex)
Generated my_app app
=INFO REPORT==== 12-Feb-2018::15:32:28 ===
application: logger
exited: stopped
type: temporary
** (Mix) Could not start application my_app: MyApp.Application.start(:normal, []) returned an error: shutdown: failed to start child: MyApp.Repo
** (EXIT) exited in: GenServer.call(Ecto.Registry, {:associate, #PID<0.367.0>, {MyApp.Repo, MyApp.Repo.Pool, [name: MyApp.Repo.Pool, otp_app: :my_app, repo: MyApp.Repo, timeout: 15000, pool_timeout: 5000, adapter: Ecto.Adapters.MySQL, username: "my_user", password: "xxxxx", database: "my_db", hostname: "my.host.tld", pool_size: 10, pool: DBConnection.Poolboy]}}, 5000)
** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
I’m not sure why it is choking. I can at least compile the app and start the server if I add :cachex to the “extra_applications” instead of to the “applications” bit. Can someone shed some light on this? Is it viable to use extra_applications instead?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 17 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
OvermindDL1
It’s in the error handling section of the phoenix docs, you can either explicitly state what error in the conn and then return the conn, or you can raise an exception that specifies to phoenix what error to return.
fireproofsocks
This is really helpful. By avoiding the “!”, the requests for which no record exists will return an error. However, this results in an HTTP 500 error… how can I change that so it returns a 404 (or any other http status code)? Are there special atoms that trigger that in Phoenix? How does that bit of error handling work?
outlog
it does blow up with a bang.. so a bit more defensive code is needed, unless you are doing bangs on purpose..
if you simply remove the bang and use get_by it will return nil on not found, and that will be cached - depending on your caching strategy you might not want that - and you can use the ignore feature.. that way the nil/not found is not cached and the db is always queried on slugs that are not found - but it all depends..
here is an example.. using :ignore so the nil is not cached.. else just remove the bang in get_by
outlog
I was thinking @fireproofsocks could test it for us
[quote=“OvermindDL1, post:14, topic:12458”]
@outlog Fantastic question! I don’t actually know, I don’t throw exceptions or use exception throwing functions… ^.^;
[/quote]
well me neither - but could save some lines of code here if cachex does indeed swallow the get_by!
OvermindDL1
@outlog Fantastic question! I don’t actually know, I don’t throw exceptions or use exception throwing functions… ^.^;
/me wraps about everything that can throw exception or errors to return error tuples instead, very much prefer error tuples except in truly exceptional error conditions, which a database failure is not
You should test it?
outlog
one question.. does Cachex swallow the get_by bang (get_by!) in case it doesn’t find any?
Repo.get_by!(Mything, slug: slug)or does it blow up?
fireproofsocks
Thank you for your patient guidance – it helps, and in retrospect, it all seems obvious.
OvermindDL1
I found that doc by going to Cachex’s dependency hex page (the first page I go to for any library):
Then I clicked the link that stated
Online documentation, which took me to:Which I then switched to night mode because Ow My Eyes… o.O
It shows two modules, they are:
Cachex
Cachex.Disk
I then clicked on the top one as it’s the main interface for it, which took me to:
In the functions section I clicked the
getfunction and it took me to the link I linked.‘Almost’ every hex library has their docs on hexdocs at a path of
https://hexdocs.pm/<library_name>for note, which is linked from Hex as well (specific versions if you want too).But yep, it’s linked on the front-most hex page of the library itself.
fireproofsocks
Honestly, I didn’t see that page – note that the links from GitHub - whitfin/cachex: A powerful caching library for Elixir with support for transactions, fallbacks and expirations · GitHub do not point to that page, and starting from https://hexdocs.pm/ did not lead me there in any sort of purposeful way (i.e. if you didn’t know what you were looking for already, it’s unlikely you’d end up there), so I hopefully can be forgiven for not finding it. Also, the examples there do not include code for handling the tuples. Yes, of course, if you know what you’re doing that’s a trivial omission, but for noobs, that’s one more snare that might get you. I hope I’m not just speaking for myself by wanting the example to include just a bit more context.
Thank you for the suggestions re idiomatic code! Very helpful and much appreciated!
OvermindDL1
It’s already documented at:
It works fine but not very idiomatic, I’d personally do it like (well I generally have the fallback built into the cache rather than on each call, but ignoring that part):
Or maybe using ok/error tuples, whichever.