Class L2Cache

  • All Implemented Interfaces:
    org.molgenis.data.transaction.TransactionListener

    @Service
    public class L2Cache
    extends java.lang.Object
    implements org.molgenis.data.transaction.TransactionListener
    In-memory cache of entities read from cacheable repositories.
    • Constructor Summary

      Constructors 
      Constructor Description
      L2Cache​(org.molgenis.data.transaction.TransactionManager transactionManager, EntityHydration entityHydration, org.molgenis.data.transaction.TransactionInformation transactionInformation, io.micrometer.core.instrument.MeterRegistry meterRegistry)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void afterCommitTransaction​(java.lang.String transactionId)  
      org.molgenis.data.Entity get​(org.molgenis.data.Repository<org.molgenis.data.Entity> repository, java.lang.Object id)
      Retrieves an entity from the cache or the underlying repository.
      org.molgenis.data.Entity get​(org.molgenis.data.Repository<org.molgenis.data.Entity> repository, java.lang.Object id, org.molgenis.data.Fetch fetch)
      Retrieves a partial entity from the cache or the underlying repository.
      java.util.List<org.molgenis.data.Entity> getBatch​(org.molgenis.data.Repository<org.molgenis.data.Entity> repository, java.lang.Iterable<java.lang.Object> ids)
      Retrieves a list of entities from the cache.
      java.util.List<org.molgenis.data.Entity> getBatch​(org.molgenis.data.Repository<org.molgenis.data.Entity> repository, java.lang.Iterable<java.lang.Object> ids, org.molgenis.data.Fetch fetch)
      Retrieves a list of partial entities from the cache.
      void logStatistics()
      Logs cumulative cache statistics for all known caches.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface org.molgenis.data.transaction.TransactionListener

        commitTransaction, doCleanupAfterCompletion, rollbackTransaction, transactionStarted
    • Constructor Detail

      • L2Cache

        public L2Cache​(org.molgenis.data.transaction.TransactionManager transactionManager,
                       EntityHydration entityHydration,
                       org.molgenis.data.transaction.TransactionInformation transactionInformation,
                       io.micrometer.core.instrument.MeterRegistry meterRegistry)
    • Method Detail

      • afterCommitTransaction

        public void afterCommitTransaction​(java.lang.String transactionId)
        Specified by:
        afterCommitTransaction in interface org.molgenis.data.transaction.TransactionListener
      • get

        public org.molgenis.data.Entity get​(org.molgenis.data.Repository<org.molgenis.data.Entity> repository,
                                            java.lang.Object id)
        Retrieves an entity from the cache or the underlying repository.
        Parameters:
        repository - the underlying repository
        id - the ID of the entity to retrieve
        Returns:
        the retrieved Entity, or null if the entity is not present.
        Throws:
        com.google.common.util.concurrent.UncheckedExecutionException - if the repository throws an error when loading the entity
      • get

        public org.molgenis.data.Entity get​(org.molgenis.data.Repository<org.molgenis.data.Entity> repository,
                                            java.lang.Object id,
                                            org.molgenis.data.Fetch fetch)
        Retrieves a partial entity from the cache or the underlying repository.
        Parameters:
        repository - the underlying repository
        id - the ID of the entity to retrieve
        fetch - containing attributes to retrieve, can be null
        Returns:
        the retrieved Entity, or null if the entity is not present.
        Throws:
        com.google.common.util.concurrent.UncheckedExecutionException - if the repository throws an error when loading the entity
      • getBatch

        public java.util.List<org.molgenis.data.Entity> getBatch​(org.molgenis.data.Repository<org.molgenis.data.Entity> repository,
                                                                 java.lang.Iterable<java.lang.Object> ids)
        Retrieves a list of entities from the cache. If the cache doesn't yet exist, will create the cache.
        Parameters:
        repository - the underlying repository, used to create the cache loader or to retrieve the existing cache
        ids - Iterable of the ids of the entities to retrieve
        Returns:
        List containing the retrieved entities, missing values are excluded
        Throws:
        java.lang.RuntimeException - if the cache failed to load the entities
      • getBatch

        public java.util.List<org.molgenis.data.Entity> getBatch​(org.molgenis.data.Repository<org.molgenis.data.Entity> repository,
                                                                 java.lang.Iterable<java.lang.Object> ids,
                                                                 org.molgenis.data.Fetch fetch)
        Retrieves a list of partial entities from the cache. If the cache doesn't yet exist, will create the cache.
        Parameters:
        repository - the underlying repository, used to create the cache loader or to retrieve the existing cache
        ids - Iterable of the ids of the entities to retrieve
        fetch - containing attributes to retrieve, can be null
        Returns:
        List containing the retrieved entities, missing values are excluded
        Throws:
        java.lang.RuntimeException - if the cache failed to load the entities
      • logStatistics

        @Scheduled(fixedRate=60000L)
        public void logStatistics()
        Logs cumulative cache statistics for all known caches.