Class: Ocm::Orm
- Inherits:
-
Object
- Object
- Ocm::Orm
- Defined in:
- lib/ocm/iron_cache_orm.rb
Instance Method Summary collapse
-
#add_alias(key_from, ob_to) ⇒ Object
You can add these to allow multiple ways to look up an object, rather than just a single key.
-
#append_to_list(key, item) ⇒ Object
Warning, this is not a safe operation, be sure it is only being called once at a time.
- #delete(key) ⇒ Object
- #find(clazz, id) ⇒ Object
- #get(key) ⇒ Object
-
#get_alias(clz, key_from) ⇒ Object
returns the object found by the key contained in the alias entry.
- #get_list(key) ⇒ Object
- #increment(key, value = 1) ⇒ Object
-
#initialize(cache) ⇒ Orm
constructor
initialize with a cache object from IronCache::Client, eg: iron_cache.cache(“my_object_cache”).
- #key_for(idable, id = nil) ⇒ Object
-
#prepend_to_list(key, item) ⇒ Object
Warning, this is not a safe operation, be sure it is only being called once at a time.
- #put(key, value, options = {}) ⇒ Object
- #remove(idable, id = nil) ⇒ Object
-
#remove_from_list(key, comps) ⇒ Object
Warning, this is not a safe operation, be sure it is only being called once at a time.
- #save(idable) ⇒ Object
- #save_list(key, array) ⇒ Object
-
#update_in_list(key, item, comps) ⇒ Object
first item that matches comps is replaced with item.
Constructor Details
#initialize(cache) ⇒ Orm
initialize with a cache object from IronCache::Client, eg: iron_cache.cache(“my_object_cache”)
20 21 22 |
# File 'lib/ocm/iron_cache_orm.rb', line 20 def initialize(cache) @cache = cache end |
Instance Method Details
#add_alias(key_from, ob_to) ⇒ Object
You can add these to allow multiple ways to look up an object, rather than just a single key
172 173 174 |
# File 'lib/ocm/iron_cache_orm.rb', line 172 def add_alias(key_from, ob_to) put(key_for(ob_to.class, key_from), key_for(ob_to)) end |
#append_to_list(key, item) ⇒ Object
Warning, this is not a safe operation, be sure it is only being called once at a time
96 97 98 99 100 |
# File 'lib/ocm/iron_cache_orm.rb', line 96 def append_to_list(key, item) = get_list(key) .push(item) put(key, ) end |
#delete(key) ⇒ Object
147 148 149 |
# File 'lib/ocm/iron_cache_orm.rb', line 147 def delete(key) @cache.delete(key) end |
#find(clazz, id) ⇒ Object
108 109 110 |
# File 'lib/ocm/iron_cache_orm.rb', line 108 def find(clazz, id) get(key_for(clazz, id)) end |
#get(key) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ocm/iron_cache_orm.rb', line 112 def get(key) val = @cache.get(key) #puts 'got val: ' + val.inspect if val.nil? puts "GOT NIL for key #{key}" return nil end val = val.value #puts "got from cache #{val}" begin val = JSON.load(val) if val.is_a?(Hash) && val['string'] val = val['string'] end rescue => ex puts "CAUGHT: #{ex.}" end val end |
#get_alias(clz, key_from) ⇒ Object
returns the object found by the key contained in the alias entry
177 178 179 180 181 |
# File 'lib/ocm/iron_cache_orm.rb', line 177 def get_alias(clz, key_from) real_key = get(key_for(clz, key_from)) return nil unless real_key get(real_key) end |
#get_list(key) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/ocm/iron_cache_orm.rb', line 47 def get_list(key) = get(key) #puts 'got messages: ' + messages.inspect if .nil? = [] end end |
#increment(key, value = 1) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/ocm/iron_cache_orm.rb', line 151 def increment(key, value=1) puts 'INC' begin ret = @cache.increment(key, value) puts 'INC RET ' + ret.inspect return ret.value rescue Rest::HttpError, IronCore::ResponseError => ex p ex puts "couldn't increment #{key}" puts ex. p ex.backtrace if ex.code == 404 puts "setting value in cache" settings.iron_cache.put(key, 1) end puts "done increment" return 1 end end |
#key_for(idable, id = nil) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/ocm/iron_cache_orm.rb', line 24 def key_for(idable, id=nil) if id # idable should be a class "#{idable.name.downcase}_#{id}" else "#{idable.class.name.downcase}_#{idable.id}" end end |
#prepend_to_list(key, item) ⇒ Object
Warning, this is not a safe operation, be sure it is only being called once at a time
89 90 91 92 93 |
# File 'lib/ocm/iron_cache_orm.rb', line 89 def prepend_to_list(key, item) = get_list(key) .unshift(item) put(key, ) end |
#put(key, value, options = {}) ⇒ Object
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/ocm/iron_cache_orm.rb', line 132 def put(key, value, ={}) if value.is_a?(String) # need to wrap it value = {string: value}.to_json else value = value.to_json end #puts 'value jsoned: ' + value.inspect @cache.put(key, value, ) end |
#remove(idable, id = nil) ⇒ Object
143 144 145 |
# File 'lib/ocm/iron_cache_orm.rb', line 143 def remove(idable, id=nil) delete(key_for(idable, id)) end |
#remove_from_list(key, comps) ⇒ Object
Warning, this is not a safe operation, be sure it is only being called once at a time
103 104 105 106 |
# File 'lib/ocm/iron_cache_orm.rb', line 103 def remove_from_list(key, comps) #id = item.is_a?(String) ? item : item.id update_in_list(key, nil, comps) end |
#save(idable) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/ocm/iron_cache_orm.rb', line 33 def save(idable) unless idable.id idable.id = Idable.generate_id end if defined? idable. idable. end put(key_for(idable), idable) end |
#save_list(key, array) ⇒ Object
43 44 45 |
# File 'lib/ocm/iron_cache_orm.rb', line 43 def save_list(key, array) put(key, array) end |
#update_in_list(key, item, comps) ⇒ Object
first item that matches comps is replaced with item.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ocm/iron_cache_orm.rb', line 57 def update_in_list(key, item, comps) = get_list(key) Ocm.logger.debug "update_in_list: messages: #{.inspect}" .each_with_index do |m, i| match = true comps.each_pair do |k, v| if m.is_a?(Hash) if m[k.to_s] != v match = false break end else if m.__send__(k.to_sym) != v match = false break end end end if match if item [i] = item else .delete_at(i) end put(key, ) return end end raise "No matching item found in list for #{comps.inspect}" end |