Package net.ygbstudio.powerwp4j.engine
Class WPCacheManager
java.lang.Object
net.ygbstudio.powerwp4j.engine.WPCacheManager
WPCacheManager is the main class for interacting with a WordPress site. It provides methods for
fetching data from the WordPress REST API and caching it locally using JSON.
- Author:
- Yoham Gabriel B.
-
Constructor Summary
ConstructorsConstructorDescriptionWPCacheManager(@NotNull String fullyQualifiedDomainName, @NotNull String username, @NotNull String applicationPassword) Initializes a new instance of the WPCacheManager class.WPCacheManager(@NotNull String fullyQualifiedDomainName, @NotNull String username, @NotNull String applicationPassword, @Nullable Path cachePath) Initializes a new instance of the WPCacheManager class.WPCacheManager(@NotNull WPSiteInfo siteInfo) Initializes a new instance of the WPCacheManager class using the providedWPSiteInfoobject.WPCacheManager(@NotNull WPSiteInfo siteInfo, Path cachePath) Initializes a new instance of the WPCacheManager class using the providedWPSiteInfoobject and an optional cache path. -
Method Summary
Modifier and TypeMethodDescriptionReturns the base URL of the WordPress REST API.booleanPerforms a cache synchronization between the local cache file and the WordPress REST API.booleancacheSync(boolean ignoreSSL) Synchronizes the cache file with the WordPress site.@NotNull Optional<HttpResponse<String>> connectWP(@NotNull Map<QueryParamEnum, String> queryParams, @NotNull WPRestPath pathParam) Connects to the WordPress REST API and returns the response.voidfetchCache(@NotNull Path cachePath, boolean overwriteCache, boolean ignoreSSLHandshakeException) Fetches the local cache file from the WordPress REST API.voidfetchCacheFromInstancePath(boolean overwriteCache, boolean ignoreSSLHandshakeException) Fetches the local cache file from the WordPress REST API.Returns a new WPCacheAnalyzer instance using the cache file specified at construction time.wpUser()Returns the user name for the WordPress site.
-
Constructor Details
-
WPCacheManager
public WPCacheManager(@NotNull @NotNull String fullyQualifiedDomainName, @NotNull @NotNull String username, @NotNull @NotNull String applicationPassword, @Nullable @Nullable Path cachePath) Initializes a new instance of the WPCacheManager class. If a local WordPress cache is found, it is loaded into memory, otherwise a new cache must be created using thefetchCache(Path, boolean, boolean)orfetchCacheFromInstancePath(boolean, boolean)if you already provided a path in the constructor.A local cache is not created automatically since the client must handle any exceptions that result from the cache creation process to ensure maximum control of client-specific flows, exception handling, and logging styles.
- Parameters:
fullyQualifiedDomainName- the fully qualified domain name of the WordPress siteusername- the username for the WordPress siteapplicationPassword- the application password for the WordPress sitecachePath- the path to the cache file
-
WPCacheManager
public WPCacheManager(@NotNull @NotNull String fullyQualifiedDomainName, @NotNull @NotNull String username, @NotNull @NotNull String applicationPassword) Initializes a new instance of the WPCacheManager class. If a local WordPress cache is not needed, the cache will be ignored and the cachePath parameter will be set to null.In case you want to create a cache in the current instance of the WPCacheManager, proceed to create the cache file using the
fetchCache(Path, boolean, boolean)method.- Parameters:
fullyQualifiedDomainName- the fully qualified domain name of the WordPress siteusername- the username for the WordPress siteapplicationPassword- the application password for the WordPress site
-
WPCacheManager
Initializes a new instance of the WPCacheManager class using the providedWPSiteInfoobject and an optional cache path. If a cache path is provided, the cache will be loaded from the file system. If a cache path is not provided, the cache will not be created and the cachePath parameter will be set to null.- Parameters:
siteInfo- the site information object containing the fully qualified domain name, username, and application password.cachePath- an optional path to the cache file.
-
WPCacheManager
Initializes a new instance of the WPCacheManager class using the providedWPSiteInfoobject. If a local WordPress cache is not needed, the cache will be ignored and the cachePath parameter will be set to null.In case you want to create a cache in the current instance of the WPCacheManager, proceed to create the cache file using the
fetchCache(Path, boolean, boolean)method.- Parameters:
siteInfo- the site information object containing the fully qualified domain name, username, and application password.
-
-
Method Details
-
connectWP
@NotNull public @NotNull Optional<HttpResponse<String>> connectWP(@NotNull @NotNull Map<QueryParamEnum, String> queryParams, @NotNull @NotNull WPRestPath pathParam) Connects to the WordPress REST API and returns the response. This a convenience method for this class, however, other methods that can carry out more functionality are available in theRestClientServiceclass.- Parameters:
queryParams- the query parameters to be used in the requestpathParam- the path parameter to be used in the request- Returns:
- an Optional containing the response from the WordPress REST API
-
fetchCacheFromInstancePath
public void fetchCacheFromInstancePath(boolean overwriteCache, boolean ignoreSSLHandshakeException) throws IOException Fetches the local cache file from the WordPress REST API.- Parameters:
overwriteCache- whether to overwrite the cache file if it existsignoreSSLHandshakeException- whether to ignore SSL Handshake Exception, useful for testing purposes only or local environments.- Throws:
IOException- if an I/O error occurs
-
fetchCache
public void fetchCache(@NotNull @NotNull Path cachePath, boolean overwriteCache, boolean ignoreSSLHandshakeException) throws IOException Fetches the local cache file from the WordPress REST API.- Parameters:
cachePath- the path to the cache fileoverwriteCache- whether to overwrite the cache file if it existsignoreSSLHandshakeException- whether to ignore SSL Handshake Exception, useful for testing purposes only or local environments.- Throws:
IOException- if an I/O error occurs
-
cacheSync
Synchronizes the cache file with the WordPress site. It assumes incremental cache changes will be taking place, and that is why this method relies heavily on sorting pipelines as the deltas are meant to be limited.- Parameters:
ignoreSSL- whether to ignore SSL errors, useful for testing purposes or local environments (do not use in production).- Returns:
- true if the cache was successfully synchronized, false if the cache is up-to-date.
- Throws:
InterruptedException- if the thread is interrupted- See Also:
-
cacheSync
Performs a cache synchronization between the local cache file and the WordPress REST API.- Returns:
trueif the cache was updated,falseotherwise- Throws:
InterruptedException- if the thread is interrupted while waiting to fetch new cache pages
-
apiBaseUrl
Returns the base URL of the WordPress REST API.- Returns:
- the base URL of the WordPress REST API
-
wpUser
Returns the user name for the WordPress site.- Returns:
- the user name for the WordPress site
-
getCacheAnalyzer
Returns a new WPCacheAnalyzer instance using the cache file specified at construction time.- Returns:
- a new WPCacheAnalyzer instance
-