1 package org.apache.jcs.auxiliary.disk.jdbc.mysql;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 }