Search in sources :

Example 81 with CacheManager

use of net.sf.ehcache.CacheManager in project trainning by fernandotomasio.

the class OrganizationalCacheAdvice method findOrganizacaoMethodInterceptor.

@Around("findOrganizacao()")
public Object findOrganizacaoMethodInterceptor(ProceedingJoinPoint joinPoint) throws Throwable {
    Object result = null;
    CacheManager cacheManager = CacheManager.create(new ClassPathResource("echache.xml").getInputStream());
    Cache cache = cacheManager.getCache("organizacoes-cache");
    Object[] args = joinPoint.getArgs();
    Long organizacaoId = (Long) args[0];
    Element element = cache.get(organizacaoId);
    if (element == null) {
        result = joinPoint.proceed();
        cache.put(new Element(organizacaoId, result));
    } else {
        Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Dados presentes no cache de organizações, não foi necessário acesso ao banco de dados");
        result = element.getValue();
    }
    return result;
}
Also used : Element(net.sf.ehcache.Element) CacheManager(net.sf.ehcache.CacheManager) ClassPathResource(org.springframework.core.io.ClassPathResource) Cache(net.sf.ehcache.Cache) Around(org.aspectj.lang.annotation.Around)

Example 82 with CacheManager

use of net.sf.ehcache.CacheManager in project trainning by fernandotomasio.

the class TesteEcache method main.

public static void main(String[] args) throws IOException, DAOException {
    CacheManager cacheManager = CacheManager.create(new ClassPathResource("echache.xml").getInputStream());
    TreinamentoSolicitadoDAO dao = new HibernateTreinamentoSolicitadoDAO();
    List<TreinamentoSolicitadoDTO> list = dao.findAll(null, null, null, null);
    System.out.println(list.size());
    Cache cache = cacheManager.getCache("colecoesCache");
    cache.put(new Element("treinamentosSolicitados", list));
    Element e = cache.get("treinamentosSolicitados");
    @SuppressWarnings("unchecked") List<TreinamentoSolicitadoDTO> recuperados = (List<TreinamentoSolicitadoDTO>) e.getValue();
    for (TreinamentoSolicitadoDTO treinamentoSolicitadoDTO : recuperados) {
        System.out.println(treinamentoSolicitadoDTO.getId());
    }
}
Also used : Element(net.sf.ehcache.Element) CacheManager(net.sf.ehcache.CacheManager) HibernateTreinamentoSolicitadoDAO(com.tomasio.projects.trainning.dao.HibernateTreinamentoSolicitadoDAO) List(java.util.List) HibernateTreinamentoSolicitadoDAO(com.tomasio.projects.trainning.dao.HibernateTreinamentoSolicitadoDAO) TreinamentoSolicitadoDAO(com.tomasio.projects.trainning.dao.TreinamentoSolicitadoDAO) ClassPathResource(org.springframework.core.io.ClassPathResource) TreinamentoSolicitadoDTO(com.tomasio.projects.trainning.dto.TreinamentoSolicitadoDTO) Cache(net.sf.ehcache.Cache)

Example 83 with CacheManager

use of net.sf.ehcache.CacheManager in project Gemma by PavlidisLab.

the class CacheKey method afterPropertiesSet.

