View Javadoc

1   package org.apache.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.io.Serializable;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.jcs.auxiliary.disk.jdbc.JDBCDiskCache;
27  import org.apache.jcs.auxiliary.disk.jdbc.TableState;
28  import org.apache.jcs.engine.behavior.ICacheElement;
29  
30  /***
31   * The MySQLDiskCache extends the core JDBCDiskCache.
32   * <p>
33   * Although the generic JDBC Disk Cache can be used for MySQL, the MySQL JDBC Disk Cache has
34   * additional features, such as table optimization that are particular to MySQL.
35   * <p>
36   * @author Aaron Smuts
37   */
38  public class MySQLDiskCache
39      extends JDBCDiskCache
40  {
41      private static final long serialVersionUID = -7169488308515823491L;
42  
43      private final static Log log = LogFactory.getLog( MySQLDiskCache.class );
44  
45      MySQLDiskCacheAttributes mySQLDiskCacheAttributes;
46  
47      /***
48       * Delegates to the super and makes use of the MySQL specific parameters used for scheduled
49       * optimization.
50       * <p>
51       * @param attributes
52       * @param tableState
53       */
54      public MySQLDiskCache( MySQLDiskCacheAttributes attributes, TableState tableState )
55      {
56          super( attributes, tableState );
57  
58          mySQLDiskCacheAttributes = attributes;
59  
60          if ( log.isDebugEnabled() )
61          {
62              log.debug( "MySQLDiskCacheAttributes = " + attributes );
63          }
64      }
65  
66      /***
67       * This delegates to the generic JDBC disk cache. If we are currently optimizing, then this
68       * method will balk and return null.
69       * <p>
70       * @param key Key to locate value for.
71       * @return An object matching key, or null.
72       */
73      public ICacheElement doGet( Serializable key )
74      {
75          if ( this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING )
76          {
77              if ( this.mySQLDiskCacheAttributes.isBalkDuringOptimization() )
78              {
79                  return null;
80              }
81          }
82          return super.doGet( key );
83      }
84  
85      /***
86       * This delegates to the generic JDBC disk cache. If we are currently optimizing, then this
87       * method will balk and do nothing.
88       * <p>
89       * @param element
90       */
91      public void doUpdate( ICacheElement element )
92      {
93          if ( this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING )
94          {
95              if ( this.mySQLDiskCacheAttributes.isBalkDuringOptimization() )
96              {
97                  return;
98              }
99          }
100         super.doUpdate( element );
101     }
102 
103     /***
104      * Removed the expired. (now - create time) > max life seconds * 1000
105      * <p>
106      * If we are currently optimizing, then this method will balk and do nothing.
107      * <p>
108      * TODO consider blocking and trying again.
109      * <p>
110      * @return the number deleted
111      */
112     protected int deleteExpired()
113     {
114         if ( this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING )
115         {
116             if ( this.mySQLDiskCacheAttributes.isBalkDuringOptimization() )
117             {
118                 return -1;
119             }
120         }
121         return super.deleteExpired();
122     }
123 }