Search in sources :

Example 1 with Ehcache

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

the class AdminResource method invalidatesCacheByTenant.

@DELETE
@Path("/" + CACHE + "/" + TENANTS)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Invalidates Caches per tenant level")
@ApiResponses(value = {})
public Response invalidatesCacheByTenant(@QueryParam("tenantApiKey") final String tenantApiKey, @javax.ws.rs.core.Context final HttpServletRequest request) throws TenantApiException {
    // creating Tenant Context from Request
    TenantContext tenantContext = context.createContext(request);
    Tenant currentTenant = tenantApi.getTenantById(tenantContext.getTenantId());
    // getting Tenant Record Id
    Long tenantRecordId = recordIdApi.getRecordId(tenantContext.getTenantId(), ObjectType.TENANT, tenantContext);
    // clear tenant-record-id cache by tenantId
    final Ehcache tenantRecordIdCache = cacheManager.getEhcache(CacheType.TENANT_RECORD_ID.getCacheName());
    tenantRecordIdCache.remove(currentTenant.getId().toString());
    // clear tenant-payment-state-machine-config cache by tenantRecordId
    final Ehcache tenantPaymentStateMachineConfigCache = cacheManager.getEhcache(CacheType.TENANT_PAYMENT_STATE_MACHINE_CONFIG.getCacheName());
    removeCacheByKey(tenantPaymentStateMachineConfigCache, tenantRecordId.toString());
    // clear tenant cache by tenantApiKey
    final Ehcache tenantCache = cacheManager.getEhcache(CacheType.TENANT.getCacheName());
    tenantCache.remove(currentTenant.getApiKey());
    // clear tenant-kv cache by tenantRecordId
    final Ehcache tenantKvCache = cacheManager.getEhcache(CacheType.TENANT_KV.getCacheName());
    removeCacheByKey(tenantKvCache, tenantRecordId.toString());
    // clear tenant-config cache by tenantRecordId
    final Ehcache tenantConfigCache = cacheManager.getEhcache(CacheType.TENANT_CONFIG.getCacheName());
    tenantConfigCache.remove(tenantRecordId);
    // clear tenant-overdue-config cache by tenantRecordId
    final Ehcache tenantOverdueConfigCache = cacheManager.getEhcache(CacheType.TENANT_OVERDUE_CONFIG.getCacheName());
    tenantOverdueConfigCache.remove(tenantRecordId);
    // clear tenant-catalog cache by tenantRecordId
    final Ehcache tenantCatalogCache = cacheManager.getEhcache(CacheType.TENANT_CATALOG.getCacheName());
    tenantCatalogCache.remove(tenantRecordId);
    return Response.status(Status.OK).build();
}
Also used : Tenant(org.killbill.billing.tenant.api.Tenant) TenantContext(org.killbill.billing.util.callcontext.TenantContext) 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 2 with Ehcache

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

the class AdminResource method invalidatesCache.

@DELETE
@Path("/" + CACHE)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Invalidates the given Cache if specified, otherwise invalidates all caches")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Cache name does not exist or is not alive") })
public Response invalidatesCache(@QueryParam("cacheName") final String cacheName, @javax.ws.rs.core.Context final HttpServletRequest request) {
    if (null != cacheName && !cacheName.isEmpty()) {
        final Ehcache cache = cacheManager.getEhcache(cacheName);
        // check if cache is null
        if (cache == null) {
            log.warn("Cache for specified cacheName='{}' does not exist or is not alive", cacheName);
            return Response.status(Status.BAD_REQUEST).build();
        }
        // Clear given cache
        cache.removeAll();
    } else {
        // if not given a specific cacheName, clear all
        cacheManager.clearAll();
    }
    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 3 with Ehcache

use of net.sf.ehcache.Ehcache in project ORCID-Source by ORCID.

the class OrcidEhCacheFactoryBean method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    Ehcache existingCache = cacheManager.getEhcache(cacheName);
    String diskStorePath = cacheManager.getConfiguration().getDiskStoreConfiguration().getPath();
    LOGGER.debug("Cache manager disk store path = " + diskStorePath);
    if (existingCache == null) {
        CacheConfiguration config = createConfig();
        if (cacheEntryFactory != null) {
            this.cache = new SelfPopulatingCache(new Cache(config), cacheEntryFactory);
        } else {
            this.cache = new Cache(config);
        }
        cacheManager.addCache(this.cache);
    } else {
        this.cache = existingCache;
    }
}
Also used : SelfPopulatingCache(net.sf.ehcache.constructs.blocking.SelfPopulatingCache) Ehcache(net.sf.ehcache.Ehcache) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration) SelfPopulatingCache(net.sf.ehcache.constructs.blocking.SelfPopulatingCache) Cache(net.sf.ehcache.Cache)

