java.lang.Object
com.broadleafcommerce.common.extension.cache.caffeine.CaffeineProxy
All Implemented Interfaces:
com.github.benmanes.caffeine.cache.Cache<Object,Object>

public class CaffeineProxy extends Object implements com.github.benmanes.caffeine.cache.Cache<Object,Object>
A decorating proxy for Caffeine Cache instances that transparently handles value serialization and deserialization using a configured Serializer.

This implementation provides three primary benefits:

  • Heap Optimization: By storing serialized byte arrays (typically via Apache Fury) instead of full object graphs, heap usage is significantly reduced (often 75%-90% smaller).
  • Immutability Enforcement: Deserializing on every get ensures that callers receive a fresh copy of the data. This prevents "cache poisoning" where a caller might unintentionally mutate a shared object, which would otherwise impact subsequent cache lookups.
  • Efficient/Accurate Cache Sizes: Since we are serializing to a byte array, we can achieve very accurate cache member sizing and do so with an inexpensive byte array count
Without serialization, deep cloning would be required to prevent cache poisoning, so there is a tax levied either way. However, using serialization gives the additional benefits of compaction and cache size accuracy.
Author:
Jeff Fischer
  • Constructor Details

    • CaffeineProxy

      public CaffeineProxy(com.github.benmanes.caffeine.cache.Cache<Object,Object> internalStore, Serializer serializer)
  • Method Details

    • getIfPresent

      public Object getIfPresent(Object key)
      Specified by:
      getIfPresent in interface com.github.benmanes.caffeine.cache.Cache<Object,Object>
    • put

      public void put(Object key, @NonNull Object value)
      Specified by:
      put in interface com.github.benmanes.caffeine.cache.Cache<Object,Object>
    • asMap

      @NonNull public ConcurrentMap<Object,Object> asMap()
      Specified by:
      asMap in interface com.github.benmanes.caffeine.cache.Cache<Object,Object>
    • get

      public Object get(Object key, @NonNull Function<? super Object,?> mappingFunction)
      Specified by:
      get in interface com.github.benmanes.caffeine.cache.Cache<Object,Object>
    • getAllPresent

      @NonNull public Map<Object,Object> getAllPresent(@NonNull Iterable<?> keys)
      Specified by:
      getAllPresent in interface com.github.benmanes.caffeine.cache.Cache<Object,Object>
    • getAll

      @NonNull public Map<Object,Object> getAll(@NonNull Iterable<?> keys, @NonNull Function<? super Set<?>,? extends Map<?,?>> mappingFunction)
      Specified by:
      getAll in interface com.github.benmanes.caffeine.cache.Cache<Object,Object>
    • putAll

      public void putAll(@NonNull Map<?,?> map)
      Specified by:
      putAll in interface com.github.benmanes.caffeine.cache.Cache<Object,Object>
    • invalidate

      public void invalidate(Object key)
      Specified by:
      invalidate in interface com.github.benmanes.caffeine.cache.Cache<Object,Object>
    • invalidateAll

      public void invalidateAll(@NonNull Iterable<?> keys)
      Specified by:
      invalidateAll in interface com.github.benmanes.caffeine.cache.Cache<Object,Object>
    • invalidateAll

      public void invalidateAll()
      Specified by:
      invalidateAll in interface com.github.benmanes.caffeine.cache.Cache<Object,Object>
    • estimatedSize

      public long estimatedSize()
      Specified by:
      estimatedSize in interface com.github.benmanes.caffeine.cache.Cache<Object,Object>
    • stats

      @NonNull public com.github.benmanes.caffeine.cache.stats.CacheStats stats()
      Specified by:
      stats in interface com.github.benmanes.caffeine.cache.Cache<Object,Object>
    • cleanUp

      public void cleanUp()
      Specified by:
      cleanUp in interface com.github.benmanes.caffeine.cache.Cache<Object,Object>
    • policy

      @NonNull public com.github.benmanes.caffeine.cache.Policy<Object,Object> policy()
      Specified by:
      policy in interface com.github.benmanes.caffeine.cache.Cache<Object,Object>
    • getManualPutCount

      public long getManualPutCount()