@Override
public void afterPropertiesSet() {
    CacheManager cacheManager = cacheManagerFactory.getObject();
    int maxElements = Settings.getInt("gemma.cache.diffex.maxelements", DifferentialExpressionResultCacheImpl.CACHE_DEFAULT_MAX_ELEMENTS);
    int timeToLive = Settings.getInt("gemma.cache.diffex.timetolive", DifferentialExpressionResultCacheImpl.CACHE_DEFAULT_TIME_TO_LIVE);
    int timeToIdle = Settings.getInt("gemma.cache.diffex.timetoidle", DifferentialExpressionResultCacheImpl.CACHE_DEFAULT_TIME_TO_IDLE);
    boolean eternal = Settings.getBoolean("gemma.cache.diffex.eternal", DifferentialExpressionResultCacheImpl.CACHE_DEFAULT_ETERNAL) && timeToLive == 0;
    boolean terracottaEnabled = Settings.getBoolean("gemma.cache.clustered", true);
    boolean overFlowToDisk = Settings.getBoolean("gemma.cache.diffex.usedisk", DifferentialExpressionResultCacheImpl.CACHE_DEFAULT_OVERFLOW_TO_DISK);
    boolean diskPersistent = Settings.getBoolean("gemma.cache.diskpersistent", false) && !terracottaEnabled;
    if (!cacheManager.cacheExists(DifferentialExpressionResultCacheImpl.CACHE_NAME_BASE)) {
        /*
             * See TerracottaConfiguration.
             */
        int diskExpiryThreadIntervalSeconds = 600;
        int maxElementsOnDisk = 10000;
        boolean terracottaCoherentReads = false;
        boolean clearOnFlush = false;
        if (terracottaEnabled) {
            CacheConfiguration config = new CacheConfiguration(DifferentialExpressionResultCacheImpl.CACHE_NAME_BASE, maxElements);
            config.setStatistics(false);
            config.setMemoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU.toString());
            config.addPersistence(new PersistenceConfiguration().strategy(Strategy.NONE));
            config.setEternal(eternal);
            config.setTimeToIdleSeconds(timeToIdle);
            config.setMaxElementsOnDisk(maxElementsOnDisk);
            config.addTerracotta(new TerracottaConfiguration());
            // noinspection ConstantConditions // Better readability
            config.getTerracottaConfiguration().setCoherentReads(terracottaCoherentReads);
            // noinspection ConstantConditions // Better readability
            config.clearOnFlush(clearOnFlush);
            config.setTimeToLiveSeconds(timeToLive);
            config.getTerracottaConfiguration().setClustered(true);
            config.getTerracottaConfiguration().setValueMode("SERIALIZATION");
            NonstopConfiguration nonstopConfiguration = new NonstopConfiguration();
            TimeoutBehaviorConfiguration tobc = new TimeoutBehaviorConfiguration();
            tobc.setType(TimeoutBehaviorType.NOOP.getTypeName());
            nonstopConfiguration.addTimeoutBehavior(tobc);
            config.getTerracottaConfiguration().addNonstop(nonstopConfiguration);
            this.cache = new Cache(config);
            this.topHitsCache = new Cache(config);
            this.topHitsCache.setName(DifferentialExpressionResultCacheImpl.TOP_HIT_CACHE_NAME_BASE);
        } else {
            this.cache = new Cache(DifferentialExpressionResultCacheImpl.CACHE_NAME_BASE, maxElements, MemoryStoreEvictionPolicy.LRU, overFlowToDisk, null, eternal, timeToLive, timeToIdle, diskPersistent, diskExpiryThreadIntervalSeconds, null);
            this.topHitsCache = new Cache(DifferentialExpressionResultCacheImpl.TOP_HIT_CACHE_NAME_BASE, maxElements, MemoryStoreEvictionPolicy.LRU, overFlowToDisk, null, eternal, timeToLive, timeToIdle, diskPersistent, diskExpiryThreadIntervalSeconds, null);
        }
        cacheManager.addCache(cache);
        cacheManager.addCache(topHitsCache);
    }
}
Also used : CacheManager(net.sf.ehcache.CacheManager) Cache(net.sf.ehcache.Cache)

Example 84 with CacheManager

use of net.sf.ehcache.CacheManager in project Gemma by PavlidisLab.

the class CoexpressionCacheImpl method afterPropertiesSet.

/**
 * Initialize the cache; if it already exists it will not be recreated.
 */
