Interface ResourceCacheResolvable

All Known Subinterfaces:
ResourceResolver
All Known Implementing Classes:
AbstractResourceCacheResolvable, AbstractResourceResolver

public interface ResourceCacheResolvable
An abstraction responsible for the following:
  • to return a ResourceDataCache as a cache store of resource representations,
  • to be able to check whether or not a specific Resource representation is cacheable,
  • to convert a Resource representation to a cacheable data object to be stored in ResourceDataCache,
  • and to convert a cached data object back to a Resource representation.

Note that a Resource object cannot necessarily be cached into ResourceDataCache directly in every case. For example, if the underlying ResourceDataCache depends on object serialization and if the given Resource instance is not practically serializable to be restored back to the original state, then it could be meaningless to try to cache the Resource instance directly into the cache store. Therefore, an implementation may choose to implement methods in this interface to convert a Resource representation to a practically cacheable data object and convert the cached data object back to the Resource representation.

For example, a JSON data based ResourceCacheResolvable implementation may have non-serializable JSON objects when resolving data from the backend. In that case, the implementation may choose to convert the JSON object to a string value to be cached. And, when restoring the Resource representation from a cached data (in this case, a string for the JSON object), the implementation may create a JSON object back to restore its original state. Of course, if the Resource implementation is practically cacheable, then those conversion methods may return the Resource object directly without having to implement any conversion logic. The same pattern can apply to other use cases. e.g, non-Serializable record object, etc.

  • Method Details

    • isCacheEnabled

      boolean isCacheEnabled()
      Returns true if caching is enabled with this.
      Returns:
      true if caching is enabled with this
    • isCacheable

      boolean isCacheable(Resource resource)
      Returns true if the given resource is cacheable.

      Note that if an implementation does not want to cache Resource representations at all for any reason, then it may always return false. ResourceServiceBroker implementation should not try to cache any Resource representations into cache store if this method returns false.

      Parameters:
      resource - resource representation
      Returns:
      true if the given resource is cacheable
    • createCacheKey

      ValueMap createCacheKey(String resourceSpace, String operationKey, String absPath, Map<String,Object> pathVariables, ExchangeHint exchangeHint)
      Create cache key based on given operation key, resource path, path variables and exchange hint. Or return null if no cache should be available.
      Parameters:
      resourceSpace - resource space name
      operationKey - a unique operation name given by a ResourceServiceBroker.
      absPath - absolute path of a Resource
      pathVariables - the variables to expand the template given by absPath
      exchangeHint - a message exchange hint for the backend
      Returns:
      cache key based on given operation key, resource path, path variables and exchange hint, or null if no cache should be available
    • toCacheData

      Object toCacheData(Resource resource) throws IOException
      Convert the given resource to a cacheable data object to be stored in ResourceDataCache.

      Implementations may simply return the resource object directly without any conversion if the the resource object can be stored (for example, the resource object is serializable) into the underlying cache store. Otherwise, the resource object can be converted to something else and returned in order to be stored into the underlying cache store by implementations.

      Parameters:
      resource - resource representation
      Returns:
      converted the given resource to a cacheable data object to be stored in ResourceDataCache
      Throws:
      IOException - if IO error occurs
    • fromCacheData

      Resource fromCacheData(Object cacheData) throws IOException
      Convert and restore back the given cacheData from the ResourceDataCache to a Resource object.

      Implementations may simply cast the cacheData object directly to Resource object without any conversion if the the resource object was stored directly into the underlying cache store. Otherwise, the cacheData object can be converted back to a Resource object by implementations.

      Parameters:
      cacheData - cached data object that is stored in ResourceDataCache
      Returns:
      Resource object converted from the cacheData
      Throws:
      IOException - if IO error occurs
    • getResourceDataCache

      ResourceDataCache getResourceDataCache()
      Returns a ResourceDataCache that represents the underlying cache store.

      Note that an implementation may return null if it doesn't want to have its own cache store representation. In that case, a ResourceServiceBroker implementation may use a default ResourceDataCache instance as a fallback cache store.

      Returns:
      a ResourceDataCache that represents the underlying cache store