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 import java.util.Map;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.jcs.auxiliary.disk.jdbc.JDBCDiskCache;
28 import org.apache.jcs.auxiliary.disk.jdbc.TableState;
29 import org.apache.jcs.engine.behavior.ICacheElement;
30 import org.apache.jcs.engine.behavior.ICompositeCacheManager;
31
32 /**
33 * The MySQLDiskCache extends the core JDBCDiskCache.
34 * <p>
35 * Although the generic JDBC Disk Cache can be used for MySQL, the MySQL JDBC Disk Cache has
36 * additional features, such as table optimization that are particular to MySQL.
37 * <p>
38 * @author Aaron Smuts
39 */
40 public class MySQLDiskCache<K extends Serializable, V extends Serializable>
41 extends JDBCDiskCache<K, V>
42 {
43 /** don't change */
44 private static final long serialVersionUID = -7169488308515823491L;
45
46 /** local logger */
47 private final static Log log = LogFactory.getLog( MySQLDiskCache.class );
48
49 /** config attributes */
50 private final MySQLDiskCacheAttributes mySQLDiskCacheAttributes;
51
52 /**
53 * Delegates to the super and makes use of the MySQL specific parameters used for scheduled
54 * optimization.
55 * <p>
56 * @param attributes
57 * @param tableState
58 * @param compositeCacheManager
59 */
60 public MySQLDiskCache( MySQLDiskCacheAttributes attributes, TableState tableState, ICompositeCacheManager compositeCacheManager )
61 {
62 super( attributes, tableState, compositeCacheManager );
63
64 mySQLDiskCacheAttributes = attributes;
65
66 if ( log.isDebugEnabled() )
67 {
68 log.debug( "MySQLDiskCacheAttributes = " + attributes );
69 }
70 }
71
72 /**
73 * This delegates to the generic JDBC disk cache. If we are currently optimizing, then this
74 * method will balk and return null.
75 * <p>
76 * @param key Key to locate value for.
77 * @return An object matching key, or null.
78 */
79 @Override
80 protected ICacheElement<K, V> processGet( K key )
81 {
82 if ( this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING )
83 {
84 if ( this.mySQLDiskCacheAttributes.isBalkDuringOptimization() )
85 {
86 return null;
87 }
88 }
89 return super.processGet( key );
90 }
91
92 /**
93 * This delegates to the generic JDBC disk cache. If we are currently optimizing, then this
94 * method will balk and return null.
95 * <p>
96 * @param pattern used for like query.
97 * @return An object matching key, or null.
98 */
99 @Override
100 protected Map<K, ICacheElement<K, V>> processGetMatching( String pattern )
101 {
102 if ( this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING )
103 {
104 if ( this.mySQLDiskCacheAttributes.isBalkDuringOptimization() )
105 {
106 return null;
107 }
108 }
109 return super.processGetMatching( pattern );
110 }
111
112 /**
113 * @param pattern
114 * @return String to use in the like query.
115 */
116 @Override
117 public String constructLikeParameterFromPattern( String pattern )
118 {
119 String likePattern = pattern.replaceAll( "\\.\\+", "%" );
120 likePattern = likePattern.replaceAll( "\\.", "_" );
121
122 if ( log.isDebugEnabled() )
123 {
124 log.debug( "pattern = [" + likePattern + "]" );
125 }
126
127 return likePattern;
128 }
129
130 /**
131 * This delegates to the generic JDBC disk cache. If we are currently optimizing, then this
132 * method will balk and do nothing.
133 * <p>
134 * @param element
135 */
136 @Override
137 protected void processUpdate( ICacheElement<K, V> element )
138 {
139 if ( this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING )
140 {
141 if ( this.mySQLDiskCacheAttributes.isBalkDuringOptimization() )
142 {
143 return;
144 }
145 }
146 super.processUpdate( element );
147 }
148
149 /**
150 * Removed the expired. (now - create time) > max life seconds * 1000
151 * <p>
152 * If we are currently optimizing, then this method will balk and do nothing.
153 * <p>
154 * TODO consider blocking and trying again.
155 * <p>
156 * @return the number deleted
157 */
158 @Override
159 protected int deleteExpired()
160 {
161 if ( this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING )
162 {
163 if ( this.mySQLDiskCacheAttributes.isBalkDuringOptimization() )
164 {
165 return -1;
166 }
167 }
168 return super.deleteExpired();
169 }
170 }