1 package org.apache.commons.jcs3.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.jcs3.auxiliary.disk.jdbc.JDBCDiskCache;
26 import org.apache.commons.jcs3.auxiliary.disk.jdbc.TableState;
27 import org.apache.commons.jcs3.auxiliary.disk.jdbc.dsfactory.DataSourceFactory;
28 import org.apache.commons.jcs3.engine.behavior.ICacheElement;
29 import org.apache.commons.jcs3.log.Log;
30 import org.apache.commons.jcs3.log.LogManager;
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 */
39 public class MySQLDiskCache<K, V>
40 extends JDBCDiskCache<K, V>
41 {
42 /** local logger */
43 private static final Log log = LogManager.getLog( MySQLDiskCache.class );
44
45 /** config attributes */
46 private final MySQLDiskCacheAttributes mySQLDiskCacheAttributes;
47
48 /**
49 * Delegates to the super and makes use of the MySQL specific parameters used for scheduled
50 * optimization.
51 * <p>
52 * @param attributes the configuration object for this cache
53 * @param dsFactory the DataSourceFactory for this cache
54 * @param tableState an object to track table operations
55 * @throws SQLException if the pool access could not be set up
56 */
57 public MySQLDiskCache( final MySQLDiskCacheAttributes attributes, final DataSourceFactory dsFactory,
58 final TableState tableState) throws SQLException
59 {
60 super( attributes, dsFactory, tableState);
61
62 mySQLDiskCacheAttributes = attributes;
63
64 log.debug( "MySQLDiskCacheAttributes = {0}", attributes );
65 }
66
67 /**
68 * This delegates to the generic JDBC disk cache. If we are currently optimizing, then this
69 * method will balk and return null.
70 * <p>
71 * @param key Key to locate value for.
72 * @return An object matching key, or null.
73 */
74 @Override
75 protected ICacheElement<K, V> processGet( final K key )
76 {
77 if (this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING &&
78 this.mySQLDiskCacheAttributes.isBalkDuringOptimization())
79 {
80 return null;
81 }
82 return super.processGet( 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 return null.
88 * <p>
89 * @param pattern used for like query.
90 * @return An object matching key, or null.
91 */
92 @Override
93 protected Map<K, ICacheElement<K, V>> processGetMatching( final String pattern )
94 {
95 if (this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING &&
96 this.mySQLDiskCacheAttributes.isBalkDuringOptimization())
97 {
98 return null;
99 }
100 return super.processGetMatching( pattern );
101 }
102
103 /**
104 * @param pattern
105 * @return String to use in the like query.
106 */
107 @Override
108 public String constructLikeParameterFromPattern( final String pattern )
109 {
110 String likePattern = pattern.replace( ".+", "%" );
111 likePattern = likePattern.replace( ".", "_" );
112
113 log.debug( "pattern = [{0}]", likePattern );
114
115 return likePattern;
116 }
117
118 /**
119 * This delegates to the generic JDBC disk cache. If we are currently optimizing, then this
120 * method will balk and do nothing.
121 * <p>
122 * @param element
123 */
124 @Override
125 protected void processUpdate( final ICacheElement<K, V> element )
126 {
127 if (this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING &&
128 this.mySQLDiskCacheAttributes.isBalkDuringOptimization())
129 {
130 return;
131 }
132 super.processUpdate( element );
133 }
134
135 /**
136 * Removed the expired. (now - create time) > max life seconds * 1000
137 * <p>
138 * If we are currently optimizing, then this method will balk and do nothing.
139 * <p>
140 * TODO consider blocking and trying again.
141 * <p>
142 * @return the number deleted
143 */
144 @Override
145 protected int deleteExpired()
146 {
147 if (this.getTableState().getState() == TableState.OPTIMIZATION_RUNNING &&
148 this.mySQLDiskCacheAttributes.isBalkDuringOptimization())
149 {
150 return -1;
151 }
152 return super.deleteExpired();
153 }
154 }