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

本文介绍了如何使用Redis和Java进行数据库缓存的配置与使用,包括直写缓存和后写缓存的实现方式,展示了MapWriter接口在更新缓存和数据库中的应用。同时还提供了Redisson库中相关配置的Java示例。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值