Class: Compostr::EntityCache
- Inherits:
-
Object
- Object
- Compostr::EntityCache
- Defined in:
- lib/compostr/entity_cache.rb
Overview
An EntityCache stores a wordpress response to the query of a Custom Post Type. All is done in memory, which is pretty nasty. The EntityCache creates two indexes (on name and uuid attribute/custom field) which are created on-the fly when accessed. Contrary to what its name suggests, the EntityCache does not store instances of the given CustomPostType subclass, but the wordpress response.
Instance Attribute Summary collapse
-
#cpt_class ⇒ Object
Returns the value of attribute cpt_class.
-
#full_data ⇒ Object
Burn-in cache.
-
#name_id_map ⇒ Object
init and return @name_id_map.
-
#uuid_id_map ⇒ Object
Returns the value of attribute uuid_id_map.
Instance Method Summary collapse
- #by_post_id(id) ⇒ Object
- #flush ⇒ Object
- #id_of_name(name) ⇒ Object
- #id_of_names(names) ⇒ Object
- #id_of_uuid(uuid) ⇒ Object
- #id_of_uuids(uuids) ⇒ Object
-
#in_mem_lookup(uuid) ⇒ Object
Pretty nasty stuff.
-
#initialize(cpt_class) ⇒ EntityCache
constructor
cpt_class has to be descendant of Compostr::CustomPostType.
-
#select_by(selector) ⇒ Object
Select entities for which given selector returns true example: selector = lambda {|e| e.to_s == ‘keepme’}.
Constructor Details
#initialize(cpt_class) ⇒ EntityCache
cpt_class has to be descendant of Compostr::CustomPostType
13 14 15 16 17 18 19 20 |
# File 'lib/compostr/entity_cache.rb', line 13 def initialize cpt_class @cpt_class = cpt_class @name_id_map = nil @uuid_id_map = nil if !(@cpt_class < Compostr::CustomPostType) raise "Unsupported Entity for EntityCache: #{@cpt_class}" end end |
Instance Attribute Details
#cpt_class ⇒ Object
Returns the value of attribute cpt_class.
9 10 11 |
# File 'lib/compostr/entity_cache.rb', line 9 def cpt_class @cpt_class end |
#full_data ⇒ Object
Burn-in cache
70 71 72 |
# File 'lib/compostr/entity_cache.rb', line 70 def full_data @full_data end |
#name_id_map ⇒ Object
init and return @name_id_map
61 62 63 |
# File 'lib/compostr/entity_cache.rb', line 61 def name_id_map @name_id_map end |
#uuid_id_map ⇒ Object
Returns the value of attribute uuid_id_map.
9 10 11 |
# File 'lib/compostr/entity_cache.rb', line 9 def uuid_id_map @uuid_id_map end |
Instance Method Details
#by_post_id(id) ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/compostr/entity_cache.rb', line 80 def by_post_id id if @full_data.nil? Compostr::wp.getPost blog_id: 0, post_id: id else @full_data.find do |p| p["post_id"] == id end end end |
#flush ⇒ Object
91 92 93 94 95 |
# File 'lib/compostr/entity_cache.rb', line 91 def flush @name_id_map = nil @uuid_id_map = nil @full_data = nil end |
#id_of_name(name) ⇒ Object
40 41 42 43 |
# File 'lib/compostr/entity_cache.rb', line 40 def id_of_name name return [] if name.nil? || name.empty? name_id_map[name] end |
#id_of_names(names) ⇒ Object
45 46 47 48 |
# File 'lib/compostr/entity_cache.rb', line 45 def id_of_names names return [] if names.nil? || names.empty? names.map{|name| name_id_map[name]} end |
#id_of_uuid(uuid) ⇒ Object
50 51 52 53 |
# File 'lib/compostr/entity_cache.rb', line 50 def id_of_uuid uuid return [] if uuid.nil? || uuid.empty? uuid_id_map[uuid] end |
#id_of_uuids(uuids) ⇒ Object
55 56 57 58 |
# File 'lib/compostr/entity_cache.rb', line 55 def id_of_uuids uuids return [] if uuids.nil? || uuids.empty? uuids.map{|uuid| id_of_uuid uuid} end |
#in_mem_lookup(uuid) ⇒ Object
Pretty nasty stuff. Stores all posts of cpt in memory (alas, only once) and looks through them until one with uuid found.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/compostr/entity_cache.rb', line 24 def in_mem_lookup uuid # TODO index (hash) on (uu)id, access index only if full_data.length == 10_000 # warn heavily elsif full_data.length > 1000 # warn softly end uuid_selector = lambda do |x| x["custom_fields"].find do |f| f["key"] == "uuid" && f["value"] == uuid end end full_data.find &uuid_selector #@full_data.find &WPEvent::Lambdas.with_cf_uuid(uuid) end |
#select_by(selector) ⇒ Object
Select entities for which given selector returns true example: selector = lambda {|e| e.to_s == ‘keepme’}
76 77 78 |
# File 'lib/compostr/entity_cache.rb', line 76 def select_by selector full_data.select &selector end |