Class JCacheContentCachingService

java.lang.Object
net.solarnetwork.central.web.support.JCacheContentCachingService
All Implemented Interfaces:
EventListener, javax.cache.event.CacheEntryCreatedListener<String,CachedContent>, javax.cache.event.CacheEntryExpiredListener<String,CachedContent>, javax.cache.event.CacheEntryListener<String,CachedContent>, javax.cache.event.CacheEntryRemovedListener<String,CachedContent>, javax.cache.event.CacheEntryUpdatedListener<String,CachedContent>, CacheUtils.CacheEvictionListener<String,CachedContent>, ContentCachingService, net.solarnetwork.service.PingTest

public class JCacheContentCachingService extends Object implements ContentCachingService, net.solarnetwork.service.PingTest, javax.cache.event.CacheEntryCreatedListener<String,CachedContent>, javax.cache.event.CacheEntryExpiredListener<String,CachedContent>, javax.cache.event.CacheEntryUpdatedListener<String,CachedContent>, javax.cache.event.CacheEntryRemovedListener<String,CachedContent>, CacheUtils.CacheEvictionListener<String,CachedContent>
Caching service backed by a Cache.
  • Field Details

    • DEFAULT_STAT_LOG_ACCESS_COUNT

      public static final int DEFAULT_STAT_LOG_ACCESS_COUNT
      The default value for the statLogAccessCount property.
      See Also:
  • Constructor Details

    • JCacheContentCachingService

      public JCacheContentCachingService(javax.cache.Cache<String,CachedContent> cache)
      Constructor.
      Parameters:
      cache - the cache to use
      Throws:
      IllegalArgumentException - if any argument is null
  • Method Details

    • getPingTestId

      public String getPingTestId()
      Specified by:
      getPingTestId in interface net.solarnetwork.service.PingTest
    • getPingTestName

      public String getPingTestName()
      Specified by:
      getPingTestName in interface net.solarnetwork.service.PingTest
    • getPingTestMaximumExecutionMilliseconds

      public long getPingTestMaximumExecutionMilliseconds()
      Specified by:
      getPingTestMaximumExecutionMilliseconds in interface net.solarnetwork.service.PingTest
    • performPingTest

      public net.solarnetwork.service.PingTest.Result performPingTest() throws Exception
      Specified by:
      performPingTest in interface net.solarnetwork.service.PingTest
      Throws:
      Exception
    • onExpired

      public void onExpired(Iterable<javax.cache.event.CacheEntryEvent<? extends String,? extends CachedContent>> events) throws javax.cache.event.CacheEntryListenerException
      Specified by:
      onExpired in interface javax.cache.event.CacheEntryExpiredListener<String,CachedContent>
      Throws:
      javax.cache.event.CacheEntryListenerException
    • onCreated

      public void onCreated(Iterable<javax.cache.event.CacheEntryEvent<? extends String,? extends CachedContent>> events) throws javax.cache.event.CacheEntryListenerException
      Specified by:
      onCreated in interface javax.cache.event.CacheEntryCreatedListener<String,CachedContent>
      Throws:
      javax.cache.event.CacheEntryListenerException
    • onUpdated

      public void onUpdated(Iterable<javax.cache.event.CacheEntryEvent<? extends String,? extends CachedContent>> events) throws javax.cache.event.CacheEntryListenerException
      Specified by:
      onUpdated in interface javax.cache.event.CacheEntryUpdatedListener<String,CachedContent>
      Throws:
      javax.cache.event.CacheEntryListenerException
    • onRemoved

      public void onRemoved(Iterable<javax.cache.event.CacheEntryEvent<? extends String,? extends CachedContent>> events) throws javax.cache.event.CacheEntryListenerException
      Specified by:
      onRemoved in interface javax.cache.event.CacheEntryRemovedListener<String,CachedContent>
      Throws:
      javax.cache.event.CacheEntryListenerException
    • onCacheEviction

      public void onCacheEviction(String key, CachedContent value)
      Description copied from interface: CacheUtils.CacheEvictionListener
      Receive a cache eviction notification.
      Specified by:
      onCacheEviction in interface CacheUtils.CacheEvictionListener<String,CachedContent>
      Parameters:
      key - the evicted key
      value - the evicted value
    • getAccept

      public List<org.springframework.http.MediaType> getAccept(jakarta.servlet.http.HttpServletRequest request)
    • keyForRequest

      public String keyForRequest(jakarta.servlet.http.HttpServletRequest request)
      Get a cache key for a given request.

      This implementation uses the following components to generate the cache key:

      1. SolarNetwork authorization user, from the Authorization HTTP header)
      2. request method (via HttpServletRequest.getMethod())
      3. request URI (via HttpServletRequest.getRequestURI())
      4. request query parameters
      5. Accept header value
      Specified by:
      keyForRequest in interface ContentCachingService
      Parameters:
      request - the HTTP request to derive a key from
      Returns:
      the key, or null if the request should not be cached
    • sendCachedResponse

      public CachedContent sendCachedResponse(String key, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException
      Description copied from interface: ContentCachingService
      Send a cached response for a given cache key, if possible.
      Specified by:
      sendCachedResponse in interface ContentCachingService
      Parameters:
      key - the cache key, returned previously from ContentCachingService.keyForRequest(HttpServletRequest)
      request - the active HTTP request
      response - the active HTTP response to send the cached data to
      Returns:
      the cached content if the response was successfully handled, null otherwise (for example a cache miss)
      Throws:
      IOException - if any IO error occurs
    • cacheResponse

      public void cacheResponse(String key, jakarta.servlet.http.HttpServletRequest request, int statusCode, org.springframework.http.HttpHeaders headers, InputStream content, ContentCachingService.CompressionType compressionType) throws IOException
      Description copied from interface: ContentCachingService
      Cache a response after completing an intercepted response.
      Specified by:
      cacheResponse in interface ContentCachingService
      Parameters:
      key - the cache key, returned previously from ContentCachingService.keyForRequest(HttpServletRequest)
      request - the active HTTP request
      statusCode - the resolved HTTP response status code
      headers - the resolved HTTP response headers
      content - the resolved HTTP response content, or null if none
      compressionType - if content has been compressed then the compression type, otherwise null
      Throws:
      IOException - if any IO error occurs
    • getCacheContentMetadata

      protected Map<String,?> getCacheContentMetadata(String key, jakarta.servlet.http.HttpServletRequest request, int statusCode, org.springframework.http.HttpHeaders headers)
      Get metadata for the cache content.

      This method returns null, so extending classes can override. Note that the returned object must implement Serializable, along with all values in the map.

      Parameters:
      key - the cache key
      request - the active request
      statusCode - the HTTP status code
      headers - the HTTP headers
      Returns:
      the metadata, or null if none
    • setCompressibleMediaTypes

      public void setCompressibleMediaTypes(Set<org.springframework.http.MediaType> compressibleMediaTypes)
      Configure a set of compressible media types.
      Parameters:
      compressibleMediaTypes - compressible media types
    • setCompressMinimumLength

      public void setCompressMinimumLength(int compressMinimumLength)
      A minimum size content must be to qualify for storing compressed.
      Parameters:
      compressMinimumLength - the minimum length, in bytes
    • setStatLogAccessCount

      public void setStatLogAccessCount(int statLogAccessCount)
      Set the statistic log update count.

      Setting this to something greater than 0 will cause INFO level statistic log entries to be emitted every statLogAccessCount times a cachable request has been processed.

      Parameters:
      statLogAccessCount - the access count the access count; defaults to DEFAULT_STAT_LOG_ACCESS_COUNT