Search in sources :

Example 26 with Ehcache

use of net.sf.ehcache.Ehcache in project hibernate-orm by hibernate.

the class AbstractEhcacheRegionFactory method getCache.

private Ehcache getCache(String name) throws CacheException {
    try {
        Ehcache cache = manager.getEhcache(name);
        if (cache == null) {
            LOG.unableToFindEhCacheConfiguration(name);
            manager.addCache(name);
            cache = manager.getEhcache(name);
            LOG.debug("started EHCache region: " + name);
        }
        return cache;
    } catch (net.sf.ehcache.CacheException e) {
        throw new CacheException(e);
    }
}
Also used : CacheException(org.hibernate.cache.CacheException) Ehcache(net.sf.ehcache.Ehcache)

Example 27 with Ehcache

use of net.sf.ehcache.Ehcache in project killbill by killbill.

the class TestCache method testInvalidateCacheByTenant.

@Test(groups = "slow", description = "Can Invalidate (clear) all Tenant Caches for current Tenant")
public void testInvalidateCacheByTenant() throws Exception {
    // creating a new Tenant for this test
    final String testApiKey = "testApiKey";
    final String testApiSecret = "testApiSecret";
    final Tenant tenant = new Tenant();
    tenant.setApiKey(testApiKey);
    tenant.setApiSecret(testApiSecret);
    loginTenant(testApiKey, testApiSecret);
    Tenant currentTenant = killBillClient.createTenant(tenant, false, requestOptions);
    // using custom RequestOptions with the new Tenant created before
    RequestOptions inputOptions = RequestOptions.builder().withCreatedBy(createdBy).withReason(reason).withComment(comment).withTenantApiKey(currentTenant.getApiKey()).withTenantApiSecret(currentTenant.getApiSecret()).build();
    // Uploading the test catalog using the new Tenant created before
    killBillClient.uploadXMLCatalog(Resources.getResource("SpyCarAdvanced.xml").getPath(), inputOptions);
    // creating an Account with PaymentMethod and a Subscription
    createAccountWithPMBundleAndSubscriptionAndWaitForFirstInvoiceWithInputOptions(inputOptions);
    // get all caches per tenant level
    final Ehcache tenantRecordIdCache = cacheManager.getEhcache(CacheType.TENANT_RECORD_ID.getCacheName());
    final Ehcache tenantPaymentStateMachineConfigCache = cacheManager.getEhcache(CacheType.TENANT_PAYMENT_STATE_MACHINE_CONFIG.getCacheName());
    final Ehcache tenantCache = cacheManager.getEhcache(CacheType.TENANT.getCacheName());
    final Ehcache tenantKvCache = cacheManager.getEhcache(CacheType.TENANT_KV.getCacheName());
    final Ehcache tenantConfigCache = cacheManager.getEhcache(CacheType.TENANT_CONFIG.getCacheName());
    final Ehcache tenantOverdueConfigCache = cacheManager.getEhcache(CacheType.TENANT_OVERDUE_CONFIG.getCacheName());
    final Ehcache tenantCatalogCache = cacheManager.getEhcache(CacheType.TENANT_CATALOG.getCacheName());
    // getting current Tenant's record Id from the specific Cache
    Long tenantRecordId = (Long) tenantRecordIdCache.get(currentTenant.getTenantId().toString()).getObjectValue();
    // verify that they are not null and have the expected tenant information
    assertNotNull(tenantRecordIdCache);
    assertNotNull(tenantRecordIdCache.get(currentTenant.getTenantId().toString()));
    assertNotNull(tenantPaymentStateMachineConfigCache);
    assertTrue(hasKeysByTenantRecordId(tenantPaymentStateMachineConfigCache, tenantRecordId.toString()));
    assertNotNull(tenantCache);
    assertNotNull(tenantCache.get(testApiKey));
    assertNotNull(tenantKvCache);
    assertTrue(hasKeysByTenantRecordId(tenantKvCache, tenantRecordId.toString()));
    assertNotNull(tenantConfigCache);
    assertNotNull(tenantConfigCache.get(tenantRecordId));
    assertNotNull(tenantOverdueConfigCache);
    assertNotNull(tenantOverdueConfigCache.get(tenantRecordId));
    assertNotNull(tenantCatalogCache);
    assertNotNull(tenantCatalogCache.get(tenantRecordId));
    // invalidate caches per tenant level
    killBillClient.invalidateCacheByTenant(inputOptions);
    // verify that now the caches don't have the previous values
    Assert.assertNull(tenantRecordIdCache.get(currentTenant.getTenantId().toString()));
    assertFalse(hasKeysByTenantRecordId(tenantPaymentStateMachineConfigCache, tenantRecordId.toString()));
    Assert.assertNull(tenantCache.get(testApiKey));
    assertFalse(hasKeysByTenantRecordId(tenantKvCache, tenantRecordId.toString()));
    Assert.assertNull(tenantConfigCache.get(tenantRecordId));
    Assert.assertNull(tenantOverdueConfigCache.get(tenantRecordId));
    Assert.assertNull(tenantCatalogCache.get(tenantRecordId));
}
Also used : Tenant(org.killbill.billing.client.model.Tenant) RequestOptions(org.killbill.billing.client.RequestOptions) Ehcache(net.sf.ehcache.Ehcache) Test(org.testng.annotations.Test)

