org.apache.cayenne.cache.OSQueryCache Maven / Gradle / Ivy
/*****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
****************************************************************/
package org.apache.cayenne.cache;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.cayenne.CayenneRuntimeException;
import org.apache.cayenne.di.BeforeScopeEnd;
import org.apache.cayenne.query.QueryMetadata;
import com.opensymphony.oscache.base.CacheEntry;
import com.opensymphony.oscache.base.NeedsRefreshException;
import com.opensymphony.oscache.general.GeneralCacheAdministrator;
/**
* A {@link QueryCache} implementation based on OpenSymphony OSCache. Query cache
* parameters are initialized from "/oscache.properties" file per OSCache
* documentation. In addition to the standard OSCache parameters, Cayenne provider allows
* to setup global cache expiration parameters, and parameters matching the main query
* cache group (i.e. the cache groups specified first). A sample oscache.properties may
* look like this:
*
*
* # OSCache configuration file
*
* # OSCache standard configuration per
* # http://www.opensymphony.com/oscache/wiki/Configuration.html
* # ---------------------------------------------------------------
*
* #cache.memory=true
* #cache.blocking=false
* cache.capacity=5000
* cache.algorithm=com.opensymphony.oscache.base.algorithm.LRUCache
*
* # Cayenne specific properties
* # ---------------------------------------------------------------
*
* # Default refresh period in seconds:
* cayenne.default.refresh = 60
*
* # Default expiry specified as cron expressions per
* # http://www.opensymphony.com/oscache/wiki/Cron%20Expressions.html
* # expire entries every hour on the 10's minute
* cayenne.default.cron = 10 * * * *
*
* # Same parameters can be overriden per query
* cayenne.group.xyz.refresh = 120
* cayenne.group.xyz.cron = 10 1 * * *
*
*
* Further extension of OSQueryCache is possible by using OSCache listener API.
*
* @since 3.0
*/
public class OSQueryCache implements QueryCache {
public static final int DEFAULT_REFRESH_PERIOD = CacheEntry.INDEFINITE_EXPIRY;
static String DEFAULT_REFRESH_KEY = "cayenne.default.refresh";
static String DEFAULT_CRON_KEY = "cayenne.default.cron";
static String GROUP_PREFIX = "cayenne.group.";
static String REFRESH_SUFFIX = ".refresh";
static String CRON_SUFFIX = ".cron";
protected GeneralCacheAdministrator osCache;
RefreshSpecification defaultRefreshSpecification;
Map refreshSpecifications;
Properties properties;
public OSQueryCache() {
OSCacheAdministrator admin = new OSCacheAdministrator();
init(admin, admin.getProperties());
}
public OSQueryCache(GeneralCacheAdministrator cache, Properties properties) {
init(cache, properties);
}
/**
* Returns a collection of group names that have been configured explicitly via
* properties.
*/
@SuppressWarnings("unchecked")
public Collection getGroupNames() {
return refreshSpecifications != null
? Collections.unmodifiableCollection(refreshSpecifications.keySet())
: Collections.EMPTY_SET;
}
public String getCronExpression(String groupName) {
RefreshSpecification spec = null;
if (refreshSpecifications != null) {
spec = refreshSpecifications.get(groupName);
}
if (spec == null) {
spec = defaultRefreshSpecification;
}
return spec.cronExpression;
}
public int getRrefreshPeriod(String groupName) {
RefreshSpecification spec = null;
if (refreshSpecifications != null) {
spec = refreshSpecifications.get(groupName);
}
if (spec == null) {
spec = defaultRefreshSpecification;
}
return spec.refreshPeriod;
}
/**
* Returns the underlying OSCache manager object.
*/
public GeneralCacheAdministrator getOsCache() {
return osCache;
}
/**
* Returns configuration properties. Usually this is the contents of
* "oscache.properties" file.
*/
public Properties getProperties() {
return properties;
}
void init(GeneralCacheAdministrator cache, Properties properties) {
this.properties = properties;
this.osCache = cache;
this.defaultRefreshSpecification = new RefreshSpecification();
// load defaults and per-query settings
if (properties != null) {
// first extract defaults...
String defaultRefresh = properties.getProperty(DEFAULT_REFRESH_KEY);
if (defaultRefresh != null) {
defaultRefreshSpecification.setRefreshPeriod(defaultRefresh);
}
String defaultCron = properties.getProperty(DEFAULT_CRON_KEY);
if (defaultCron != null) {
defaultRefreshSpecification.cronExpression = defaultCron;
}
// now check for per-query settings
for (final Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy