Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

use JSON for serializing attributes with Redis - ends up being much q…

…uicker
  • Loading branch information...
commit c09b3f7a01e29338467282a9751a80f47f2bfb52 1 parent d650d99
Alex MacCaw maccman authored

Showing 1 changed file with 5 additions and 31 deletions. Show diff stats Hide diff stats

  1. +5 31 lib/supermodel/redis.rb
36 lib/supermodel/redis.rb
@@ -5,10 +5,7 @@ def self.extended(base)
5 5 base.class_eval do
6 6 class_inheritable_array :indexed_attributes
7 7 self.indexed_attributes = []
8   -
9   - class_inheritable_array :serialized_attributes
10   - self.serialized_attributes = []
11   -
  8 +
12 9 class_inheritable_hash :redis_options
13 10 self.redis_options = {}
14 11 end
@@ -29,11 +26,7 @@ def redis
29 26 def indexes(*indexes)
30 27 self.indexed_attributes += indexes.map(&:to_s)
31 28 end
32   -
33   - def serialize(*attributes)
34   - self.serialized_attributes += attributes.map(&:to_s)
35   - end
36   -
  29 +
37 30 def redis_key(*args)
38 31 args.unshift(self.namespace)
39 32 args.join(":")
@@ -140,36 +133,17 @@ def indexed_attributes
140 133 def redis
141 134 self.class.redis
142 135 end
143   -
  136 +
144 137 def redis_key(*args)
145 138 self.class.redis_key(id, *args)
146 139 end
147 140
148   - def serialized_attributes
149   - self.class.serialized_attributes
150   - end
151   -
152   - def serialize_attribute(key, value)
153   - return value unless serialized_attributes.include?(key)
154   - value.to_json
155   - end
156   -
157   - def deserialize_attribute(key, value)
158   - return value unless serialized_attributes.include?(key)
159   - value && ActiveSupport::JSON.decode(value)
160   - end
161   -
162 141 def redis_set
163   - serializable_hash.each do |(key, value)|
164   - redis.set(redis_key(key), serialize_attribute(key, value))
165   - end
  142 + redis.set(redis_key, serializable_hash.to_json)
166 143 end
167 144
168 145 def redis_get
169   - known_attributes.each do |key|
170   - result = deserialize_attribute(key, redis.get(redis_key(key)))
171   - send("#{key}=", result)
172   - end
  146 + ActiveSupport::JSON.decode(redis.get(redis_key))
173 147 end
174 148 public :redis_get
175 149

0 comments on commit c09b3f7

Please sign in to comment.
Something went wrong with that request. Please try again.