Class: FMCache::Engine
- Inherits:
-
Object
- Object
- FMCache::Engine
- Defined in:
- lib/fmcache/engine.rb
Constant Summary collapse
- DEFAULT_TTL =
7 days
7 * 24 * 3600
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#decoder ⇒ Object
readonly
Returns the value of attribute decoder.
-
#encoder ⇒ Object
readonly
Returns the value of attribute encoder.
-
#fm_parser ⇒ Object
readonly
Returns the value of attribute fm_parser.
Instance Method Summary collapse
- #delete(ids:) ⇒ Object
- #fetch(ids:, field_mask:, skip_write: false) {|ids,| ... } ⇒ <Hash>
-
#initialize(client:, fm_parser:, ttl: DEFAULT_TTL, notifier: nil, json_serializer: nil, id_key_prefix: nil, save_condition: nil) ⇒ Engine
constructor
A new instance of Engine.
- #read(ids:, field_mask:) ⇒ <Hash>, IncompleteInfo
- #write(values:, field_mask:) ⇒ Boolean
Constructor Details
#initialize(client:, fm_parser:, ttl: DEFAULT_TTL, notifier: nil, json_serializer: nil, id_key_prefix: nil, save_condition: nil) ⇒ Engine
Returns a new instance of Engine.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/fmcache/engine.rb', line 12 def initialize( client:, fm_parser:, ttl: DEFAULT_TTL, notifier: nil, json_serializer: nil, id_key_prefix: nil, save_condition: nil ) @client = Client.new(client, notifier) @fm_parser = wrap(fm_parser) @ttl = ttl @id_key_gen = IdKeyGen.new(id_key_prefix) @encoder = Encoder.new(@id_key_gen) @decoder = Decoder.new(@fm_parser) @jsonizer = Jsonizer.new(json_serializer) @save_condition = save_condition end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
31 32 33 |
# File 'lib/fmcache/engine.rb', line 31 def client @client end |
#decoder ⇒ Object (readonly)
Returns the value of attribute decoder.
31 32 33 |
# File 'lib/fmcache/engine.rb', line 31 def decoder @decoder end |
#encoder ⇒ Object (readonly)
Returns the value of attribute encoder.
31 32 33 |
# File 'lib/fmcache/engine.rb', line 31 def encoder @encoder end |
#fm_parser ⇒ Object (readonly)
Returns the value of attribute fm_parser.
31 32 33 |
# File 'lib/fmcache/engine.rb', line 31 def fm_parser @fm_parser end |
Instance Method Details
#delete(ids:) ⇒ Object
96 97 98 99 |
# File 'lib/fmcache/engine.rb', line 96 def delete(ids:) ids = ids.map(&:to_i) client.del(keys: @id_key_gen.to_keys(ids)) end |
#fetch(ids:, field_mask:, skip_write: false) {|ids,| ... } ⇒ <Hash>
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/fmcache/engine.rb', line 67 def fetch(ids:, field_mask:, skip_write: false, &block) ids = ids.map(&:to_i) normalize!(field_mask) values, incomplete_values, incomplete_info = read(ids: ids, field_mask: field_mask) return values if incomplete_info.ids.size == 0 # NOTE: get new data d = block.call(incomplete_info.ids, incomplete_info.field_mask) write(values: d, field_mask: incomplete_info.field_mask) unless skip_write older = encode(incomplete_values, field_mask) newer = encode(d, incomplete_info.field_mask) v, i_v, i_i = decode(older.deep_merge(newer), field_mask) if i_i.ids.size == 0 r = values + v + i_v else # NOTE: Fallback to block.call with full field_mask d2 = block.call(i_i.ids, field_mask) write(values: d2, field_mask: field_mask) unless skip_write r = values + d2 end Helper.sort(r, ids) end |
#read(ids:, field_mask:) ⇒ <Hash>, IncompleteInfo
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/fmcache/engine.rb', line 46 def read(ids:, field_mask:) ids = ids.map(&:to_i) normalize!(field_mask) keys = @id_key_gen.to_keys(ids) fields = Helper.to_fields(field_mask) h = client.get(keys: keys, fields: fields) with_id, with_no_id = split(h) v, i_v, i_i = decode(@jsonizer.dejsonize(with_id), field_mask) with_no_id_list = @id_key_gen.to_ids(with_no_id.keys) return v, i_v, merge(i_i, with_no_id_list, field_mask) end |
#write(values:, field_mask:) ⇒ Boolean
36 37 38 39 40 41 |
# File 'lib/fmcache/engine.rb', line 36 def write(values:, field_mask:) values = values.select { |value| @save_condition.call(value) } if @save_condition normalize!(field_mask) h = encode(values, field_mask) client.set(values: @jsonizer.jsonize(h), ttl: @ttl) end |