Permalink
Browse files

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

…uicker
  • Loading branch information...
1 parent d650d99 commit c09b3f7a01e29338467282a9751a80f47f2bfb52 @maccman maccman committed Oct 13, 2010
Showing with 5 additions and 31 deletions.
  1. +5 −31 lib/supermodel/redis.rb
View
36 lib/supermodel/redis.rb
@@ -5,10 +5,7 @@ def self.extended(base)
base.class_eval do
class_inheritable_array :indexed_attributes
self.indexed_attributes = []
-
- class_inheritable_array :serialized_attributes
- self.serialized_attributes = []
-
+
class_inheritable_hash :redis_options
self.redis_options = {}
end
@@ -29,11 +26,7 @@ def redis
def indexes(*indexes)
self.indexed_attributes += indexes.map(&:to_s)
end
-
- def serialize(*attributes)
- self.serialized_attributes += attributes.map(&:to_s)
- end
-
+
def redis_key(*args)
args.unshift(self.namespace)
args.join(":")
@@ -140,36 +133,17 @@ def indexed_attributes
def redis
self.class.redis
end
-
+
def redis_key(*args)
self.class.redis_key(id, *args)
end
- def serialized_attributes
- self.class.serialized_attributes
- end
-
- def serialize_attribute(key, value)
- return value unless serialized_attributes.include?(key)
- value.to_json
- end
-
- def deserialize_attribute(key, value)
- return value unless serialized_attributes.include?(key)
- value && ActiveSupport::JSON.decode(value)
- end
-
def redis_set
- serializable_hash.each do |(key, value)|
- redis.set(redis_key(key), serialize_attribute(key, value))
- end
+ redis.set(redis_key, serializable_hash.to_json)
end
def redis_get
- known_attributes.each do |key|
- result = deserialize_attribute(key, redis.get(redis_key(key)))
- send("#{key}=", result)
- end
+ ActiveSupport::JSON.decode(redis.get(redis_key))
end
public :redis_get

0 comments on commit c09b3f7

Please sign in to comment.