@Override
public void afterPropertiesSet() {
    CacheManager cacheManager = cacheManagerFactory.getObject();
    assert cacheManager != null;
    int maxElements = Settings.getInt("gemma.cache.gene2gene.maxelements", CoexpressionCacheImpl.GENE_COEXPRESSION_CACHE_DEFAULT_MAX_ELEMENTS);
    int timeToLive = Settings.getInt("gemma.cache.gene2gene.timetolive", CoexpressionCacheImpl.GENE_COEXPRESSION_CACHE_DEFAULT_TIME_TO_LIVE);
    int timeToIdle = Settings.getInt("gemma.cache.gene2gene.timetoidle", CoexpressionCacheImpl.GENE_COEXPRESSION_CACHE_DEFAULT_TIME_TO_IDLE);
    boolean overFlowToDisk = Settings.getBoolean("gemma.cache.gene2gene.usedisk", CoexpressionCacheImpl.GENE_COEXPRESSION_CACHE_DEFAULT_OVERFLOW_TO_DISK);
    boolean eternal = Settings.getBoolean("gemma.cache.gene2gene.eternal", CoexpressionCacheImpl.GENE_COEXPRESSION_CACHE_DEFAULT_ETERNAL) && timeToLive == 0;
    boolean terracottaEnabled = Settings.getBoolean("gemma.cache.clustered", false);
    boolean diskPersistent = Settings.getBoolean("gemma.cache.diskpersistent", false) && !terracottaEnabled;
    this.cache = CacheUtils.createOrLoadCache(cacheManager, CoexpressionCacheImpl.GENE_COEXPRESSION_CACHE_NAME, terracottaEnabled, maxElements, overFlowToDisk, eternal, timeToIdle, timeToLive, diskPersistent);
}
Also used : CacheManager(net.sf.ehcache.CacheManager)

Example 85 with CacheManager

use of net.sf.ehcache.CacheManager in project openmrs-core by openmrs.

the class OpenmrsCacheManagerFactoryBean method getObject.

@Override
public CacheManager getObject() {
    CacheManager cacheManager = super.getObject();
    Map<String, CacheConfiguration> cacheConfig = cacheManager.getConfiguration().getCacheConfigurations();
    List<CacheConfiguration> cacheConfigurations = CachePropertiesUtil.getCacheConfigurations();
    cacheConfigurations.stream().filter(cc -> cacheConfig.get(cc.getName()) == null).forEach(cc -> cacheManager.addCache(new Cache(cc)));
    return cacheManager;
}
Also used : List(java.util.List) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration) EhCacheManagerFactoryBean(org.springframework.cache.ehcache.EhCacheManagerFactoryBean) Map(java.util.Map) CacheManager(net.sf.ehcache.CacheManager) Cache(net.sf.ehcache.Cache) CacheManager(net.sf.ehcache.CacheManager) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration) Cache(net.sf.ehcache.Cache)

Aggregations

CacheManager (net.sf.ehcache.CacheManager)102 Cache (net.sf.ehcache.Cache)55 ClassPathResource (org.springframework.core.io.ClassPathResource)21 Element (net.sf.ehcache.Element)20 Configuration (net.sf.ehcache.config.Configuration)18 Test (org.junit.Test)18 CacheConfiguration (net.sf.ehcache.config.CacheConfiguration)17 MarkupCache (org.apache.wicket.markup.MarkupCache)10 CacheException (net.sf.ehcache.CacheException)9 IOException (java.io.IOException)7 Ehcache (net.sf.ehcache.Ehcache)7 UpdatingSelfPopulatingCache (net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache)6 URL (java.net.URL)5 BlockingCache (net.sf.ehcache.constructs.blocking.BlockingCache)5 SelfPopulatingCache (net.sf.ehcache.constructs.blocking.SelfPopulatingCache)5 DiskStoreConfiguration (net.sf.ehcache.config.DiskStoreConfiguration)4 PersistenceConfiguration (net.sf.ehcache.config.PersistenceConfiguration)4 Around (org.aspectj.lang.annotation.Around)4 Before (org.junit.Before)4 File (java.io.File)3