Class WPCacheManager
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 Summary
ConstructorsConstructorDescriptionWPCacheManager(@NotNull URI wpURI, @NotNull String username, @NotNull String applicationPassword) Initializes a new instance of the WPCacheManager class.WPCacheManager(@NotNull URI wpURI, @NotNull String username, @NotNull String applicationPassword, @Nullable Path cachePath, @Nullable SSLContext sslContext) Initializes a new instance of the WPCacheManager class.WPCacheManager(@NotNull WPSiteInfo siteInfo, @Nullable Path cachePath, @Nullable SSLContext sslContext) Initializes a new instance of the WPCacheManager class using the providedWPSiteInfoobject and an optional cache path.WPCacheManager(@NotNull WPSiteInfo siteInfo, @Nullable SSLContext sslContext) Initializes a new instance of the WPCacheManager class using the providedWPSiteInfoobject. -
Method Summary
Modifier and TypeMethodDescriptionReturns the base URL of the WordPress REST API.booleanSynchronizes the local cache with the WordPress site.@Nullable 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) Fetches the cache to an explicit file path.voidFetches the cache using the instance'scachePath.Returns a new WPCacheAnalyzer instance using the cache file specified at construction time.voidoverrideDefaultPerPage(short defaultPerPage) Overrides the pagination size for REST requests.voidsetCachePath(Path cachePath) Sets the file system path for the JSON cache.wpUser()Returns the user name for the WordPress site.
-
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 usingfetchCache(Path),fetchCacheFromInstancePath(), or the first call tocacheSync()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 siteusername- the username for the WordPress siteapplicationPassword- the application password for the WordPress sitecachePath- path to the cache file, ornullto run without a local cachesslContext- SSL context for HTTPS requests, ornullto 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 siteusername- the username for the WordPress siteapplicationPassword- 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 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 passwordcachePath- an optional path to the cache file, ornullfor no local cachesslContext- SSL context for HTTPS requests, ornullto use the default
-
WPCacheManager
public WPCacheManager(@NotNull @NotNull WPSiteInfo siteInfo, @Nullable @Nullable SSLContext sslContext) 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)method.- Parameters:
siteInfo- the site information object containing the fully qualified domain name, username, and application passwordsslContext- SSL context for HTTPS requests, ornullto 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 inRestClientService.- Parameters:
queryParams- the query parameters to be used in the requestpathParam- the path parameter to be used in the request- Returns:
- the HTTP response, or
nullif the request could not be executed
-
fetchCacheFromInstancePath
public void fetchCacheFromInstancePath()Fetches the cache using the instance'scachePath.Alternatively, if no cache exists the first call to
cacheSync()will create it automatically.- Throws:
UnsupportedOperationException- if no cache path was provided at constructionCacheConstructionException- if remote metadata cannot be obtainedCacheFileSystemException- if the cache cannot be written to disk
-
fetchCache
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 obtainedCacheFileSystemException- 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 viafetchCacheFromInstancePath()and returnstrue.- Returns:
trueif the cache was created or updated,falseif it was already up-to-date- Throws:
CacheFileSystemException- ifcachePathisnullor not setCacheMetaDataException- if remote metadata cannot be refreshedCacheConstructionException- if the initial cache creation failsCacheFileSystemException- 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- ifdefaultPerPageis outside [10, 100]
-
setCachePath
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, ornullto clear
-
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
-