Rails low level cache not working in development mode - ruby-on-rails

I've configured the development.rb file like this:
# Enable/disable caching. By default caching is disabled.
config.action_controller.perform_caching = true
config.cache_store = :memory_store
config.public_file_server.headers = {
'Cache-Control' => 'public, max-age=172800'
}
(erased the default config that checks for a file just to test)
Then I open a rails console to test this but it does not seem to be working:
[1] pry(main)> Rails.cache.write("asd", "asd")
=> true
[2] pry(main)> Rails.cache.read("asd")
=> nil
I've managed to make it work a couple of days ago, but now it is not working.
A I missing something?

I fixed it by restarting spring. Doing
> spring stop
Spring stopped.
> spring start
does the trick.
Note: when starting spring again, it may fail, but it'll work anyway.

In rails 6.1.4
run rails dev:cache to toggle caching in development

Related

Upgrade redis from v2 to v4 on Windows causes Rails connection error

Background
My dev environment is Windows, Rails cache_store worked fine for redis V2.
I am implementing ActiveJob with sidekiq, which required redis >= v3
I installed Redis for Windows from GitHub, I tried both V4 and V5.
Windows setup
Ran the installation .msi file
After install and re-boot, checked the redis-cli and connected and tested fine.
Rail setup
from config/environments/development.rb
config.cache_store = :redis_cache_store, { url: 'redis://localhost:6379/0' }
Rails console
>> Rails.cache
=> #<ActiveSupport::Cache::RedisCacheStore options={:namespace=>nil, :compress=>true, :compress_threshold=>1024, :expires_in=>nil, :race_condition_ttl=>nil} redis=#<Redis client v4.2.5 for redis://localhost:6379/0>>
>> Rails.cache.redis.connected?
=> false
>> t = Rails.cache.fetch('test') { 'T' }
=> "T"
>> r = Redis.new
=> #<Redis client v4.2.5 for redis://127.0.0.1:6379/0>
>> r.get 'test'
=> "123"
>> r.connected?
=> true
Question
What am I doing wrong in my config?
Solved!
Changed development.rb to:
config.cache_store = :redis_cache_store, { url: 'redis://127.0.0.1:6379/0' }

Rails Production log dont update

I'm using Rails 6.0.3
In production I try to run a tail -f on the production log and I don't receive updates when I do any action on the application. Any idea?
I tried to apply 0644 permissions to the production.log file, but that's not the problem, because there is data in the file.
I am using nginx and puma too.
Production Log:
Production log file permissions
-rw-rw-r-- 1 ubuntu ubuntu 733 Apr 25 11:44 production.log
production.rb
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# Code is not reloaded between requests.
config.cache_classes = true
# Eager load code on boot. This eager loads most of Rails and
# your application in memory, allowing both threaded web servers
# and those relying on copy on write to perform better.
# Rake tasks automatically ignore this option for performance.
config.eager_load = true
# Full error reports are disabled and caching is turned on.
config.consider_all_requests_local = false
# Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"]
# or in config/master.key. This key is used to decrypt credentials (and other encrypted files).
# config.require_master_key = true
# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
# Store uploaded files on the local file system (see config/storage.yml for options).
config.active_storage.service = :local
config.cache_store = :redis_cache_store, { url: "redis://localhost:6379/0" }
# Mount Action Cable outside main process or domain.
.....
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
# Use the lowest log level to ensure availability of diagnostic information
# when problems arise.
config.log_level = :debug
# Prepend all log lines with the following tags.
# config.log_tags = [ :request_id ]
# Use a different cache store in production.
# config.cache_store = :mem_cache_store
config.action_mailer.perform_caching = false
# Ignore bad email addresses and do not raise email delivery errors.
# Set this to true and configure the email server for immediate delivery to raise delivery errors.
# config.action_mailer.raise_delivery_errors = false
............
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation cannot be found).
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners.
config.active_support.deprecation = :notify
# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new
# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false
end
Thanks in advance.
I solved my problem by adding these lines to my production file:
config.logger = Logger.new (Rails.root.join ("log / production.log"))
And restarting the cougar on the production server.

Rails Puma: --no-dev-caching does not work

Whenever I change an included .scss file I cannot see the changes, even if I restart the server. I found that if I manually delete tmp/cache/* then restart the server, I can see the changes one time. Further changes are not shown (and the cache is back). I have tried rails server --no-dev-caching but it doesn't help. I have
config.assets.digest = false
config.assets.debug = false
config.assets.quiet = false
config.assets.raise_runtime_errors = true
config.action_controller.perform_caching = false
config.cache_store = :null_store
How can I see changes to my SCSS files when I reload the page in development? I test with curl http://localhost:3000/assets/application.css | less and search for the class that I changed and saved.
=> Booting Puma
=> Rails 5.0.7.2 application starting in development on http://localhost:3000
* Version 3.12.2 (ruby 2.4.3-p205), codename: Llamas in Pajamas
* Environment: development
Docs: https://guides.rubyonrails.org/v5.0/configuring.html#configuring-assets
I commented out Puma, used WebBrick with rails server --no-dev-caching, set config.assets.debug = true, loaded $ curl http://localhost:3000/assets/theme/theme.self.css | less, and the change was still not there.
I found the answer here! https://stackoverflow.com/a/59895954/148844
I had to change #import "theme-overrides.scss"; => #import "theme-overrides";, rename theme-overrides.scss => _theme-overrides.scss, change #import "theme_components/helpers.scss"; => #import "theme_components/helpers";, rename helpers.scss => _helpers.scss, and it all worked!
Now I have to rename 100 different files!

Set HTTP Headers for Files in Public Folder

I want to set Accept-Ranges none for video files that are requested from my public folder in Rails. The video file is not in my assets pipeline so the video simply lives at /public/videos/example.mp4. How can these HTTP headers be set in development mode? I tried editing the config.public_file_server.headers hash in development.rb but I don't believe this is the correct config.
config.public_file_server.headers = {
'Cache-Control' => "public, max-age=15768000",
"Accept-Ranges" => "none"
}
As far as I know in Rails 4, besides Cache-Control no other response header can be set on files. That’s a limitation.
However with changes in Rails 5, you can set any header you want and that is the correct place to do it for development: config.public_file_server.headers in development.rb
However, for changes to take effect, you'll have to create a development cache using rails dev:cache before startig the server.
Demo:
development.rb:
if Rails.root.join('tmp/caching-dev.txt').exist?
config.action_controller.perform_caching = true
config.cache_store = :dalli_store
config.public_file_server.headers = {
'Cache-Control' => 'public, max-age=172800',
'Accept-Ranges' => 'none'
}
else
config.action_controller.perform_caching = false
config.cache_store = :null_store
end
dev-cache and server
$ rails dev:cache
Development mode is now being cached.
$ rails s
Request:
$ curl -sI http://localhost:3000/car-images-silhouettes/back.png | grep Accept-Ranges
Accept-Ranges: none

config.cache_classes=true only for certian domains in production mode in ruby on rails

Hi I would like to use config.cache_classes = true on www.cybercellar.com and config.cache_classes = false on dev2013.cybercellar.com How would I go about this? both are running in production mode.
I tried this in my /app/config/enviroments/production.rb but to no avail
if == "www.cybercellar.com"
config.cache_classes = true
else
config.cache_classes = false
end
but i got this error
undefined local variable or method `request' for main:Object (NameError)
You can create a new environment like production. Steps are,
1 . Add new environment in config/database.yml, the name like Sandbox.
2 . Create sandbox.rb under config/enviroments/. Here load your settings based on domains or anything.

Resources