use of net.sf.ehcache.Ehcache in project camel by apache.
the class CacheValidate method isValid.
public boolean isValid(CacheManager cacheManager, String cacheName, String key) {
LOG.trace("Cache Name: {}", cacheName);
if (!cacheManager.cacheExists(cacheName)) {
LOG.debug("No existing Cache found with name: {}" + ". Please ensure a cache is first instantiated using a Cache Consumer or Cache Producer." + " Replacement will not be performed since the cache {} does not presently exist", cacheName, cacheName);
return false;
}
LOG.debug("Found an existing cache: {}", cacheName);
if (LOG.isTraceEnabled()) {
LOG.trace("Cache {} currently contains {} elements", cacheName, cacheManager.getCache(cacheName).getSize());
}
Ehcache cache = cacheManager.getCache(cacheName);
if (!cache.isKeyInCache(key)) {
LOG.debug("No Key with name: {} presently exists in the cache. It is also possible that the key may have expired in the cache." + " Replacement will not be performed until an appropriate key/value pair is added to (or) found in the cache.", key);
return false;
}
return true;
}
use of net.sf.ehcache.Ehcache in project camel by apache.
the class CacheEndpoint method initializeCache.
/**
* Returns {@link Cache} instance or create new one if not exists.
*
* @return {@link Cache}
*/
public Ehcache initializeCache() {
CacheManager cacheManager = getCacheManagerFactory().getInstance();
Ehcache cache;
if (cacheManager.cacheExists(config.getCacheName())) {
if (LOG.isTraceEnabled()) {
LOG.trace("Found an existing cache: {}", config.getCacheName());
LOG.trace("Cache {} currently contains {} elements", config.getCacheName(), cacheManager.getEhcache(config.getCacheName()).getSize());
}
cache = cacheManager.getEhcache(config.getCacheName());
} else {
cache = new Cache(config.getCacheName(), config.getMaxElementsInMemory(), config.getMemoryStoreEvictionPolicy(), config.isOverflowToDisk(), config.getDiskStorePath(), config.isEternal(), config.getTimeToLiveSeconds(), config.getTimeToIdleSeconds(), config.isDiskPersistent(), config.getDiskExpiryThreadIntervalSeconds(), null);
for (CacheEventListener listener : config.getEventListenerRegistry().getEventListeners()) {
cache.getCacheEventNotificationService().registerListener(listener);
}
for (CacheLoaderWrapper loader : config.getCacheLoaderRegistry().getCacheLoaders()) {
loader.init(cache);
cache.registerCacheLoader(loader);
}
cacheManager.addCache(cache);
if (LOG.isDebugEnabled()) {
LOG.debug("Added a new cache: " + cache.getName());
}
}
return cache;
}
use of net.sf.ehcache.Ehcache in project cas by apereo.
the class EhcacheTicketRegistryConfiguration method ticketRegistry.
@Autowired
@RefreshScope
@Bean
public TicketRegistry ticketRegistry(@Qualifier("ehcacheTicketCacheManager") final CacheManager manager, @Qualifier("ticketCatalog") final TicketCatalog ticketCatalog) {
final EncryptionRandomizedSigningJwtCryptographyProperties crypto = casProperties.getTicket().getRegistry().getEhcache().getCrypto();
final Collection<TicketDefinition> definitions = ticketCatalog.findAll();
definitions.forEach(t -> {
final Ehcache ehcache = buildCache(t);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Created Ehcache cache [{}] for [{}]", ehcache.getName(), t);
final CacheConfiguration config = ehcache.getCacheConfiguration();
LOGGER.debug("TicketCache.maxEntriesLocalHeap=[{}]", config.getMaxEntriesLocalHeap());
LOGGER.debug("TicketCache.maxEntriesLocalDisk=[{}]", config.getMaxEntriesLocalDisk());
LOGGER.debug("TicketCache.maxEntriesInCache=[{}]", config.getMaxEntriesInCache());
LOGGER.debug("TicketCache.persistenceConfiguration=[{}]", config.getPersistenceConfiguration().getStrategy());
LOGGER.debug("TicketCache.synchronousWrites=[{}]", config.getPersistenceConfiguration().getSynchronousWrites());
LOGGER.debug("TicketCache.timeToLive=[{}]", config.getTimeToLiveSeconds());
LOGGER.debug("TicketCache.timeToIdle=[{}]", config.getTimeToIdleSeconds());
LOGGER.debug("TicketCache.cacheManager=[{}]", ehcache.getCacheManager().getName());
}
manager.addDecoratedCacheIfAbsent(ehcache);
});
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("The following caches are available: [{}]", (Object[]) manager.getCacheNames());
}
return new EhCacheTicketRegistry(ticketCatalog, manager, CoreTicketUtils.newTicketRegistryCipherExecutor(crypto, "ehcache"));
}
use of net.sf.ehcache.Ehcache in project cas by apereo.
the class EhCacheTicketRegistry method addTicket.
@Override
public void addTicket(final Ticket ticketToAdd) {
final TicketDefinition metadata = this.ticketCatalog.find(ticketToAdd);
final Ticket ticket = encodeTicket(ticketToAdd);
final Element element = new Element(ticket.getId(), ticket);
int idleValue = ticketToAdd.getExpirationPolicy().getTimeToIdle().intValue();
if (idleValue <= 0) {
idleValue = ticketToAdd.getExpirationPolicy().getTimeToLive().intValue();
}
if (idleValue <= 0) {
idleValue = Integer.MAX_VALUE;
}
element.setTimeToIdle(idleValue);
int aliveValue = ticketToAdd.getExpirationPolicy().getTimeToLive().intValue();
if (aliveValue <= 0) {
aliveValue = Integer.MAX_VALUE;
}
element.setTimeToLive(aliveValue);
final Ehcache cache = getTicketCacheFor(metadata);
LOGGER.debug("Adding ticket [{}] to the cache [{}] to live [{}] seconds and stay idle for [{}] seconds", ticket.getId(), cache.getName(), aliveValue, idleValue);
cache.put(element);
}
use of net.sf.ehcache.Ehcache in project cas by apereo.
the class EhCacheTicketRegistry method getTicket.
@Override
public Ticket getTicket(final String ticketIdToGet) {
if (StringUtils.isBlank(ticketIdToGet)) {
return null;
}
final TicketDefinition metadata = this.ticketCatalog.find(ticketIdToGet);
if (metadata == null) {
LOGGER.warn("Ticket [{}] is not registered in the catalog and is unrecognized", ticketIdToGet);
return null;
}
final String ticketId = encodeTicketId(ticketIdToGet);
if (StringUtils.isBlank(ticketId)) {
return null;
}
final Ehcache ehcache = getTicketCacheFor(metadata);
final Element element = ehcache.get(ticketId);
if (element == null) {
LOGGER.debug("No ticket by id [{}] is found in the registry", ticketId);
return null;
}
final Ticket ticket = decodeTicket((Ticket) element.getObjectValue());
final CacheConfiguration config = new CacheConfiguration();
config.setTimeToIdleSeconds(ticket.getExpirationPolicy().getTimeToIdle());
config.setTimeToLiveSeconds(ticket.getExpirationPolicy().getTimeToLive());
if (element.isExpired(config) || ticket.isExpired()) {
ehcache.remove(element);
LOGGER.debug("Ticket [{}] has expired and is now evicted from the cache", ticket.getId());
return null;
}
return ticket;
}
Aggregations