Class WPCacheManager

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

public class WPCacheManager extends Object
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 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 the fetchCache(Path, boolean, boolean) or fetchCacheFromInstancePath(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 site
      username - the username for the WordPress site
      applicationPassword - the application password for the WordPress site
      cachePath - 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 site
      username - the username for the WordPress site
      applicationPassword - the application password for the WordPress site
    • WPCacheManager

      public WPCacheManager(@NotNull @NotNull WPSiteInfo siteInfo, Path cachePath)
      Initializes a new instance of the WPCacheManager class using the provided WPSiteInfo object 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

      public WPCacheManager(@NotNull @NotNull WPSiteInfo siteInfo)
      Initializes a new instance of the WPCacheManager class using the provided WPSiteInfo object. 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 the RestClientService class.
      Parameters:
      queryParams - the query parameters to be used in the request
      pathParam - 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 exists
      ignoreSSLHandshakeException - 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 file
      overwriteCache - whether to overwrite the cache file if it exists
      ignoreSSLHandshakeException - whether to ignore SSL Handshake Exception, useful for testing purposes only or local environments.
      Throws:
      IOException - if an I/O error occurs
    • cacheSync

      public boolean cacheSync(boolean ignoreSSL) throws InterruptedException
      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

      public boolean cacheSync() throws InterruptedException
      Performs a cache synchronization between the local cache file and the WordPress REST API.
      Returns:
      true if the cache was updated, false otherwise
      Throws:
      InterruptedException - if the thread is interrupted while waiting to fetch new cache pages
    • apiBaseUrl

      public String apiBaseUrl()
      Returns the base URL of the WordPress REST API.
      Returns:
      the base URL of the WordPress REST API
    • wpUser

      public String wpUser()
      Returns the user name for the WordPress site.
      Returns:
      the user name for the WordPress site
    • getCacheAnalyzer

      public WPCacheAnalyzer getCacheAnalyzer()
      Returns a new WPCacheAnalyzer instance using the cache file specified at construction time.
      Returns:
      a new WPCacheAnalyzer instance