I have been struggling for a while to find out if there is some default expiration time set by Rails, in case we don't provide any while storing a key-value pair to memcache?
e.g. Rails.cache.write('some-key', 'some-value')
Would rails set some expiration time by default if we haven't specified?
If you're using the default, built-in MemCacheStore class provided by Rails, then no. It won't assume an expiry time when you create new cache entries. You can read the applicable code to verify that. It checks to see if you've passed an expires_in option to the #write method like
Rails.cache.write("key", "content", expires_in: 2.hours)
and if you haven't, simply passes 0 to memcache indicating no expiry time. Hope this helps!
If you are using the newer (and I think better) Dalli memcached gem, you can configure it at the adapter-level using a line like the following:
config.cache_store = :dalli_store, 'cache-1.example.com', 'cache-2.example.com',
{ :namespace => NAME_OF_RAILS_APP, :expires_in => 1.day}
See the README for a detailed explanation of the :expires_in option. Overall, I think Dalli is worth checking out for more than just this feature, its also faster and supports some newer authentication features, etc.
Related
When Active Storage creates a signed variant URL, it sets a default timeout of 5.minutes. I really want to increase this, but I've been trawling Github issues, code diving and cannot find it anywhere.
On line 44 of the services class a class_attribute is set, but how can this be overwritten?
https://github.com/rails/rails/blob/5-2-stable/activestorage/lib/active_storage/service.rb#L44
I'm using url_for to generate the signed variant links and there doesn't seem to be anyway to change the setting then.
Any help would be greatly appreciated.
Thank you! :)
Set ActiveStorage::Service.url_expires_in directly, e.g. in an initializer:
# config/initializers/active_storage.rb
ActiveStorage::Service.url_expires_in = 1.hour
Rails 6 will add config.active_storage.service_urls_expire_in:
# config/initializers/active_storage.rb
Rails.application.config.active_storage.service_urls_expire_in = 1.hour
The documentation for the file-based cache in rails says:
Note that the cache will grow until the disk is full unless you
periodically clear out old entries.
Unfortunately it doesnt give any information about how to clear old entries periodically. Does setting an appropriate value for :expires_in do the job or is there some other sort of black magic behind clearing the cache?
Also, the documenation gives an option to limit the memory-based cache in size:
config.cache_store = :memory_store, { size: 64.megabytes }
Does this also work for the file based cache? And even more importantly, what happens when the cache growths below that size limit? Does it remove old cached values or will it throw some kind of exception?
thanks in advance,
danijoo
Experimenting with FileStore cache I found that :expires_in options works, but :size one doesn't.
If you want to specify options then you need to also specify the path, try with the following example:
config.cache_store = :file_store, Rails.root.join('tmp', 'cache'), { expires_in: 1.minute }
Put the code in config/application.rb and remember to activate the cache in config/environments/development.rb and restart the app.
P.S. I use 1 minute to easily do a quick test.
Yes the limit applies in case of file-based cache too. And yes a value for :expires_in will do the job.
When this limit is reached, no further stuff will be cached. No exception is thrown.
In Rails 3.2.8, you can write a cookie like this:
cookies[:login] = {
:value => "XJ-122",
:expires => 1.hour.from_now
}
The docs say that these are the available option symbols:
:value
:path
:domain
:expires
:secure
:httponly
I would expect :max_age to be available as an option too, but perhaps user agent support is not widespread enough yet (?) to warrant including it.
So how should I set a cookie's Max-Age in Rails?
I read over the Rails source code for ActionDispatch::Cookies. If you look at how the handle_options method is used you will see that even options not specified in the documentation are passed through. Rails usually passes options around quite liberally, with the philosophy that, somewhere down the line, a method will know what to do with the left-over options.
So, I would suggest that you give it a try with the :max_age option, even though it is not documented, and see what happens.
Note: Rails relies on Rack to set the cookie header, so if for some reason the "Max-Age" "Set-Cookie" header is being passed to Rack but is not being passed through, I would ask over on the Github Rack issue tracker.
Update #1: there has been at least one pull request having to do with Max-Age and Rack, but I'm not sure it is relevant. If the above doesn't work, I think you may want to discuss on the Rack ticket tracker as I mention above.
Update #2: Have you looked at the Rack::Cache middleware? It may be of use.
We have an online store running on Rails 3 Spree platform. Recently customers started reporting weird errors during checkout and after analyzing production logs I found the following error:
Errno::ENAMETOOLONG (File name too long - /var/www/store/tmp/cache/UPS-R43362140-US-NJ-FlorhamPark07932-1025786194_1%7C1025786087_1%7C1025786089_15%7C1025786146_4%7C1025786147_3%7C1025786098_3%7C1025786099_4%7C1025786100_2%7C1025786114_1%7C1025786120_1%7C1025786121_1%7C1025786181_1%7C1025786182_1%7C1025786208_120110412-2105-1e14pq5.lock)
I'm not sure why this file name is so long and if this error is specific to Rails or Spree. Also I'm not very familiar with Rails caching system. I would appreciate any help on how I can resolve this problem.
I'm guessing you are using spree_active_shipping, as that looks like a cache id for a UPS shipping quote. This will happen when someone creates an order that has a lot of line items in it. With enough line items this will of course create a very large filename for the cache, thus giving you the error.
One option would be to use memcache or redis for your Rails.cache instead of using the filesystem cache. Another would be to modify the algorithm that generates the cache_key within app/models/active_shipping.rb in the spree_active_shipping gem.
The latter option would probably be best, and you could simply have the generated cache key run through a hash like MD5 or SHA1. This way you'll get predictable cache key lengths.
Really this should be fixed within spree_active_shipping though, it shouldn't be generating unpredictably long cache keys, even if you a key-value store is used, that's wasted memory.
It is more related to your file system. Either set up a file system which supports longer file names or change the software to make better (md5?timestamp?unique id?) file names.
May be this help:
config.assets.digest and config.assets.debug can't both be true
It's a bug : https://github.com/rails/jquery-rails/issues/33
I am using rails 3.2.x and having same issue. I end up generating MD5 digest in the view helper method used to generate cache key.
FILENAME_MAX_SIZE = 200
def cache_key(prefix, params)
params = Array.wrap(params) if params.instance_of?(String)
key = "#{prefix}/" << params.entries.sort { |a,b| a[0].to_s <=> b[0].to_s }.map { |k,v| "#{k}:#{v}"}.join(',').to_s
if URI.encode_www_form_component(key).size > FILENAME_MAX_SIZE
key = Digest::MD5.hexdigest(key)
end
key
end
Here I have to check length of URI encoded key value using URI.encode_www_form_component(key).size because as you can see in my case, cache key is generated using : and , separators. Rails encodes the key before caching the results.
I took reference from the pull request.
Are you using paperclip gem? If yes, this issue is solved: https://github.com/thoughtbot/paperclip/issues/1246.
Please update your paperclip gem to the latest version.
I have a rails application, with a model that is a kind of repository.
The records stored in the DB for that model are (almost) never changed, but are read all the time. Also there is not a lot of them.
I would like to store these records in cache, in a generic way.
I would like to do something like acts_as_cached, but here are the issue I have:
I can not find a decent documentation for acts as cached (neither can I find it's repository)
I don't want to use memcached, but something simpler (static variable, or something like that).
Do you have any idea of what gems I could use to do that ?
Thanks
EDIT
I am still looking for something similar to cache_flu but without memcached
Could you store the data in a file and load it into a constant (as suggested on Ruby on Rails: Talk):
require "yaml"
class ApplicationController < ActionController::Base
MY_CONFIG = YAML.load(File.read(File.join(RAILS_ROOT, "config", "my_config.yml")))
end
I have started a gem called cache_me which can work with any cache_store
it's in Alpha mode but you can give a try and then open some pull request / Issues.
https://github.com/arunagw/cache_me
I will let you know when it's ready to use full mode.
acts_as_cached was superseded by cache_fu.
You can store data in rails default cache or, as seems to be the most popular choice, use mem_cache_store which uses memcached.
#production.rb
config.cache_store = :mem_cache_store, '127.0.0.1:11211', {:namespace => "production"}
#some_helper.rb
def get_some_data
Rails.cache.fetch('some_reference'){Model.find_some_data}
end
See also:
http://guides.rubyonrails.org/caching_with_rails.html
Also, if you're using passenger you'll need to do this:
if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
if forked
Rails.cache.instance_variable_get(:#data).reset if Rails.cache.class == ActiveSupport::Cache::MemCacheStore
else
# No need to do anything.
end
end
end