Module: RedisStorageMethods::ClassMethods
- Defined in:
- lib/redis_storage_methods.rb
Instance Method Summary collapse
-
#all_redis ⇒ Object
returns all redis stored objects.
-
#associations_names ⇒ Object
- “videos”, “user”, …
-
plural if an array, singular if a has_one/belongs_to.
-
#create_with_associations_and_redis(params = nil) ⇒ Object
if an object is passed as params, we know we’re only putting this in redis, it already exists in db if params is a hash, know we’re creating in both.
-
#find_with_redis(id) ⇒ Object
populates a model with redis hash note you cannot write with this guy, it won’t let you even though all the fields are set.
-
#put_all_in_redis ⇒ Object
emergency method if redis db was lost, repopulate associations and stuff.
- #redis ⇒ Object
-
#remove_and_return_associations_hash!(params) ⇒ Object
form submits with something like { “fields” => keys… “video_attributes” => { “0” => { “fields” => keys}}} but DataMapper doesnt support nested form submission like active record, so we have to remove these nestings and then handle them appropriately here..We use a class var that the class must set to know what to pull out.
- #sanitize_all(params) ⇒ Object
-
#sync_all_with_redis ⇒ Object
should never get called by client, is method for cronjob to update db.
Instance Method Details
#all_redis ⇒ Object
returns all redis stored objects
84 85 86 87 88 89 90 |
# File 'lib/redis_storage_methods.rb', line 84 def all_redis selfs = [] redis.smembers(self.to_s.downcase.pluralize).each do |id| selfs << self.find_with_redis(id) end selfs end |
#associations_names ⇒ Object
- “videos”, “user”, …
-
plural if an array, singular if a has_one/belongs_to.
122 123 124 |
# File 'lib/redis_storage_methods.rb', line 122 def associations_names raise 'Must be implemented in object class' end |
#create_with_associations_and_redis(params = nil) ⇒ Object
if an object is passed as params, we know we’re only putting this in redis, it already exists in db if params is a hash, know we’re creating in both.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/redis_storage_methods.rb', line 32 def create_with_associations_and_redis(params=nil) params = sanitize_all(params) if params.is_a?(Hash) if params.keys.select{ |k| k if k.to_s.match(/_attributes$/) }.size>0 associations = remove_and_return_associations_hash!(params) me = self.create(params) raise "Invalid #{me.class}: #{me.errors[me.errors.keys.first].first}" unless me.valid? me.create_associations_from_params(associations) else me = self.create(params) # if submits as normal form, not AR form. raise "Invalid #{me.class}: #{me.errors[me.errors.keys.first].first}" unless me.valid? end else me = params end redis.pipelined { redis.del("#{me.class.to_s.downcase}:#{me.id}") #overwrite it, make sure. redis.hmset("#{me.class.to_s.downcase}:#{me.id}", me.assemble_key_value_array_for_redis) redis.sadd("#{me.class.to_s.downcase.pluralize}", me.id) me.create_custom_redis_fields } me.after_redis_create_do return me end |
#find_with_redis(id) ⇒ Object
populates a model with redis hash note you cannot write with this guy, it won’t let you even though all the fields are set.
95 96 97 98 99 100 101 102 103 |
# File 'lib/redis_storage_methods.rb', line 95 def find_with_redis(id) me = self.new redis_attr = redis.hgetall("#{me.class.to_s.downcase}:#{id}") return nil unless redis_attr["id"] me.attributes = redis_attr.reject { |field| field.match(/^\w+\d+\w+$/) or field.match(/^\w+_.*_\w+$/)} me.populate_associations_from_redis(redis_attr.select { |field| field.match(/\w+\d\w+/)}) me.populate_custom_fields_from_extraneous_redis_calls me end |
#put_all_in_redis ⇒ Object
emergency method if redis db was lost, repopulate associations and stuff.
113 114 115 116 117 |
# File 'lib/redis_storage_methods.rb', line 113 def put_all_in_redis self.all.each do |a| a.put_in_redis end end |
#redis ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/redis_storage_methods.rb', line 6 def redis unless @redis @redis ||= Redis.new( :driver => :hiredis, :host => ENV['redis_host'], :port => ENV['redis_port']) @redis.auth(ENV['redis_auth']) if ENV['redis_auth'] end @tries = 0 unless @tries begin @redis.get("test") #tests to see if we're still authed. @tries = 0 #means we can reset our trymeter. rescue Exception => e @tries+=1 if @tries if @tries<5 @redis = nil redis #reset it up. end end return @redis end |
#remove_and_return_associations_hash!(params) ⇒ Object
form submits with something like { “fields” => keys… “video_attributes” => { “0” => { “fields” => keys}}} but DataMapper doesnt support nested form submission like active record, so we have to remove these nestings and then handle them appropriately here..We use a class var that the class must set to know what to pull out.
75 76 77 78 79 80 81 |
# File 'lib/redis_storage_methods.rb', line 75 def remove_and_return_associations_hash!(params) associations = Hash.new associations_names.each do |a| associations[a] = params.delete("#{a.singularize}_attributes") end associations end |
#sanitize_all(params) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/redis_storage_methods.rb', line 59 def sanitize_all(params) #below sanitizes all top level inputs return nil unless params to_ret = nil if params.is_a?(Hash) to_ret = params.inject(Hash.new){ |hash, entry| hash[entry[0]] = entry[1].is_a?(String) ? Sanitize.clean(entry[1]) : sanitize_all(entry[1]); hash} elsif params.is_a?(Array) to_ret = params.inject(Array.new){ |arr, entry| arr << (entry.is_a?(String) ? Sanitize.clean(entry) : sanitize_all(entry)); arr} end return to_ret end |
#sync_all_with_redis ⇒ Object
should never get called by client, is method for cronjob to update db.
106 107 108 109 110 |
# File 'lib/redis_storage_methods.rb', line 106 def sync_all_with_redis self.all.each do |o| o.sync_with_redis end end |