Class: JOffice::RedisClientBase

Inherits:
Object
  • Object
show all
Defined in:
lib/joffice_redis/redis_client_base.rb

Constant Summary collapse

DEFAULT_KEYS_TO_SAVE =
[:id].freeze

Instance Method Summary collapse

Instance Method Details

#[](k) ⇒ Object



26
27
28
# File 'lib/joffice_redis/redis_client_base.rb', line 26

def [](k)
  db.get(key(k))
end

#[]=(k, value) ⇒ Object



30
31
32
# File 'lib/joffice_redis/redis_client_base.rb', line 30

def []=(k, value)
  db.set!(key(k), value)
end

#add_or_update(model) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/joffice_redis/redis_client_base.rb', line 94

def add_or_update(model)
  id=build_key(model)
  keys_to_save.each do |key|
    db.tail_push!(id, Marshal.dump(model.send(key)))
  end
  db.ltrim!(id, 0, keys_to_save.length)
end

#build_key(model) ⇒ Object



79
80
81
# File 'lib/joffice_redis/redis_client_base.rb', line 79

def build_key(model)
  key(model_id(model))
end

#dbObject



46
47
48
# File 'lib/joffice_redis/redis_client_base.rb', line 46

def db
  @db||=JOffice::RedisManager.instance.open_db_em(db_name)
end

#db_nameObject



42
43
44
# File 'lib/joffice_redis/redis_client_base.rb', line 42

def db_name
  raise "Missed db name in class #{self}"
end

#delete_allObject



38
39
40
# File 'lib/joffice_redis/redis_client_base.rb', line 38

def delete_all
  db.del!(db.keys("#{prefix}*"))
end

#exists?(k) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/joffice_redis/redis_client_base.rb', line 34

def exists?(k)
  db.exists?(key(k))   
end

#flush_dbObject



62
63
64
# File 'lib/joffice_redis/redis_client_base.rb', line 62

def flush_db
  db.flush_db!
end

#get(id, raw = true) ⇒ Object



74
75
76
77
# File 'lib/joffice_redis/redis_client_base.rb', line 74

def get(id, raw=true)      
  value=db.get(key(id))
   ((raw || !value) ? value : Marshal.load(value))
end

#key(hash_key) ⇒ Object



54
55
56
# File 'lib/joffice_redis/redis_client_base.rb', line 54

def key(hash_key)
  "#{prefix}#{hash_key.to_s.gsub(/ /,'_')}"      
end

#keys_to_saveObject



58
59
60
# File 'lib/joffice_redis/redis_client_base.rb', line 58

def keys_to_save
  DEFAULT_KEYS_TO_SAVE
end

#load_by_range(key, ranges) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/joffice_redis/redis_client_base.rb', line 102

def load_by_range(key, ranges)
  result=[]
  ranges.each do |range|        
    db.lrange(key,range.first,range.last).each {|v| result << Marshal.load(v);   }        
  end
  result
end

#load_values(id, *keys) ⇒ Object



110
111
112
# File 'lib/joffice_redis/redis_client_base.rb', line 110

def load_values(id, *keys)
  load_by_range(key(id), to_index_range(keys))      
end

#model_id(model) ⇒ Object



83
84
85
# File 'lib/joffice_redis/redis_client_base.rb', line 83

def model_id(model)
  model.id  
end

#prefixObject



50
51
52
# File 'lib/joffice_redis/redis_client_base.rb', line 50

def prefix
  ''
end

#set(id, value, raw = true) ⇒ Object



70
71
72
# File 'lib/joffice_redis/redis_client_base.rb', line 70

def set(id, value, raw=true)      
  db.set!(key(id), raw ? value : Marshal.dump(value))
end

#update_attribute(id, key, value) ⇒ Object



66
67
68
# File 'lib/joffice_redis/redis_client_base.rb', line 66

def update_attribute(id, key, value)			
  db.lset!("#{prefix}#{id}", keys_to_save.index(key), Marshal.dump(value))
end

#update_attributes(model, attributes) ⇒ Object



87
88
89
90
91
92
# File 'lib/joffice_redis/redis_client_base.rb', line 87

def update_attributes(model, attributes)
  id=model_id(model)
  attributes.each do |name|
    update_attribute(id, name, model.send(name))
  end
end