method

write

Importance_2
v7.1.3.2 - Show latest stable - 1 note - Class: ActiveSupport::Cache::MemCacheStore
write public

Behaves the same as ActiveSupport::Cache::Store#write, but supports additional options specific to memcached.

Additional Options

  • raw: true - Sends the value directly to the server as raw bytes. The value must be a string or number. You can use memcached direct operations like increment and decrement only on raw values.

  • unless_exist: true - Prevents overwriting an existing cache entry.

Show source
Register or log in to add new notes.
October 2, 2008
1 thank

Setting ttl

Use the expires_in option to set a TTL for the cached item.

Rails.cache.write("top_items", :expires_in => 5.minutes)

You can use this with the fetch method as well:

Rails.cache.fetch("top_items", :expires_in => 5.minutes) do
  # some very expensive calculations
end

Note: this only works with supporting cache stores, like the MemCacheStore