Class WPCacheManager

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

public final class WPCacheManager extends Object
Main entry point for interacting with a WordPress REST API and managing a local JSON cache.

Typical lifecycle: construct with WPSiteInfo and an optional Path cache file, fetch full content via fetchCache(Path), fetchCacheFromInstancePath() or the first call to cacheSync(), keep it up to date with subsequent cacheSync() calls, and analyse it with getCacheAnalyzer().

If no cache exists, the first cacheSync() invocation creates it automatically. A local cache is otherwise not created automatically; callers needing explicit control should use fetchCache(Path).

Pagination is controlled by DEFAULT_PER_PAGE (WordPress allows 10–100, see overrideDefaultPerPage(short)). SSL is configured via the supplied SSLContext.

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

    • WPCacheManager

      public WPCacheManager(@NotNull @NotNull URI wpURI, @NotNull @NotNull String username, @NotNull @NotNull String applicationPassword, @Nullable @Nullable Path cachePath, @Nullable @Nullable SSLContext sslContext)
      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 fetchCache(Path), fetchCacheFromInstancePath(), or the first call to cacheSync() 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:
      wpURI - URI of the WordPress site
      username - the username for the WordPress site
      applicationPassword - the application password for the WordPress site
      cachePath - path to the cache file, or null to run without a local cache
      sslContext - SSL context for HTTPS requests, or null to use the default
    • WPCacheManager

      public WPCacheManager(@NotNull @NotNull URI wpURI, @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) method.

      Parameters:
      wpURI - URI 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, @Nullable @Nullable Path cachePath, @Nullable @Nullable SSLContext sslContext)
      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, or null for no local cache
      sslContext - SSL context for HTTPS requests, or null to use the default
    • WPCacheManager

      public WPCacheManager(@NotNull @NotNull WPSiteInfo siteInfo, @Nullable @Nullable SSLContext sslContext)
      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) method.

      Parameters:
      siteInfo - the site information object containing the fully qualified domain name, username, and application password
      sslContext - SSL context for HTTPS requests, or null to use the default
  • Method Details

    • connectWP

      @Nullable public @Nullable HttpResponse<String> connectWP(@NotNull @NotNull Map<QueryParamEnum,String> queryParams, @NotNull @NotNull WPRestPath pathParam)
      Connects to the WordPress REST API and returns the response. This is a convenience method for this class; more capable operations are available in RestClientService.
      Parameters:
      queryParams - the query parameters to be used in the request
      pathParam - the path parameter to be used in the request
      Returns:
      the HTTP response, or null if the request could not be executed
    • fetchCacheFromInstancePath

      public void fetchCacheFromInstancePath()
      Fetches the cache using the instance's cachePath.

      Alternatively, if no cache exists the first call to cacheSync() will create it automatically.

      Throws:
      UnsupportedOperationException - if no cache path was provided at construction
      CacheConstructionException - if remote metadata cannot be obtained
      CacheFileSystemException - if the cache cannot be written to disk
    • fetchCache

      public void fetchCache(@NotNull @NotNull Path cachePath)
      Fetches the cache to an explicit file path.

      Alternatively, if no cache exists the first call to cacheSync() will create it automatically when an instance path has been set.

      Parameters:
      cachePath - the destination path for the cache file
      Throws:
      CacheConstructionException - if remote metadata cannot be obtained
      CacheFileSystemException - if the cache cannot be written to disk
    • cacheSync

      public boolean cacheSync()
      Synchronizes the local cache with the WordPress site. Assumes incremental changes and uses sorting pipelines to compute deltas efficiently; not suited for large rewrites.

      If no cache file exists at cachePath, the first invocation creates it via fetchCacheFromInstancePath() and returns true.

      Returns:
      true if the cache was created or updated, false if it was already up-to-date
      Throws:
      CacheFileSystemException - if cachePath is null or not set
      CacheMetaDataException - if remote metadata cannot be refreshed
      CacheConstructionException - if the initial cache creation fails
      CacheFileSystemException - if the cache file cannot be written
    • overrideDefaultPerPage

      public void overrideDefaultPerPage(short defaultPerPage)
      Overrides the pagination size for REST requests.
      Parameters:
      defaultPerPage - number of posts per page, must be between 10 and 100 inclusive
      Throws:
      IllegalArgumentException - if defaultPerPage is outside [10, 100]
    • setCachePath

      public void setCachePath(Path cachePath)
      Sets the file system path for the JSON cache.

      If no cache exists at the new path, the next cacheSync() call will create it.

      Parameters:
      cachePath - the new cache file path, or null to clear
    • 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