Class: JOffice::RedisClientBase
- Inherits:
-
Object
- Object
- JOffice::RedisClientBase
- Defined in:
- lib/joffice_redis/redis_client_base.rb
Constant Summary collapse
- DEFAULT_KEYS_TO_SAVE =
[:id].freeze
Instance Method Summary collapse
- #[](k) ⇒ Object
- #[]=(k, value) ⇒ Object
- #add_or_update(model) ⇒ Object
- #build_key(model) ⇒ Object
- #db ⇒ Object
- #db_name ⇒ Object
- #delete_all ⇒ Object
- #exists?(k) ⇒ Boolean
- #flush_db ⇒ Object
- #get(id, raw = true) ⇒ Object
- #key(hash_key) ⇒ Object
- #keys_to_save ⇒ Object
- #load_by_range(key, ranges) ⇒ Object
- #load_values(id, *keys) ⇒ Object
- #model_id(model) ⇒ Object
- #prefix ⇒ Object
- #set(id, value, raw = true) ⇒ Object
- #update_attribute(id, key, value) ⇒ Object
- #update_attributes(model, attributes) ⇒ Object
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 |
#db ⇒ Object
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_name ⇒ Object
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_all ⇒ Object
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
34 35 36 |
# File 'lib/joffice_redis/redis_client_base.rb', line 34 def exists?(k) db.exists?(key(k)) end |
#flush_db ⇒ Object
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_save ⇒ Object
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 |
#prefix ⇒ Object
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 |