001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.commons.jcs3.jcache; 020 021import java.util.Collections; 022import java.util.Map; 023 024import javax.cache.Cache; 025import javax.cache.configuration.CacheEntryListenerConfiguration; 026import javax.cache.event.EventType; 027 028import org.apache.commons.jcs3.engine.behavior.ICacheElement; 029import org.apache.commons.jcs3.engine.behavior.ICompositeCacheAttributes; 030import org.apache.commons.jcs3.engine.behavior.IElementAttributes; 031import org.apache.commons.jcs3.engine.control.CompositeCache; 032 033// allows us to plug some lifecycle callbacks on the core cache without impacting too much the core 034public class ExpiryAwareCache<A, B> extends CompositeCache<A, B> 035{ 036 private Map<CacheEntryListenerConfiguration<A, B>, JCSListener<A, B>> listeners; 037 private Cache<A, B> cacheRef; 038 039 ExpiryAwareCache(final ICompositeCacheAttributes cattr, final IElementAttributes attr) 040 { 041 super(cattr, attr); 042 } 043 044 @Override 045 protected void doExpires(final ICacheElement<A, B> element) 046 { 047 super.doExpires(element); 048 for (final JCSListener<A, B> listener : listeners.values()) 049 { 050 listener.onExpired(Collections.singletonList(new JCSCacheEntryEvent<>( 051 cacheRef, EventType.REMOVED, null, element.getKey(), element.getVal()))); 052 } 053 } 054 055 void init(final Cache<A, B> cache, final Map<CacheEntryListenerConfiguration<A, B>, JCSListener<A, B>> listeners) 056 { 057 this.cacheRef = cache; 058 this.listeners = listeners; 059 } 060}