使用 Redis 和 Java 进行数据库缓存 - DZone 数据库
return(list);
}
@Override
public String load( String key )
{
PreparedStatement preparedStatement = conn.prepareStatement( "SELECT name FROM student where id = ?" );
try {
preparedStatement.setString( 1, key );
ResultSet result = preparedStatement.executeQuery();
if ( result.next() )
{
return(result.getString( 1 ) );
}
return(null);
} finally {
preparedStatement.close();
}
}
}
配置使用案例:
MapOptions<K, V> options = MapOptions.< K, V > defaults()
.loader( mapLoader );
RMap<K, V> map = redisson.getMap( "test", options );
/* or */
RMapCache<K, V> map = redisson.getMapCache( "test", options );
/* or with boost up to 45x times */
RLocalCachedMap<K, V> map = redisson.getLocalCachedMap( "test", options );
/* or with boost up to 45x times */
RLocalCachedMapCache<K, V> map = redisson.getLocalCachedMapCache( "test", options );
下面是一个 Java 示例,说明如何在 Redis 中使用 Redis 中的 Redis 使用直写缓存。
在 MapWriter 对象更新缓存和数据库之前,缓存更新方法不会返回:
MapWriter<String, String> mapWriter = new MapWriter<String, String>()
{
@Override
public void writeAll( Map<String, String> map )
{
PreparedStatement preparedStatement = conn.prepareStatement( "INSERT INTO student (id, name) values (?, ?)" );
try {
for ( Entry<String, String> entry : map.entrySet() )
{
preparedStatement.setString( 1, entry.getKey() );
preparedStatement.setString( 2, entry.getValue() );
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
} finally {
preparedStatement.close();
}
}
@Override
public void write( String key, String value )
{
PreparedStatement preparedStatement = conn.prepareStatement( "INSERT INTO student (id, name) values (?, ?)" );
try {
preparedStatement.setString( 1, key );
preparedStatement.setString( 2, value );
preparedStatement.executeUpdate();
} finally {
preparedStatement.close();
}
}
@Override
public void deleteAll( Collection<String> keys )
{
PreparedStatement preparedStatement = conn.prepareStatement( "DELETE FROM student where id = ?" );
try {
for ( String key : keys )
{
preparedStatement.setString( 1, key );
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
} finally {
preparedStatement.close();
}
}
@Override
public void delete( String key )
{
PreparedStatement preparedStatement = conn.prepareStatement( "DELETE FROM student where id = ?" );
try {
preparedStatement.setString( 1, key );
preparedStatement.executeUpdate();
} finally {
preparedStatement.close();
}
}
}
使用配置案例:
MapOptions<K, V> options = MapOptions.< K, V > defaults()
.writer( mapWriter )
.writeMode( WriteMode.WRITE_THROUGH );
RMap<K, V> map = redisson.getMap( "test", options );
/* or */
RMapCache<K, V> map = redisson.getMapCache( "test", options );
/* or with boost up to 45x times */
RLocalCachedMap<K, V> map = redisson.getLocalCachedMap( "test", options );
/* or with boost up to 45x times */
RLocalCachedMapCache<K, V> map = redisson.getLocalCachedMapCache( "test", options );
MapWriter 接口还用于异步提交对 Map 对象(缓存)和外部存储(数据库)的更新。后台写入操作执行中使用的线程数量通过 writeBehindThreads 设置设置。
下面,我们看到 Redisson 中基于 Redis 的后写缓存实现的配置的 Java 示例:
MapOptions<K, V> options = MapOptions.< K, V > defaults()
.writer( mapWriter )
.writeMode( WriteMode.WRITE_BEHIND )
.writeBehindThreads( 8 );
RMap<K, V> map = redisson.getMap( "test", options );
/* or */
RMapCache<K, V> map = redisson.getMapCache( "test", options );
/* or with boost up to 45x times */
RLocalCachedMap<K, V> map = redisson.getLocalCachedMap( "test", options );
评论