Example 4 with Ehcache

use of net.sf.ehcache.Ehcache in project spring-framework by spring-projects.

the class EhCacheFactoryBean method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws CacheException {
    // If no cache name given, use bean name as cache name.
    String cacheName = getName();
    if (cacheName == null) {
        cacheName = this.beanName;
        setName(cacheName);
    }
    // If no CacheManager given, fetch the default.
    if (this.cacheManager == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Using default EhCache CacheManager for cache region '" + cacheName + "'");
        }
        this.cacheManager = CacheManager.getInstance();
    }
    synchronized (this.cacheManager) {
        // Fetch cache region: If none with the given name exists, create one on the fly.
        Ehcache rawCache;
        boolean cacheExists = this.cacheManager.cacheExists(cacheName);
        if (cacheExists) {
            if (logger.isDebugEnabled()) {
                logger.debug("Using existing EhCache cache region '" + cacheName + "'");
            }
            rawCache = this.cacheManager.getEhcache(cacheName);
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("Creating new EhCache cache region '" + cacheName + "'");
            }
            rawCache = createCache();
            rawCache.setBootstrapCacheLoader(this.bootstrapCacheLoader);
        }
        if (this.cacheEventListeners != null) {
            for (CacheEventListener listener : this.cacheEventListeners) {
                rawCache.getCacheEventNotificationService().registerListener(listener);
            }
        }
        // Needs to happen after listener registration but before setStatisticsEnabled
        if (!cacheExists) {
            this.cacheManager.addCache(rawCache);
        }
        if (this.disabled) {
            rawCache.setDisabled(true);
        }
        Ehcache decoratedCache = decorateCache(rawCache);
        if (decoratedCache != rawCache) {
            this.cacheManager.replaceCacheWithDecoratedCache(rawCache, decoratedCache);
        }
        this.cache = decoratedCache;
    }
}
Also used : CacheEventListener(net.sf.ehcache.event.CacheEventListener) Ehcache(net.sf.ehcache.Ehcache)

Example 5 with Ehcache

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

the class TestCache method testInvalidateCacheByName.

@Test(groups = "slow", description = "Can Invalidate (clear) a Cache by name")
public void testInvalidateCacheByName() throws Exception {
    // get Ehcache item with name "record-id"
    final Ehcache cache = cacheManager.getEhcache(CacheType.RECORD_ID.getCacheName());
    // verify that it is not null and has one stored key (the default tenant created for all integration tests)
    assertNotNull(cache);
    Assert.assertEquals(cache.getSize(), 1);
    // invalidate the specified cache
    killBillClient.invalidateCacheByName(cache.getName(), requestOptions);
    // verify that now the cache is empty and has no keys stored
    Assert.assertEquals(cache.getSize(), 0);
}
Also used : Ehcache(net.sf.ehcache.Ehcache) Test(org.testng.annotations.Test)

Aggregations

Ehcache (net.sf.ehcache.Ehcache)52 Test (org.junit.Test)15 CacheConfiguration (net.sf.ehcache.config.CacheConfiguration)14 Element (net.sf.ehcache.Element)13 ResourcesElementsProvider (org.jasig.resourceserver.utils.aggr.ResourcesElementsProvider)9 Cache (net.sf.ehcache.Cache)7 CacheManager (net.sf.ehcache.CacheManager)7 CacheException (net.sf.ehcache.CacheException)6 Configuration (net.sf.ehcache.config.Configuration)5 DiskStoreConfiguration (net.sf.ehcache.config.DiskStoreConfiguration)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 CacheKey (org.apereo.portal.utils.cache.CacheKey)4 FileSystemResource (org.springframework.core.io.FileSystemResource)4 Resource (org.springframework.core.io.Resource)4 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 FileReader (java.io.FileReader)3 IOException (java.io.IOException)3