Search in sources :

Example 31 with CacheManager

use of net.sf.ehcache.CacheManager in project onebusaway-application-modules by camsys.

the class TransitDataFederationBaseTestSupport method createCache.

public static Cache createCache() {
    CacheManager manager = new CacheManager(TransitDataFederationBaseTestSupport.class.getResourceAsStream("ehcache.xml"));
    manager.addCache("cache");
    return manager.getCache("cache");
}
Also used : CacheManager(net.sf.ehcache.CacheManager)

Example 32 with CacheManager

use of net.sf.ehcache.CacheManager in project gravitee-gateway by gravitee-io.

the class SubscriptionsCacheConfiguration method cache.

@Bean
public Cache cache() {
    CacheManager cacheManager = cacheManager();
    Cache apiKeyCache = cacheManager.getCache(CACHE_NAME);
    if (apiKeyCache == null) {
        LOGGER.warn("EHCache cache for " + CACHE_NAME + " not found. Fallback to default EHCache configuration");
        CacheConfiguration cacheConfiguration = new CacheConfiguration(CACHE_NAME, 1000);
        cacheManager.addCache(new Cache(cacheConfiguration));
    }
    return cacheManager.getCache(CACHE_NAME);
}
Also used : CacheManager(net.sf.ehcache.CacheManager) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration) Cache(net.sf.ehcache.Cache) Bean(org.springframework.context.annotation.Bean)

Example 33 with CacheManager

use of net.sf.ehcache.CacheManager in project ANNIS by korpling.

the class SaltBasedExporter method convertText.

