写点什么

使用 Redis 和 Java 进行数据库缓存 - DZone 数据库,工作感悟

用户头像
极客good
关注
发布于: 刚刚

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 );


[](


)Redis 中的直写缓存




下面是一个 Java 示例,说明如何在 Redis 中使用 Redis 中的 Redis 使用直写缓存。


在 MapWriter 对象更新缓存和数据库之前,缓存更新方法不会返回:


【一线大厂Java面试题解析+核心总结学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


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 );


[](


)Redis 中的后写缓存




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 );

用户头像

极客good

关注

还未添加个人签名 2021.03.18 加入

还未添加个人简介

评论

发布
暂无评论
使用Redis和Java进行数据库缓存 - DZone数据库,工作感悟