View Javadoc
1   package org.apache.commons.jcs.auxiliary.disk.jdbc.mysql;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.sql.SQLException;
23  import java.util.Map;
24  
25  import org.apache.commons.jcs.auxiliary.disk.jdbc.JDBCDiskCache;
26  import org.apache.commons.jcs.auxiliary.disk.jdbc.TableState;
27  import org.apache.commons.jcs.auxiliary.disk.jdbc.dsfactory.DataSourceFactory;
28  import org.apache.commons.jcs.engine.behavior.ICacheElement;
29  import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  /**
34   * The MySQLDiskCache extends the core JDBCDiskCache.
35   * <p>
36   * Although the generic JDBC Disk Cache can be used for MySQL, the MySQL JDBC Disk Cache has
37   * additional features, such as table optimization that are particular to MySQL.
38   * <p>
39   * @author Aaron Smuts
40   */
41  public class MySQLDiskCache<K, V>
42  	extends JDBCDiskCache<K, V>
43  {
44      /** local logger */
45      private static final Log log = LogFactory.getLog( MySQLDiskCache.class );
46  
47      /** config attributes */
48      private final MySQLDiskCacheAttributes mySQLDiskCacheAttributes;
49  
50      /**
51       * Delegates to the super and makes use of the MySQL specific parameters used for scheduled
52       * optimization.
53       * <p>
54       * @param attributes the configuration object for this cache
55       * @param dsFactory the DataSourceFactory for this cache
56       * @param tableState an object to track table operations
57       * @param compositeCacheManager the global cache manager
58       * @throws SQLException if the pool access could not be set up
59       */
60      public MySQLDiskCache( MySQLDiskCacheAttributes attributes, DataSourceFactory dsFactory,
61      		TableState tableState, ICompositeCacheManager compositeCacheManager ) throws SQLException
62      {
63          super( attributes, dsFactory, tableState, compositeCacheManager );
64  
65          mySQLDiskCacheAttributes = attributes;
66  
67          if ( log.isDebugEnabled() )
68          {
69              log.debug( "MySQLDiskCacheAttributes = " + attributes );
70          }
71      }
72  
73      /**
74       * This delegates to the generic JDBC disk cache. If we are currently optimizing, then this
75       * method will balk and return null.
76       * <p>
77       * @param key Key to locate value for.
78       * @return An object matching key, or null.
79       */
80      @Override
81      protected ICacheElement<K, V> processGet( K key )
82      {
83          if ( this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING )
84          {
85              if ( this.mySQLDiskCacheAttributes.isBalkDuringOptimization() )
86              {
87                  return null;
88              }
89          }
90          return super.processGet( key );
91      }
92  
93      /**
94       * This delegates to the generic JDBC disk cache. If we are currently optimizing, then this
95       * method will balk and return null.
96       * <p>
97       * @param pattern used for like query.
98       * @return An object matching key, or null.
99       */
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) &gt; 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 }