This is an automated email from the ASF dual-hosted git repository. tv pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-jcs.git
commit 83e8dd4f3a8beeb013dbc1924e949b97f7eab1e2 Author: Thomas Vandahl <t...@apache.org> AuthorDate: Mon Feb 15 22:23:00 2021 +0100 Fix key handling and streamline code --- .../jcs3/auxiliary/disk/jdbc/JDBCDiskCache.java | 190 ++++++++------------- 1 file changed, 73 insertions(+), 117 deletions(-) diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/jdbc/JDBCDiskCache.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/jdbc/JDBCDiskCache.java index b425a61..4c8ede0 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/jdbc/JDBCDiskCache.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/jdbc/JDBCDiskCache.java @@ -37,6 +37,7 @@ import javax.sql.DataSource; import org.apache.commons.jcs3.auxiliary.AuxiliaryCacheAttributes; import org.apache.commons.jcs3.auxiliary.disk.AbstractDiskCache; import org.apache.commons.jcs3.auxiliary.disk.jdbc.dsfactory.DataSourceFactory; +import org.apache.commons.jcs3.engine.behavior.ICache; import org.apache.commons.jcs3.engine.behavior.ICacheElement; import org.apache.commons.jcs3.engine.behavior.IElementSerializer; import org.apache.commons.jcs3.engine.logging.behavior.ICacheEvent; @@ -99,9 +100,6 @@ public class JDBCDiskCache<K, V> /** # of times getMatching was called */ private final AtomicInteger getMatchingCount = new AtomicInteger(0); - /** if count % interval == 0 then log */ - private static final int LOG_INTERVAL = 100; - /** db connection pool */ private final DataSourceFactory dsFactory; @@ -150,33 +148,20 @@ public class JDBCDiskCache<K, V> { log.debug( "Putting [{0}] on disk.", () -> ce.getKey()); - final byte[] element; - try { - element = getElementSerializer().serialize( ce ); + final byte[] element = getElementSerializer().serialize( ce ); + insertOrUpdate( ce, con, element ); } catch ( final IOException e ) { log.error( "Could not serialize element", e ); - return; } - - insertOrUpdate( ce, con, element ); } catch ( final SQLException e ) { log.error( "Problem getting connection.", e ); } - - if ( log.isInfoEnabled() ) - { - if ( updateCount.get() % LOG_INTERVAL == 0 ) - { - // TODO make a log stats method - log.info( "Update Count [{0}]", updateCount); - } - } } /** @@ -222,25 +207,19 @@ public class JDBCDiskCache<K, V> private boolean insertRow( final ICacheElement<K, V> ce, final Connection con, final byte[] element ) { boolean exists = false; - final String sqlI = "insert into " - + getJdbcDiskCacheAttributes().getTableName() - + " (CACHE_KEY, REGION, ELEMENT, MAX_LIFE_SECONDS, IS_ETERNAL, CREATE_TIME, UPDATE_TIME_SECONDS, SYSTEM_EXPIRE_TIME_SECONDS) " - + " values (?, ?, ?, ?, ?, ?, ?, ?)"; + final String sqlI = String.format("insert into %s" + + " (CACHE_KEY, REGION, ELEMENT, MAX_LIFE_SECONDS, IS_ETERNAL, CREATE_TIME, UPDATE_TIME_SECONDS," + + " SYSTEM_EXPIRE_TIME_SECONDS) " + + " values (?, ?, ?, ?, ?, ?, ?, ?)", getJdbcDiskCacheAttributes().getTableName()); try (PreparedStatement psInsert = con.prepareStatement( sqlI )) { - psInsert.setString( 1, (String) ce.getKey() ); + psInsert.setString( 1, ce.getKey().toString() ); psInsert.setString( 2, this.getCacheName() ); psInsert.setBytes( 3, element ); psInsert.setLong( 4, ce.getElementAttributes().getMaxLife() ); - if ( ce.getElementAttributes().getIsEternal() ) - { - psInsert.setString( 5, "T" ); - } - else - { - psInsert.setString( 5, "F" ); - } + psInsert.setString( 5, ce.getElementAttributes().getIsEternal() ? "T" : "F" ); + final Timestamp createTime = new Timestamp( ce.getElementAttributes().getCreateTime() ); psInsert.setTimestamp( 6, createTime ); @@ -282,9 +261,9 @@ public class JDBCDiskCache<K, V> */ private void updateRow( final ICacheElement<K, V> ce, final Connection con, final byte[] element ) { - final String sqlU = "update " + getJdbcDiskCacheAttributes().getTableName() + final String sqlU = String.format("update %s" + " set ELEMENT = ?, CREATE_TIME = ?, UPDATE_TIME_SECONDS = ?, " + " SYSTEM_EXPIRE_TIME_SECONDS = ? " - + " where CACHE_KEY = ? and REGION = ?"; + + " where CACHE_KEY = ? and REGION = ?", getJdbcDiskCacheAttributes().getTableName()); try (PreparedStatement psUpdate = con.prepareStatement( sqlU )) { @@ -322,8 +301,8 @@ public class JDBCDiskCache<K, V> { boolean exists = false; // don't select the element, since we want this to be fast. - final String sqlS = "select CACHE_KEY from " + getJdbcDiskCacheAttributes().getTableName() - + " where REGION = ? and CACHE_KEY = ?"; + final String sqlS = String.format("select CACHE_KEY from %s where REGION = ? and CACHE_KEY = ?", + getJdbcDiskCacheAttributes().getTableName()); try (PreparedStatement psSelect = con.prepareStatement( sqlS )) { @@ -366,37 +345,36 @@ public class JDBCDiskCache<K, V> ICacheElement<K, V> obj = null; - byte[] data = null; - try - { - // region, key - final String selectString = "select ELEMENT from " + getJdbcDiskCacheAttributes().getTableName() - + " where REGION = ? and CACHE_KEY = ?"; + // region, key + final String selectString = String.format("select ELEMENT from %s where REGION = ? and CACHE_KEY = ?", + getJdbcDiskCacheAttributes().getTableName()); - try (Connection con = getDataSource().getConnection()) + try (Connection con = getDataSource().getConnection()) + { + try (PreparedStatement psSelect = con.prepareStatement( selectString )) { - try (PreparedStatement psSelect = con.prepareStatement( selectString )) + psSelect.setString( 1, this.getCacheName() ); + psSelect.setString( 2, key.toString() ); + + try (ResultSet rs = psSelect.executeQuery()) { - psSelect.setString( 1, this.getCacheName() ); - psSelect.setString( 2, key.toString() ); + byte[] data = null; + + if ( rs.next() ) + { + data = rs.getBytes( 1 ); + } - try (ResultSet rs = psSelect.executeQuery()) + if ( data != null ) { - if ( rs.next() ) + try { - data = rs.getBytes( 1 ); + // USE THE SERIALIZER + obj = getElementSerializer().deSerialize( data, null ); } - if ( data != null ) + catch ( final IOException | ClassNotFoundException e ) { - try - { - // USE THE SERIALIZER - obj = getElementSerializer().deSerialize( data, null ); - } - catch ( final Exception e ) - { - log.error( "Problem getting item for key [{0}]", key, e ); - } + log.error( "Problem getting item for key [{0}]", key, e ); } } } @@ -408,14 +386,6 @@ public class JDBCDiskCache<K, V> key, sqle ); } - if ( log.isInfoEnabled() ) - { - if ( getCount.get() % LOG_INTERVAL == 0 ) - { - // TODO make a log stats method - log.info( "Get Count [{0}]", getCount ); - } - } return obj; } @@ -440,37 +410,33 @@ public class JDBCDiskCache<K, V> final Map<K, ICacheElement<K, V>> results = new HashMap<>(); - try - { - // region, key - final String selectString = "select CACHE_KEY, ELEMENT from " + getJdbcDiskCacheAttributes().getTableName() - + " where REGION = ? and CACHE_KEY like ?"; + // region, key + final String selectString = String.format("select ELEMENT from %s where REGION = ? and CACHE_KEY like ?", + getJdbcDiskCacheAttributes().getTableName()); - try (Connection con = getDataSource().getConnection()) + try (Connection con = getDataSource().getConnection()) + { + try (PreparedStatement psSelect = con.prepareStatement( selectString )) { - try (PreparedStatement psSelect = con.prepareStatement( selectString )) - { - psSelect.setString( 1, this.getCacheName() ); - psSelect.setString( 2, constructLikeParameterFromPattern( pattern ) ); + psSelect.setString( 1, this.getCacheName() ); + psSelect.setString( 2, constructLikeParameterFromPattern( pattern ) ); - try (ResultSet rs = psSelect.executeQuery()) + try (ResultSet rs = psSelect.executeQuery()) + { + while ( rs.next() ) { - while ( rs.next() ) + final byte[] data = rs.getBytes(1); + if ( data != null ) { - final String key = rs.getString( 1 ); - final byte[] data = rs.getBytes( 2 ); - if ( data != null ) + try + { + // USE THE SERIALIZER + final ICacheElement<K, V> value = getElementSerializer().deSerialize( data, null ); + results.put( value.getKey(), value ); + } + catch ( final IOException | ClassNotFoundException e ) { - try - { - // USE THE SERIALIZER - final ICacheElement<K, V> value = getElementSerializer().deSerialize( data, null ); - results.put( (K) key, value ); - } - catch ( final Exception e ) - { - log.error( "Problem getting items for pattern [{0}]", pattern, e ); - } + log.error( "Problem getting items for pattern [{0}]", pattern, e ); } } } @@ -483,14 +449,6 @@ public class JDBCDiskCache<K, V> pattern, sqle ); } - if ( log.isInfoEnabled() ) - { - if ( getMatchingCount.get() % LOG_INTERVAL == 0 ) - { - // TODO make a log stats method - log.info( "Get Matching Count [{0}]", getMatchingCount); - } - } return results; } @@ -519,21 +477,18 @@ public class JDBCDiskCache<K, V> protected boolean processRemove( final K key ) { // remove single item. - String sql = "delete from " + getJdbcDiskCacheAttributes().getTableName() - + " where REGION = ? and CACHE_KEY = ?"; + final String sqlSingle = String.format("delete from %s where REGION = ? and CACHE_KEY = ?", + getJdbcDiskCacheAttributes().getTableName()); + // remove all keys of the same name group. + final String sqlPartial = String.format("delete from %s where REGION = ? and CACHE_KEY like ?", + getJdbcDiskCacheAttributes().getTableName()); try (Connection con = getDataSource().getConnection()) { - boolean partial = false; - if ( key instanceof String && key.toString().endsWith( NAME_COMPONENT_DELIMITER ) ) - { - // remove all keys of the same name group. - sql = "delete from " + getJdbcDiskCacheAttributes().getTableName() - + " where REGION = ? and CACHE_KEY like ?"; - partial = true; - } + boolean partial = key.toString().endsWith(ICache.NAME_COMPONENT_DELIMITER); + String sql = partial ? sqlPartial : sqlSingle; - try (PreparedStatement psSelect = con.prepareStatement( sql )) + try (PreparedStatement psSelect = con.prepareStatement(sql)) { psSelect.setString( 1, this.getCacheName() ); if ( partial ) @@ -573,10 +528,11 @@ public class JDBCDiskCache<K, V> // it should never get here from the abstract disk cache. if ( this.jdbcDiskCacheAttributes.isAllowRemoveAll() ) { + final String sql = String.format("delete from %s where REGION = ?", + getJdbcDiskCacheAttributes().getTableName()); + try (Connection con = getDataSource().getConnection()) { - final String sql = "delete from " + getJdbcDiskCacheAttributes().getTableName() + " where REGION = ?"; - try (PreparedStatement psDelete = con.prepareStatement( sql )) { psDelete.setString( 1, this.getCacheName() ); @@ -624,8 +580,8 @@ public class JDBCDiskCache<K, V> getTableState().setState( TableState.DELETE_RUNNING ); final long now = System.currentTimeMillis() / 1000; - final String sql = "delete from " + getJdbcDiskCacheAttributes().getTableName() - + " where IS_ETERNAL = ? and REGION = ? and ? > SYSTEM_EXPIRE_TIME_SECONDS"; + final String sql = String.format("delete from %s where IS_ETERNAL = ? and REGION = ?" + + " and ? > SYSTEM_EXPIRE_TIME_SECONDS", getJdbcDiskCacheAttributes().getTableName()); try (PreparedStatement psDelete = con.prepareStatement( sql )) { @@ -679,7 +635,7 @@ public class JDBCDiskCache<K, V> @Override public void processDispose() { - final ICacheEvent<K> cacheEvent = createICacheEvent( getCacheName(), (K)"none", ICacheEventLogger.DISPOSE_EVENT ); + final ICacheEvent<K> cacheEvent = createICacheEvent( getCacheName(), (K)null, ICacheEventLogger.DISPOSE_EVENT ); try { @@ -706,8 +662,8 @@ public class JDBCDiskCache<K, V> int size = 0; // region, key - final String selectString = "select count(*) from " + getJdbcDiskCacheAttributes().getTableName() - + " where REGION = ?"; + final String selectString = String.format("select count(*) from %s where REGION = ?", + getJdbcDiskCacheAttributes().getTableName()); try (Connection con = getDataSource().getConnection()) {