rails object to memcached and then out again
I want to store a simple active record object using memcached. I know I need to first convert the object to JSON before saving it to memcached my question is how I can pull it out again, deserialize it and use it as an activerecord relation. Do I have to make a custom parser the JSON or am I overlooking some drop dead easy solution?
The active record object looks like this:
+------+-----+-----------+---------------------------------+---------------------+-------+
| id | ppl | exclusive | name | price | spots |
+------+-----+-----------+---------------------------------+---------------------+-------+
| 8948 | 12 | false | 12 Bed Mixed Dorm | 9.0000000000000000 | 12 |
| 8947 | 10 | false | 10 Bed Mixed Dorm | 9.5000000000000000 | 10 |
| 8946 | 6 | false | 6 Bed Mixed Dorm | 10.0000000000000000 | 6 |
| 8945 | 4 | false | Basic 4 Bed Mixed Dorm | 10.0000000000000000 | 4 |
| 8944 | 2 | true | Twin Private Shared Bathroom | 12.0000000000000000 | 1 开发者_JAVA百科|
| 8943 | 1 | true | Standard Single Private Ensuite | 15.0000000000000000 | 1 |
+------+-----+-----------+---------------------------------+---------------------+-------+
You shouldn't don't need to worry about the serialization -- in almost all cases, this can be handled for you:
#Gemfile
gem install dalli
#config/environments/production.rb
config.cache_store = :dalli_store, '127.0.0.1' #use memcached
#Get id 1245 from model_names
Rails.cache.fetch("ModelName#1245") do
ModelName.find(1245)
end
精彩评论