I'm going to use Dalli cache as key-value store.
Usually in production and development environments we have line
config.cache_store = :dalli_store
so then we can use Rails.cache construction to read from and write to cache.
But in the test environment usually, we don't have this config line.
What is the right way to set up a cache in a test environment in purpose to test my storing logic?
P.S. I'm using Linux(Ubuntu)
dalli is a client for the caching service (memcached)
set it globally whatever the environment, ie in your config/application.rb
config.cache_store = :dalli_store
caching being deactivated in the test environment is a common approach, check config/environments/test.rb
config.action_controller.perform_caching = false
so you can enable it for the test environment, but it could lead to some weird conflicts
best is probably to enable it on the go for a specific specs only:
before do # enable caching
#caching_state = ActionController::Base.perform_caching
ActionController::Base.perform_caching = true
end
after do # disable caching
ActionController::Base.perform_caching = #caching_state
end
I have assumed you are on Ubuntu and did a google of "ubuntu install memcached rails" and found several pages with details. Below are the key points.
To Install memecache
sudo apt-get install memcached
To restart memcahce
/etc/init.d/memcached restart
Related
I am trying to upgrade rails app from v3 to v4. In v3, the server start time is less than a minute. but in v4, it is taking more than 30 mins. In logs, i can see that for each server start,
1/10 preloading assets...
2/10 preloading assets...
.
.
10/10 preloading aseets...
done
is being logged and this is the part taking up 99% of the time. I believe assets are being compiled every time while loading the classes. could someone please let me know which config is causing this ?
I have tried most of the suggested solutions related to asset config in stackoverflow but doesnt seem to get resolved.
current config:
config.cache_classes = false
config.eager_load = false
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.reload_classes_only_on_change = true
config.assets.digest = false
config.assets.debug = false
config.assets.compress = false
The best way to tackle this is to profile your app startup. There are several ways that you can take, some fairly technical, but not invasive (e.g. using DTrace), others more invasive, but easier to do (e.g. monkeypatch require).
This page contains a couple of relevant sources: https://waynechu.cc/posts/196-profiling-rails-boot-time. I'd be surprised if none of the options proposed there help.
Another option can be found here: https://gist.github.com/robdimarco/e610b2b5c31c68bb13fe
The IMO easiest way to use this is as follows:
add ruby-prof to your Gemfile and run bundle install
boot an irb shell with it: bundle exec irb -rruby-prof
run the code from the snippet in the irb shell
The output there should give you a good indication where to start looking.
Thanks for pitching in folks. Just now, got it resolved.
I am using turbo-sprockets-rails-4 in which preload assets is enabled by default for dev environments.
added below config in config/environments/development.rb
TurboSprockets.configure do |config|
config.preloader.enabled = false
end
now server startup time is as fast as rails3
this is mentioned in their readme itself, but missed it while skimming through
I want to show unbundled javascript and css of my web-application to a UI developer. I have tried adding require 'sprockets/railties'
config.assets.debug = true
in my production.rb, but it did not work and I can still see bundled, uglified css/js in my browser sources.
I tried running my production in development mode by adding rack_env development in my /etc/nginx/nginx.conf http block, but I get Bad Request due to following error:-
invalid number of arguments in "rack_env" directive in /etc/nginx/nginx.conf:16
Please help
Did you try purging and recompiling your assets by any chance? Depending on your deploy method, production assets may not be recompiled on every deploy / application start.
rake assets:clean (rake assets:clobber) for Rails 4+
rake assets:precompile
By default, config.assets.debug = true is what controls this 'bundling' behavior.
You might also try to comment out
config.assets.js_compressor = ... or
config.assets.css_compressor = ...
if you have any of those in your production.rb.
Another reason may be any sort of external cache, depending on where you host your app: Cloudflare or Heroku Asset Pipeline Cache (those usually cache based on MD5 of your assets).
And the last but not the least is... browser cache, just in case :)
It should not be necessary to restart Rails server after any normal change. However, when I make little changes on my app controllers, they aren't applied if I don't restart the server. Even if I wrote bad code and made errors intentionally, the old error persists. How can I change that or verify that's well set up?
I have in config/environment/development.rb file:
config.cache_classes = false
This didn't work for me.
UDPATE 1:
development.rb
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
# Asset digests allow you to set far-future HTTP expiration dates on all assets,
# yet still be able to expire them through the digest params.
config.assets.digest = true
# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
end
You can use nodemon for auto restarting your old rails applications.
1) Install nodemon.
sudo npm install -g nodemon
2) Create nodemon.json file on root dir and configure it
{
"ignore": [
".git",
"node_modules/**/node_modules"
],
"watch": [
"app/controllers/",
"app/models/",
"app/assets/",
"config/",
"db/"
],
"ext": "rb yml js css scss"
}
3) Create rails.sh file on root dir to start rails application using nodemon.
kill -9 `cat tmp/pids/server.pid`
echo "APP READY!!!"
echo "Ruby on Rails"
rails s -d
4) Give permission to rails.sh file.
sudo chmod +x rails.sh
5) Start server using sh command.
nodemon -L --exec "./rails.sh"
Note: Steps verified on mac machine. different os may have different command or configuration.
I'm trying to set maxmemory and maxmemory-policy in my cache_store configuration of my Rails app.
I did the following in my production.rb file:
redis_url = "redis://localhost:6379/0"
config.cache_store = :redis_store, redis_url, { :expires_in => 4.weeks ,
:namespace => 'rails-cache',
:maxmemory => '25gb',
'maxmemory-policy' => 'volatile-ttl'}
But the maxmemory doesn't seam to be working. When I do Rails.cache.methods I don't get any methods about memory or max.
I dont' see any examples on the web for Rails, the closest thing was handling redis maxmemory situations with rails when using rails caching but it doesn't give any examples.
I also cloned and grepped for maxmemory in the the redis-rb gem (https://github.com/redis/redis-rb), but nothing comes up. So it seem like it has not been implemented.
If you set the cache store to use redis-rb, and it hasn't implemented maxmemory, I don't see why it'd work.
In particular, it seems like you configure redis's maxmemory in the redis server's config, so I don't think you can do it through a connecting client (ie. redis-rb).
I believe you'd have to set it in redis.conf (or your AWS configuraton) http://redis.io/topics/lru-cache
I have come into an existing Rails project which claims to use memcached. As a test I tried putting an object in the cache with
Rails.cache.write("gateway", #gateway)
Then retrieving it with
Rails.cache.read("gateway", #gateway)
however this returns nil, why is this?
This is in a development environment, memcached is installed and running and should be enabled by the entries config.cache_classes = true and config.action_controller.perform_caching = true.
Rails projects use memcached in various different ways but if you are working on a rails 3 project then I would suggest they may be using the 'dalli' gem which uses a memcached session store. So using the cache could instead be done something like this session[:gateway] = #gateway and the opposite #gateway = session[:gateway] the other way it is done is memcache.set('gateway',#gateway') and memcache.get('gateway')
Would be helpful to see the configuration code. check /config/initializers/session_store.rb for something like Rails.application.config.session_store :dalli_store ............
Also as said in the comments if you are in development caching may be turned off. Check your config/development.rb file for the following:
config.action_controller.perform_caching = false
the other thing is you need to have memcached installed on your os for linux this is sudo apt-get install memcached and can be checked by ps aux | grep memcache (this should show two proccesses the grep and memcache)
Update
Should also check out the rails caching guide