001package org.apache.commons.jcs.auxiliary.disk.jdbc.mysql; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.sql.SQLException; 023import java.util.Map; 024 025import org.apache.commons.jcs.auxiliary.disk.jdbc.JDBCDiskCache; 026import org.apache.commons.jcs.auxiliary.disk.jdbc.TableState; 027import org.apache.commons.jcs.auxiliary.disk.jdbc.dsfactory.DataSourceFactory; 028import org.apache.commons.jcs.engine.behavior.ICacheElement; 029import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager; 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.LogFactory; 032 033/** 034 * The MySQLDiskCache extends the core JDBCDiskCache. 035 * <p> 036 * Although the generic JDBC Disk Cache can be used for MySQL, the MySQL JDBC Disk Cache has 037 * additional features, such as table optimization that are particular to MySQL. 038 * <p> 039 * @author Aaron Smuts 040 */ 041public class MySQLDiskCache<K, V> 042 extends JDBCDiskCache<K, V> 043{ 044 /** local logger */ 045 private static final Log log = LogFactory.getLog( MySQLDiskCache.class ); 046 047 /** config attributes */ 048 private final MySQLDiskCacheAttributes mySQLDiskCacheAttributes; 049 050 /** 051 * Delegates to the super and makes use of the MySQL specific parameters used for scheduled 052 * optimization. 053 * <p> 054 * @param attributes the configuration object for this cache 055 * @param dsFactory the DataSourceFactory for this cache 056 * @param tableState an object to track table operations 057 * @param compositeCacheManager the global cache manager 058 * @throws SQLException if the pool access could not be set up 059 */ 060 public MySQLDiskCache( MySQLDiskCacheAttributes attributes, DataSourceFactory dsFactory, 061 TableState tableState, ICompositeCacheManager compositeCacheManager ) throws SQLException 062 { 063 super( attributes, dsFactory, tableState, compositeCacheManager ); 064 065 mySQLDiskCacheAttributes = attributes; 066 067 if ( log.isDebugEnabled() ) 068 { 069 log.debug( "MySQLDiskCacheAttributes = " + attributes ); 070 } 071 } 072 073 /** 074 * This delegates to the generic JDBC disk cache. If we are currently optimizing, then this 075 * method will balk and return null. 076 * <p> 077 * @param key Key to locate value for. 078 * @return An object matching key, or null. 079 */ 080 @Override 081 protected ICacheElement<K, V> processGet( K key ) 082 { 083 if ( this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING ) 084 { 085 if ( this.mySQLDiskCacheAttributes.isBalkDuringOptimization() ) 086 { 087 return null; 088 } 089 } 090 return super.processGet( key ); 091 } 092 093 /** 094 * This delegates to the generic JDBC disk cache. If we are currently optimizing, then this 095 * method will balk and return null. 096 * <p> 097 * @param pattern used for like query. 098 * @return An object matching key, or null. 099 */ 100 @Override 101 protected Map<K, ICacheElement<K, V>> processGetMatching( String pattern ) 102 { 103 if ( this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING ) 104 { 105 if ( this.mySQLDiskCacheAttributes.isBalkDuringOptimization() ) 106 { 107 return null; 108 } 109 } 110 return super.processGetMatching( pattern ); 111 } 112 113 /** 114 * @param pattern 115 * @return String to use in the like query. 116 */ 117 @Override 118 public String constructLikeParameterFromPattern( String pattern ) 119 { 120 String likePattern = pattern.replaceAll( "\\.\\+", "%" ); 121 likePattern = likePattern.replaceAll( "\\.", "_" ); 122 123 if ( log.isDebugEnabled() ) 124 { 125 log.debug( "pattern = [" + likePattern + "]" ); 126 } 127 128 return likePattern; 129 } 130 131 /** 132 * This delegates to the generic JDBC disk cache. If we are currently optimizing, then this 133 * method will balk and do nothing. 134 * <p> 135 * @param element 136 */ 137 @Override 138 protected void processUpdate( ICacheElement<K, V> element ) 139 { 140 if ( this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING ) 141 { 142 if ( this.mySQLDiskCacheAttributes.isBalkDuringOptimization() ) 143 { 144 return; 145 } 146 } 147 super.processUpdate( element ); 148 } 149 150 /** 151 * Removed the expired. (now - create time) > max life seconds * 1000 152 * <p> 153 * If we are currently optimizing, then this method will balk and do nothing. 154 * <p> 155 * TODO consider blocking and trying again. 156 * <p> 157 * @return the number deleted 158 */ 159 @Override 160 protected int deleteExpired() 161 { 162 if ( this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING ) 163 { 164 if ( this.mySQLDiskCacheAttributes.isBalkDuringOptimization() ) 165 { 166 return -1; 167 } 168 } 169 return super.deleteExpired(); 170 } 171}