Class WPCacheAnalyzer
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 Summary
ConstructorsConstructorDescriptionCreates an analyzer without loading a cache yet.WPCacheAnalyzer(@NotNull Path cachePath) Creates an analyzer and loads the cache from the given path. -
Method Summary
Modifier and TypeMethodDescriptioncalculateTermFrequencyByClassMarker(ClassMarkerEnum classMarker, WPClassMapping<String, Long> classMapping, boolean containsMatch) Calculates the term frequency of a given class mapping by class marker.calculateTermFrequencyByClassValue(ClassValueKeyEnum classValueKey, WPClassMapping<String, Long> classMapping) Calculates the term frequency of a given class mapping by class value key.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 theCacheKeyEnuminterface.longgetCacheKeyCount(CacheKeyEnum cacheKey) Returns the count of unique cache keys from the in-memory local cache.<V> Set<V> getCacheKeyValueSet(CacheKeyEnum cacheKey, @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 theCacheKeyEnuminterface.Stream<tools.jackson.databind.JsonNode> getCacheKeyValueStream(CacheKeyEnum cacheKey) Returns the ArrayNodes from the in-memory local cache filtered by a specified enum that follows theCacheKeyEnuminterface.<V> Stream<WPCacheKeySnapshot<V>> getCacheSubKeySnapshotStream(@NotNull Function<? super tools.jackson.databind.JsonNode, ? extends V> subKeyTransformer, CacheKeyEnum cacheKey, CacheSubKeyEnum... subKeys) ReturnsWPCacheKeySnapshotinstances extracted from the in-memory local cache filtered by a specified cache key.longReturns the count of unique categories from the in-memory local cache.getClassListStream(Predicate<? super String> filterClassListElems) Returns class list elements from the in-memory local cache that match the provided filter.getClassListStream(ClassMarkerEnum classMarker) Returns class list elements from the in-memory local cache that match the provided class marker enum.longgetClassValueCount(ClassValueKeyEnum classValues) Returns the count of unique class values from the in-memory local cache.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 theClassValueKeyEnuminterface.Returns unique categories from the in-memory local cache.Returns unique tags from the in-memory local cache.longReturns the count of unique content from the in-memory local cache.ReturnsWPCacheKeySnapshotinstances extracted from the in-memory local cache filtered by the cache keyWPCacheKey.longReturns the count of unique excerpts from the in-memory local cache.ReturnsWPCacheKeySnapshotinstances extracted from the in-memory local cache filtered by the cache keyWPCacheKeyenum and containing the"rendered"and"protected"represented by theWPCacheSubKeyenum.longReturns the count of unique GUIDs from the in-memory local cache.getGuids()ReturnsWPCacheKeySnapshotinstances extracted from the in-memory local cache filtered by the cache keyWPCacheKey.GUIDenum and containing the"rendered"represented by theWPCacheSubKeyenum.longReturns the count of unique links from the in-memory local cache.getLinks()Returns unique links from the in-memory local cache.longReturns the count of unique posts from the in-memory local cache.Returns unique categories as found in the in-memory local cache.Returns unique tags as found in the in-memory local cache.longReturns the count of unique slugs from the in-memory local cache.getSlugs()Returns unique slugs from the in-memory local cache.longReturns the count of unique tags from the in-memory local cache.groupClassMarkerByPostId(UnaryOperator<String> transformClassMarkers) Groups class markers by post ID.groupPostIdsByClassMarker(ClassMarkerEnum classMarker, ClassValueKeyEnum classValueKey) Groups post IDs by class markers and maps them to their corresponding class values.voidloadLocalCache(@NotNull Path cachePath) Loads the local cache file from the filesystem.mapWPClassId(UnaryOperator<String> transformClassText, ClassMarkerEnum classMarker, ClassValueKeyEnum classValue) ReturnsWPClassMappinginstances extracted from the in-memory local cache filtered by the class markerTaxonomyMarker.TAGand the class valueTaxonomyValues.TAGS.
-
Constructor Details
-
WPCacheAnalyzer
public WPCacheAnalyzer()Creates an analyzer without loading a cache yet. -
WPCacheAnalyzer
Creates an analyzer and loads the cache from the given path.- Parameters:
cachePath- the path to the cache file
-
-
Method Details
-
loadLocalCache
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
Returns the ArrayNodes from the in-memory local cache filtered by a specified enum that follows theCacheKeyEnuminterface.- Parameters:
cacheKey- the enum representing the cache key to filter by- Returns:
- a stream of ArrayNodes containing the filtered cache keys
-
getCacheKeyArrayStream
Returns the ArrayNodes from the in-memory local cache filtered by a specified enum that follows theCacheKeyEnuminterface.- 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 theClassValueKeyEnuminterface.- Parameters:
classValues- the enum representing the class value to filter by- Returns:
- a stream of ArrayNodes containing the filtered class values
-
getClassValueCount
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 theCacheKeyEnuminterface.- Type Parameters:
V- the type of the values in the returned set- Parameters:
cacheKey- the enum representing the cache key to filter bytransformer- 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) ReturnsWPCacheKeySnapshotinstances extracted from the in-memory local cache filtered by a specified cache key. EachWPCacheKeySnapshotinstance 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 typeVcacheKey- the enum representing the cache key to filter bysubKeys- the cache subkeys to filter by- Returns:
- a stream of
WPCacheKeySnapshotinstances extracted from the cache filtered by the specified cache key
-
getCacheKeyCount
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
Returns unique slugs from the in-memory local cache.- Returns:
- a set of unique slugs
-
getLinks
Returns unique links from the in-memory local cache.- Returns:
- a set of unique links
-
getClassListStream
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
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
Returns unique categories from the in-memory local cache.The categories are filtered by the
TaxonomyMarker.CATEGORYenum 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 thegetClassListStream(Predicate)method to apply a function and collect the results based on your needs.- Returns:
- a set of unique categories
-
getRawCategories
Returns unique categories as found in the in-memory local cache.The categories are filtered by the
TaxonomyMarker.CATEGORYenum.- Returns:
- a set of unique categories
-
getCleanTags
Returns unique tags from the in-memory local cache.The tags are filtered by the
TaxonomyMarker.TAGenum 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
Returns unique tags as found in the in-memory local cache.The tags are filtered by the
TaxonomyMarker.TAGenum and their prefix is removed.- Returns:
- a set of unique tags
-
getContents
ReturnsWPCacheKeySnapshotinstances extracted from the in-memory local cache filtered by the cache keyWPCacheKey.Each
WPCacheKeySnapshotinstance contains the cache key and a map ofWPCacheSubKeyas strings (in this method) and their corresponding values.- Returns:
- a stream of
WPCacheKeySnapshotinstances extracted from the cache filtered by the specified cache key
-
getExcerpts
ReturnsWPCacheKeySnapshotinstances extracted from the in-memory local cache filtered by the cache keyWPCacheKeyenum and containing the"rendered"and"protected"represented by theWPCacheSubKeyenum.Each
WPCacheKeySnapshotinstances extracted from the in-memory local instance contains the cache key and a map ofWPCacheSubKeyas strings (in this method) and their corresponding values.- Returns:
- a set of
WPCacheKeySnapshotinstances extracted from the cache filtered by the specified cache key
-
getGuids
ReturnsWPCacheKeySnapshotinstances extracted from the in-memory local cache filtered by the cache keyWPCacheKey.GUIDenum and containing the"rendered"represented by theWPCacheSubKeyenum.Each
WPCacheKeySnapshotinstance contains the cache key and a map of the presentWPCacheSubKeyas strings (in this method) and their corresponding values.- Returns:
- a set of
WPCacheKeySnapshotinstances extracted from the cache filtered by the specified cache key
-
mapWPClassId
public Stream<WPClassMapping<String,Long>> mapWPClassId(UnaryOperator<String> transformClassText, ClassMarkerEnum classMarker, ClassValueKeyEnum classValue) ReturnsWPClassMappinginstances extracted from the in-memory local cache filtered by the class markerTaxonomyMarker.TAGand the class valueTaxonomyValues.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 groupingclassMarker- the class marker enum to extractclassValue- 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
UnaryOperatoras a parameter to transform the class markers before grouping. TheUnaryOperatorreceives 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
WPClassGroupelements, 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, groupPostIdsByClassMarkerLong>>> (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
classMarkerparameter, and the mapping is defined by theclassValueKeyparameter.- Parameters:
classMarker- the class marker enum to group byclassValueKey- the class value key enum to map to- Returns:
- a stream of
WPClassGroupelements where each key is a post ID and each value is a set ofWPClassMappingelements 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 byclassMapping- 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
containsMatchflag totrue. Otherwise, it will match the entire string.- Parameters:
classMarker- the class marker enum to calculate term frequency byclassMapping- the class mapping to calculate term frequency forcontainsMatch- whether to match by containment instead of exact equality- Returns:
- the term frequency of the given class mapping by class marker
-