@Override
public Exception convertText(String queryAnnisQL, int contextLeft, int contextRight, Set<String> corpora, List<String> keys, String argsAsString, boolean alignmc, WebResource annisResource, Writer out, EventBus eventBus, Map<String, CorpusConfig> corpusConfigs) {
    CacheManager cacheManager = CacheManager.create();
    try {
        Cache cache = cacheManager.getCache("saltProjectsCache");
        if (keys == null || keys.isEmpty()) {
            // auto set
            keys = new LinkedList<>();
            keys.add("tok");
            List<AnnisAttribute> attributes = new LinkedList<>();
            for (String corpus : corpora) {
                attributes.addAll(annisResource.path("corpora").path(urlPathEscape.escape(corpus)).path("annotations").queryParam("fetchvalues", "false").queryParam("onlymostfrequentvalues", "false").get(new AnnisAttributeListType()));
            }
            for (AnnisAttribute a : attributes) {
                if (a.getName() != null) {
                    String[] namespaceAndName = a.getName().split(":", 2);
                    if (namespaceAndName.length > 1) {
                        keys.add(namespaceAndName[1]);
                    } else {
                        keys.add(namespaceAndName[0]);
                    }
                }
            }
        }
        Map<String, String> args = new HashMap<>();
        for (String s : argsAsString.split("&|;")) {
            String[] splitted = s.split("=", 2);
            String key = splitted[0];
            String val = "";
            if (splitted.length > 1) {
                val = splitted[1];
            }
            args.put(key, val);
        }
        int stepSize = 10;
        int pCounter = 1;
        Map<Integer, Integer> offsets = new HashMap<Integer, Integer>();
        // 1. Get all the matches as Salt ID
        InputStream matchStream = annisResource.path("search/find/").queryParam("q", Helper.encodeJersey(queryAnnisQL)).queryParam("corpora", StringUtils.join(corpora, ",")).accept(MediaType.TEXT_PLAIN_TYPE).get(InputStream.class);
        // get node count for the query
        WebResource resource = Helper.getAnnisWebResource();
        List<QueryNode> nodes = resource.path("query/parse/nodes").queryParam("q", Helper.encodeJersey(queryAnnisQL)).get(new GenericType<List<QueryNode>>() {
        });
        Integer nodeCount = nodes.size();
        try (BufferedReader inReader = new BufferedReader(new InputStreamReader(matchStream, "UTF-8"))) {
            WebResource subgraphRes = annisResource.path("search/subgraph");
            MatchGroup currentMatches = new MatchGroup();
            String currentLine;
            int offset = 1;
            // 2. iterate over all matches and get the sub-graph for a group of matches
            while (!Thread.currentThread().isInterrupted() && (currentLine = inReader.readLine()) != null) {
                Match match = Match.parseFromString(currentLine);
                currentMatches.getMatches().add(match);
                if (currentMatches.getMatches().size() >= stepSize) {
                    WebResource res = subgraphRes.queryParam("left", "" + contextLeft).queryParam("right", "" + contextRight);
                    if (args.containsKey("segmentation")) {
                        res = res.queryParam("segmentation", args.get("segmentation"));
                    }
                    SubgraphFilter filter = getSubgraphFilter();
                    if (filter != null) {
                        res = res.queryParam("filter", filter.name());
                    }
                    Stopwatch stopwatch = Stopwatch.createStarted();
                    SaltProject p = res.post(SaltProject.class, currentMatches);
                    stopwatch.stop();
                    // export was fast enough
                    if (stopwatch.elapsed(TimeUnit.MILLISECONDS) < 500 && stepSize < 50) {
                        stepSize += 10;
                    }
                    convertSaltProject(p, keys, args, alignmc, offset - currentMatches.getMatches().size(), corpusConfigs, out, nodeCount);
                    offsets.put(pCounter, offset - currentMatches.getMatches().size());
                    cache.put(new Element(pCounter++, p));
                    currentMatches.getMatches().clear();
                    if (eventBus != null) {
                        eventBus.post(offset + 1);
                    }
                }
                offset++;
            }
            if (Thread.interrupted()) {
                return new InterruptedException("Exporter job was interrupted");
            }
            // query the left over matches
            if (!currentMatches.getMatches().isEmpty()) {
                WebResource res = subgraphRes.queryParam("left", "" + contextLeft).queryParam("right", "" + contextRight);
                if (args.containsKey("segmentation")) {
                    res = res.queryParam("segmentation", args.get("segmentation"));
                }
                SubgraphFilter filter = getSubgraphFilter();
                if (filter != null) {
                    res = res.queryParam("filter", filter.name());
                }
                SaltProject p = res.post(SaltProject.class, currentMatches);
                convertSaltProject(p, keys, args, alignmc, offset - currentMatches.getMatches().size() - 1, corpusConfigs, out, nodeCount);
                offsets.put(pCounter, offset - currentMatches.getMatches().size() - 1);
                cache.put(new Element(pCounter++, p));
            }
            offset = 1;
        }
        // build the list of ordered match numbers (ordering by occurrence in text)
        getOrderedMatchNumbers();
        @SuppressWarnings("unchecked") List<Integer> cacheKeys = cache.getKeys();
        List<Integer> listOfKeys = new ArrayList<Integer>();
        for (Integer key : cacheKeys) {
            listOfKeys.add(key);
        }
        Collections.sort(listOfKeys);
        for (Integer key : listOfKeys) {
            SaltProject p = (SaltProject) cache.get(key).getObjectValue();
            convertSaltProject(p, keys, args, alignmc, offsets.get(key), corpusConfigs, out, null);
        }
        out.append(System.lineSeparator());
        return null;
    } catch (AnnisQLSemanticsException | AnnisQLSyntaxException | AnnisCorpusAccessException | UniformInterfaceException | IOException | CacheException | IllegalStateException | ClassCastException ex) {
        return ex;
    } finally {
        cacheManager.removalAll();
        cacheManager.shutdown();
    }
}
Also used : HashMap(java.util.HashMap) CacheException(net.sf.ehcache.CacheException) AnnisAttribute(annis.service.objects.AnnisAttribute) Element(net.sf.ehcache.Element) Stopwatch(com.google.common.base.Stopwatch) ArrayList(java.util.ArrayList) WebResource(com.sun.jersey.api.client.WebResource) Match(annis.service.objects.Match) AnnisQLSyntaxException(annis.exceptions.AnnisQLSyntaxException) CacheManager(net.sf.ehcache.CacheManager) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) AnnisQLSemanticsException(annis.exceptions.AnnisQLSemanticsException) SaltProject(org.corpus_tools.salt.common.SaltProject) IOException(java.io.IOException) SubgraphFilter(annis.service.objects.SubgraphFilter) LinkedList(java.util.LinkedList) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) AnnisCorpusAccessException(annis.exceptions.AnnisCorpusAccessException) QueryNode(annis.model.QueryNode) MatchGroup(annis.service.objects.MatchGroup) BufferedReader(java.io.BufferedReader) Cache(net.sf.ehcache.Cache)

Example 34 with CacheManager

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

the class PessoaRemoveInterceptor method invoke.

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    Object result = null;
    CacheManager cacheManager = CacheManager.create(new ClassPathResource("echache.xml").getInputStream());
    Cache uniqueCache = cacheManager.getCache("pessoasCache");
    Cache collectionsCache = cacheManager.getCache("colecoesCache");
    Long pessoaId = (Long) invocation.getArguments()[0];
    Element uniqueElement = uniqueCache.get(pessoaId);
    if (uniqueElement != null) {
        uniqueCache.remove(pessoaId);
    }
    Element collectionElement = collectionsCache.get("pessoas");
    if (collectionElement != null) {
        collectionsCache.remove("pessoas");
    }
    result = invocation.proceed();
    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)

Example 35 with CacheManager

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

the class TurmaFindInterceptor method invoke.

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    Object result = null;
    CacheManager cacheManager = CacheManager.create(new ClassPathResource("echache.xml").getInputStream());
    Cache cache = cacheManager.getCache("turmasCache");
    Long turmaId = (Long) invocation.getArguments()[0];
    Element element = cache.get(turmaId);
    if (element == null) {
        System.out.println("com banco");
        result = invocation.proceed();
        cache.put(new Element(turmaId, result));
    } else {
        System.out.println("sem banco");
        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)

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