Class WPCacheAnalyzer

java.lang.Object
net.ygbstudio.powerwp4j.engine.WPCacheAnalyzer

public class WPCacheAnalyzer extends Object
WPCacheAnalyzer is a class that provides methods for analyzing the local WordPress cache.

Note that these utilities only retrieve values from the local cache and do not make any requests to the WordPress REST API, that said, you can expect that only items that are currently in use be taken into account in the return values, and accuracy will depend on whether your cache is up-to-date.

Author:
Yoham Gabriel B.
See Also:
  • Constructor Details

    • WPCacheAnalyzer

      public WPCacheAnalyzer()
      Creates an analyzer without loading a cache yet.
    • WPCacheAnalyzer

      public WPCacheAnalyzer(@NotNull @NotNull Path cachePath)
      Creates an analyzer and loads the cache from the given path.
      Parameters:
      cachePath - the path to the cache file
  • Method Details

    • loadLocalCache

      public void loadLocalCache(@NotNull @NotNull Path cachePath)
      Loads the local cache file from the filesystem.
      Parameters:
      cachePath - the path to the cache file
      Throws:
      CacheFileSystemException - if the provided path does not point to an existing file
    • getCacheKeyValueStream

      public Stream<tools.jackson.databind.JsonNode> getCacheKeyValueStream(CacheKeyEnum cacheKey)
      Returns the ArrayNodes from the in-memory local cache filtered by a specified enum that follows the CacheKeyEnum interface.
      Parameters:
      cacheKey - the enum representing the cache key to filter by
      Returns:
      a stream of ArrayNodes containing the filtered cache keys
    • getCacheKeyArrayStream

      public Stream<tools.jackson.databind.node.ArrayNode> getCacheKeyArrayStream(CacheKeyEnum cacheKey)
      Returns the ArrayNodes from the in-memory local cache filtered by a specified enum that follows the CacheKeyEnum interface.
      Parameters:
      cacheKey - the enum representing the cache key to filter by
      Returns:
      a stream of ArrayNodes containing the filtered cache keys
    • getClassValueStream

      public Stream<tools.jackson.databind.node.ArrayNode> getClassValueStream(ClassValueKeyEnum classValues)
      Returns the ArrayNodes from the in-memory local cache filtered by a specified enum that follows the ClassValueKeyEnum interface.
      Parameters:
      classValues - the enum representing the class value to filter by
      Returns:
      a stream of ArrayNodes containing the filtered class values
    • getClassValueCount

      public long getClassValueCount(ClassValueKeyEnum classValues)
      Returns the count of unique class values from the in-memory local cache.
      Parameters:
      classValues - the enum representing the class value to count
      Returns:
      the count of the class values
    • getCacheKeyValueSet

      public <V> Set<V> getCacheKeyValueSet(CacheKeyEnum cacheKey, @NotNull @NotNull Function<? super tools.jackson.databind.JsonNode,? extends V> transformer)
      Returns values extracted from the in-memory local cache filtered by a specified enum that follows the CacheKeyEnum interface.
      Type Parameters:
      V - the type of the values in the returned set
      Parameters:
      cacheKey - the enum representing the cache key to filter by
      transformer - the function used to transform the JsonNode to the desired type
      Returns:
      a set of values extracted from the cache filtered by the specified cache key
    • getCacheSubKeySnapshotStream

      public <V> Stream<WPCacheKeySnapshot<V>> getCacheSubKeySnapshotStream(@NotNull @NotNull Function<? super tools.jackson.databind.JsonNode,? extends V> subKeyTransformer, CacheKeyEnum cacheKey, CacheSubKeyEnum... subKeys)
      Returns WPCacheKeySnapshot instances extracted from the in-memory local cache filtered by a specified cache key. Each WPCacheKeySnapshot instance contains a cache key and a map of cache subkeys and their corresponding values. The values are transformed by the specified function.
      Type Parameters:
      V - the type of the values in the cache subkey map
      Parameters:
      subKeyTransformer - the function used to transform the JsonNode to the desired type V
      cacheKey - the enum representing the cache key to filter by
      subKeys - the cache subkeys to filter by
      Returns:
      a stream of WPCacheKeySnapshot instances extracted from the cache filtered by the specified cache key
    • getCacheKeyCount

      public long getCacheKeyCount(CacheKeyEnum cacheKey)
      Returns the count of unique cache keys from the in-memory local cache.
      Parameters:
      cacheKey - the enum representing the cache key to count
      Returns:
      the count of the cache keys
    • getTagCount

      public long getTagCount()
      Returns the count of unique tags from the in-memory local cache.
      Returns:
      the count of the tags
    • getCategoryCount

      public long getCategoryCount()
      Returns the count of unique categories from the in-memory local cache.
      Returns:
      the count of the categories
    • getPostCount

      public long getPostCount()
      Returns the count of unique posts from the in-memory local cache.
      Returns:
      the count of the posts
    • getSlugCount

      public long getSlugCount()
      Returns the count of unique slugs from the in-memory local cache.
      Returns:
      the count of the slugs
    • getGuidCount

      public long getGuidCount()
      Returns the count of unique GUIDs from the in-memory local cache.
      Returns:
      the count of the GUIDs
    • getContentCount

      public long getContentCount()
      Returns the count of unique content from the in-memory local cache.
      Returns:
      the count of the content
    • getExcerptCount

      public long getExcerptCount()
      Returns the count of unique excerpts from the in-memory local cache.
      Returns:
      the count of the excerpts
    • getLinkCount

      public long getLinkCount()
      Returns the count of unique links from the in-memory local cache.
      Returns:
      the count of the links
    • getSlugs

      public Set<String> getSlugs()
      Returns unique slugs from the in-memory local cache.
      Returns:
      a set of unique slugs
    • getLinks

      public Set<String> getLinks()
      Returns unique links from the in-memory local cache.
      Returns:
      a set of unique links
    • getClassListStream

      public Stream<String> getClassListStream(Predicate<? super String> filterClassListElems)
      Returns class list elements from the in-memory local cache that match the provided filter.
      Parameters:
      filterClassListElems - the predicate to filter the class list elements
      Returns:
      a stream of class list elements that match the provided filter
    • getClassListStream

      public Stream<String> getClassListStream(ClassMarkerEnum classMarker)
      Returns class list elements from the in-memory local cache that match the provided class marker enum.
      Parameters:
      classMarker - the class marker enum to filter the class list elements
      Returns:
      a stream of class list elements that match the provided class marker enum
    • getCleanCategories

      public Set<String> getCleanCategories()
      Returns unique categories from the in-memory local cache.

      The categories are filtered by the TaxonomyMarker.CATEGORY enum and their prefix is removed. Additionally, any non-alphanumeric characters are replaced with spaces.

      If you need custom processing for your categories,
      you can use the getClassListStream(Predicate) method to apply a function and collect the results based on your needs.

      Returns:
      a set of unique categories
    • getRawCategories

      public Set<String> getRawCategories()
      Returns unique categories as found in the in-memory local cache.

      The categories are filtered by the TaxonomyMarker.CATEGORY enum.

      Returns:
      a set of unique categories
    • getCleanTags

      public Set<String> getCleanTags()
      Returns unique tags from the in-memory local cache.

      The tags are filtered by the TaxonomyMarker.TAG enum and their prefix is removed. Additionally, any non-alphanumeric characters are replaced with spaces.

      If you need custom processing for your categories, you can use the getClassListStream(ClassMarkerEnum) method to apply a function and collect the results based on your needs.

      Returns:
      a set of unique tags
    • getRawTags

      public Set<String> getRawTags()
      Returns unique tags as found in the in-memory local cache.

      The tags are filtered by the TaxonomyMarker.TAG enum and their prefix is removed.

      Returns:
      a set of unique tags
    • getContents

      public Set<WPCacheKeySnapshot<String>> getContents()
      Returns WPCacheKeySnapshot instances extracted from the in-memory local cache filtered by the cache key WPCacheKey.

      Each WPCacheKeySnapshot instance contains the cache key and a map of WPCacheSubKey as strings (in this method) and their corresponding values.

      Returns:
      a stream of WPCacheKeySnapshot instances extracted from the cache filtered by the specified cache key
    • getExcerpts

      public Set<WPCacheKeySnapshot<String>> getExcerpts()
      Returns WPCacheKeySnapshot instances extracted from the in-memory local cache filtered by the cache key WPCacheKey enum and containing the "rendered" and "protected" represented by the WPCacheSubKey enum.

      Each WPCacheKeySnapshot instances extracted from the in-memory local instance contains the cache key and a map of WPCacheSubKey as strings (in this method) and their corresponding values.

      Returns:
      a set of WPCacheKeySnapshot instances extracted from the cache filtered by the specified cache key
    • getGuids

      public Set<WPCacheKeySnapshot<String>> getGuids()
      Returns WPCacheKeySnapshot instances extracted from the in-memory local cache filtered by the cache key WPCacheKey.GUID enum and containing the "rendered" represented by the WPCacheSubKey enum.

      Each WPCacheKeySnapshot instance contains the cache key and a map of the present WPCacheSubKey as strings (in this method) and their corresponding values.

      Returns:
      a set of WPCacheKeySnapshot instances extracted from the cache filtered by the specified cache key
    • mapWPClassId

      public Stream<WPClassMapping<String,Long>> mapWPClassId(UnaryOperator<String> transformClassText, ClassMarkerEnum classMarker, ClassValueKeyEnum classValue)
      Returns WPClassMapping instances extracted from the in-memory local cache filtered by the class marker TaxonomyMarker.TAG and the class value TaxonomyValues.TAGS. Each entry contains a tag name and the corresponding value assigned to the term by the WordPress backend.

      Tip: If you do not wish to transform/clean your taxonomy (class) strings, you can pass the UnaryOperator.identity() to ignore the transformation step.

      Parameters:
      transformClassText - transformer applied to each taxonomy/class string before grouping
      classMarker - the class marker enum to extract
      classValue - the class value key enum to extract
      Returns:
      a stream of instances extracted from the cache filtered by the specified cache key
    • groupClassMarkerByPostId

      public Stream<WPClassGroup<Long,String>> groupClassMarkerByPostId(UnaryOperator<String> transformClassMarkers)
      Groups class markers by post ID.

      This method groups class markers by post IDs. It takes a UnaryOperator as a parameter to transform the class markers before grouping. The UnaryOperator receives a string representing the class marker and returns the transformed string.

      Tip: If you do not wish to transform/clean your taxonomy (class) strings, you can pass the UnaryOperator.identity() to ignore the transformation step.

      Parameters:
      transformClassMarkers - the unary operator to transform the class markers
      Returns:
      a stream of WPClassGroup elements, where each key is a post ID and each value is a set of class markers grouped by the post ID
    • groupPostIdsByClassMarker

      public Stream<WPClassGroup<Long,WPClassMapping<String,Long>>> groupPostIdsByClassMarker(ClassMarkerEnum classMarker, ClassValueKeyEnum classValueKey)
      Groups post IDs by class markers and maps them to their corresponding class values.

      This method groups post IDs by class markers and maps each group to its corresponding class values. The grouping is defined by the classMarker parameter, and the mapping is defined by the classValueKey parameter.

      Parameters:
      classMarker - the class marker enum to group by
      classValueKey - the class value key enum to map to
      Returns:
      a stream of WPClassGroup elements where each key is a post ID and each value is a set of WPClassMapping elements which map a class marker to its corresponding class value
    • calculateTermFrequencyByClassValue

      public Long calculateTermFrequencyByClassValue(ClassValueKeyEnum classValueKey, WPClassMapping<String,Long> classMapping)
      Calculates the term frequency of a given class mapping by class value key.

      It utilizes the class numeric value, which is the most accurate way to calculate term frequency.

      Parameters:
      classValueKey - the class value key enum to calculate term frequency by
      classMapping - the class mapping to calculate term frequency for
      Returns:
      the term frequency of the given class mapping by class value key
    • calculateTermFrequencyByClassMarker

      public Long calculateTermFrequencyByClassMarker(ClassMarkerEnum classMarker, WPClassMapping<String,Long> classMapping, boolean containsMatch)
      Calculates the term frequency of a given class mapping by class marker.

      It utilizes the string key of the mapping and matches it with other class markers to calculate the term frequency. If you need to match partial strings, you can set the containsMatch flag to true. Otherwise, it will match the entire string.

      Parameters:
      classMarker - the class marker enum to calculate term frequency by
      classMapping - the class mapping to calculate term frequency for
      containsMatch - whether to match by containment instead of exact equality
      Returns:
      the term frequency of the given class mapping by class marker