class Compostr::EntityCache
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.
Attributes
cpt_class[RW]
full_data[RW]
name_id_map[RW]
uuid_id_map[RW]
Public Class Methods
new(cpt_class)
click to toggle source
cpt_class
has to be descendant of Compostr::CustomPostType
# 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
Public Instance Methods
by_post_id(id)
click to toggle source
# 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()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
Pretty nasty stuff. Stores all posts of cpt in memory (alas, only once) and looks through them until one with uuid found.
# 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)
click to toggle source
Select entities for which given selector returns true example: selector = lambda {|e| e.to_s == 'keepme'}
# File lib/compostr/entity_cache.rb, line 76 def select_by selector full_data.select &selector end
Private Instance Methods
get_all_posts()
click to toggle source
# File lib/compostr/entity_cache.rb, line 99 def get_all_posts Compostr::logger.debug "Get all #{@cpt_class.post_type} posts" all_posts = Compostr::wp.getPosts blog_id: 0, filter: { post_type: @cpt_class.post_type, number: 100_000 } Compostr::logger.debug "Got all #{all_posts.count} #{@cpt_class.post_type} posts" return all_posts end
uuid_pid_map()
click to toggle source
# File lib/compostr/entity_cache.rb, line 107 def uuid_pid_map full_data.map do |post| # TODO lambda uuid = post["custom_fields"].find {|f| f["key"] == "uuid"}&.fetch("value", nil) [uuid, post["post_id"]] end.to_h end