Problem with Flask-Caching/ Redis
I'm trying to setup a simple caching mecanism to my Plotly Dash (Flask) application deployed to railway. I'm not being able to setup it correctly. Is this the correct implementation?
Project ID: eda27a88-d8f4-495f-b917-1fd2269f5764
56 Replies
Project ID:
eda27a88-d8f4-495f-b917-1fd2269f5764
please setup a
MONGO_URL
and a REDIS_URL
reference variable in your service variables
https://docs.railway.app/develop/variables#reference-variables
once done please provide a screenshot of your service variablesOk.
Something like this?
please use reference variables, remove them, and have a look at the docs section I linked
My mongo DB must be in the same project?
yes it does need to be
I apologize for my knowledge level.
no worries, that looks good now
now use those variables in your code when connecting to the database
ok. I should use like this:
variable = ${{MongoDB.MONGO_URL}}
or
variable = MONGO_URL
of course option 1, good?
neither of those options are python related
I think for python it's
os.getenv()
but definitely read the docs for thatI will do it. Thanks for you fast and incredible help, really impressed by this support.
It looks like its working. Any way I can assure the redis caching is working?
do something in your app that would put something in the cache, then use the data tab of redis to see if you have any cache entries
Unfortunately no logs and no data saved.
and you dont have any database connection errors?
Only a warning on redis db log
im talking about in your own service
make sure you are either logging a success message or an error message for your database connections
Looks good
can you show me the database connection code?
you are still hard coding database variables, please use environment variables
i will try to comment line 21
and you also aren't logging any success or error messages for the database connections
okay thats looking much better
Here is the updated code logging sucess or error mesages. And the variables arent hard coded
well done
Thanks for your advice.
can i assume you have added the new variables to the service variables? can i see a screenshot?
looking good
and you arent getting any errors in the deployment logs?
all good
how do you run this app locally?
I didnt tried to run locally with the caching mechanism
I would have to change the cache_type to something local aswell
you can still use the railway redis and mongo databases locally
install the railway cli https://docs.railway.app/develop/cli#installation
once you have that all setup and your service linked you can run
railway run <the command you use to start the app locally>
this will run the given command with the railway service variables available
note: do not use any .env files for this, the variables in the .env file will overwrite the variables injected by railway run
this way you could work out any bugs in your code locally without having to deploy to railway for every changeOk, tks for advice
I will run it
👍
Damn, I got some data on Redis running locally with railway CLI
awsome, now go to your railway services settings and disable private networking
done
now do more stuff with the railway hosted app and see if you get more data in redis
yeah thats working
means its solved...
i believe
awsome!
Brody, you are a monster... I wish I had 10% of your knowledge. Awesome job! Kiss on your cheek and my regards from Brazil.
I can't be thankfull enought.
So its a good practice to build using railway CLI and railway env variables
it sure is, now you have no need for an .env file since you can safely store everything in your service variables
are a monster of computing
thank you 🙂
sorry to bother again, but why private networking was blocking my redis db?
good question, private networking takes 2 seconds to initialize and if your database connection timeout isnt set high enough funky things can happen, you wernt using private networking so the easiest option was simply disabling it
tks! you would do a great teacher. You can consider this ticket closed. Tks a lot again.