写点什么

使用 Redis 和 Java 进行数据库缓存 - DZone 数据库

作者:Java高工P7
  • 2021 年 11 月 11 日
  • 本文字数:1764 字

    阅读完需:约 6 分钟

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


Redis 中的直写缓存




下面是一个 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 );


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


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


/* or */


RMapCache<K, V> map = redisson.getMapCache( "test", options );


/* or with boost up to 45x times */


RLocalCachedMap<K, V> map = redisson.getLocalCachedMap( "test", options );

用户头像

Java高工P7

关注

还未添加个人签名 2021.11.08 加入

还未添加个人简介

评论

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