我想使用memcached存储一个简单的活动记录对象。我知道我需要先将对象转换为JSON,然后再将其保存到memcached我的问题是如何将其拉出来,反序列化并将其用作activerecord关系。我是否必须使用JSON自定义解析器,或者我是否忽略了一些简单的解决方案?
活动记录对象如下所示:
+------+-----+-----------+---------------------------------+---------------------+-------+
| 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 |
| 8943 | 1 | true | Standard Single Private Ensuite | 15.0000000000000000 | 1 |
+------+-----+-----------+---------------------------------+---------------------+-------+
答案 0 :(得分:1)
您不应该担心序列化 - 几乎在所有情况下,都可以为您处理:
#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