Example 28 with Ehcache

use of net.sf.ehcache.Ehcache in project killbill by killbill.

the class ShiroEhCacheInstrumentor method instrument.

public void instrument(final String cacheName) {
    // Initialize the cache, if it doesn't exist yet
    // Note: Shiro's cache manager is not thread safe. Concurrent requests on startup
    // can throw org.apache.shiro.cache.CacheException: net.sf.ehcache.ObjectExistsException: Cache shiro-activeSessionCache already exists
    shiroEhCacheManager.getCache(cacheName);
    final Ehcache shiroEhcache = ehCacheCacheManager.getEhcache(cacheName);
    final Ehcache decoratedCache = InstrumentedEhcache.instrument(metricRegistry, shiroEhcache);
    try {
        ehCacheCacheManager.replaceCacheWithDecoratedCache(shiroEhcache, decoratedCache);
    } catch (final CacheException e) {
        logger.warn("Unable to instrument cache {}: {}", shiroEhcache.getName(), e.getMessage());
    }
}
Also used : CacheException(net.sf.ehcache.CacheException) InstrumentedEhcache(com.codahale.metrics.ehcache.InstrumentedEhcache) Ehcache(net.sf.ehcache.Ehcache)

Example 29 with Ehcache

use of net.sf.ehcache.Ehcache in project killbill by killbill.

the class AdminResource method invalidatesCacheByAccount.

@DELETE
@Path("/" + CACHE + "/" + ACCOUNTS + "/{accountId:" + UUID_PATTERN + "}/")
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Invalidates Caches per account level")
@ApiResponses(value = {})
public Response invalidatesCacheByAccount(@PathParam("accountId") final String accountId, @javax.ws.rs.core.Context final HttpServletRequest request) {
    // clear account-record-id cache by accountId
    final Ehcache accountRecordIdCache = cacheManager.getEhcache(CacheType.ACCOUNT_RECORD_ID.getCacheName());
    accountRecordIdCache.remove(accountId);
    // clear account-immutable cache by accountId
    final Ehcache accountImmutableCache = cacheManager.getEhcache(CacheType.ACCOUNT_IMMUTABLE.getCacheName());
    accountImmutableCache.remove(UUID.fromString(accountId));
    // clear account-bcd cache by accountId
    final Ehcache accountBcdCache = cacheManager.getEhcache(CacheType.ACCOUNT_BCD.getCacheName());
    accountBcdCache.remove(UUID.fromString(accountId));
    return Response.status(Status.OK).build();
}
Also used : Ehcache(net.sf.ehcache.Ehcache) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 30 with Ehcache

use of net.sf.ehcache.Ehcache in project camel by apache.

the class CacheBasedTokenReplacer method process.

public void process(Exchange exchange) throws Exception {
    String cacheKey = key.evaluate(exchange, String.class);
    if (isValid(cacheManager, cacheName, cacheKey)) {
        Ehcache cache = cacheManager.getCache(cacheName);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Replacing Token {} in Message with value stored against key {} in CacheName {}", new Object[] { replacementToken, cacheKey, cacheName });
        }
        exchange.getIn().setHeader(CacheConstants.CACHE_KEY, cacheKey);
        Object body = exchange.getIn().getBody();
        InputStream is = exchange.getContext().getTypeConverter().convertTo(InputStream.class, body);
        byte[] buffer;
        try {
            buffer = IOConverter.toBytes(is);
        } finally {
            IOHelper.close(is, "is", LOG);
        }
        // Note: The value in the cache must be a String
        String cacheValue = exchange.getContext().getTypeConverter().convertTo(String.class, cache.get(cacheKey).getObjectValue());
        String replacedTokenString = new String(buffer).replaceAll(replacementToken, cacheValue);
        LOG.trace("replacedTokenString = {}", replacedTokenString);
        exchange.getIn().setBody(replacedTokenString.getBytes());
    }
}
Also used : InputStream(java.io.InputStream) Ehcache(net.sf.ehcache.Ehcache)

Aggregations

Ehcache (net.sf.ehcache.Ehcache)39 Test (org.junit.Test)14 ResourcesElementsProvider (org.jasig.resourceserver.utils.aggr.ResourcesElementsProvider)9 Element (net.sf.ehcache.Element)8 CacheConfiguration (net.sf.ehcache.config.CacheConfiguration)7 CacheException (net.sf.ehcache.CacheException)5 CacheManager (net.sf.ehcache.CacheManager)5 CachedResource (org.apereo.portal.utils.cache.resource.CachedResource)5 CachingResourceLoaderImpl (org.apereo.portal.utils.cache.resource.CachingResourceLoaderImpl)5 LoadedResource (org.apereo.portal.utils.cache.resource.LoadedResource)5 FileSystemResource (org.springframework.core.io.FileSystemResource)5 Resource (org.springframework.core.io.Resource)5 CacheKey (org.apereo.portal.utils.cache.CacheKey)4 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)4 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)4 Test (org.testng.annotations.Test)4 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 FileReader (java.io.FileReader)3 InputStream (java.io.InputStream)3