ubuntu, ror, jQuery, css, website memo
太嫩, 沒啥心得, 用來紀錄每天學的
2008年11月14日 星期五
cache in rails 2.1
http://railscasts.com/episodes/115-caching-in-rails-2-1
http://thewebfellas.com/blog/2008/6/9/rails-2-1-now-with-better-integrated-caching
cache的用法
# Write a string to the cache
Rails.cache.write('test_key', 'test_value')
=> true
# Read it back in
Rails.cache.read('test_key')
=> 'test_value'
# Delete the item
Rails.cache.delete('test_key')
=> true
#find, if does not exist create
Rails.cache.fetch('now') { Time.now}
有幾種cache 機制
# Use the memory store - this store has no options
config.cache_store = :memory_store
# Use the file store with a custom storage path (if the directory doesn’t already exist it will be created)
config.cache_store = :file_store, '/my_cache_path'
# Use the memcached store with default options (localhost, TCP port 11211)
config.cache_store = :mem_cache_store
# Use the memcached store with an options hash
config.cache_store = :mem_cache_store, 'localhost', '192.168.1.1:1001', :namespace => 'test'
自己生個cache object
# Create a separate memory cache in addition to the global Rails cache
my_cache = ActiveSupport::Cache.lookup_store(:memory_cache)
幫助你生cache_key
>> u = User.first
=> #
>> u.cache_key
=> "users/1-20081114035214"
memcachestore 是production 常選用的方式, default 存入是存成marshal
# Write a raw value to the cache
Rails.cache.write('test_key', 1, :raw => true)
# Read a raw value from the cache
Rails.cache.read('test_key', :raw => true)
# Write a value using the add method and setting expiry to 15 minutes
Rails.cache.write('test_key', 'test_value', :unless_exist => true, :expires_in => 15.minutes)
沒有留言:
張貼留言
較新的文章
較舊的文章
首頁
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言