增强jedis连接池功能
程序员文章站
2024-01-06 09:45:34
...
maven 依赖
<dependency> <groupId>com.duowan.common</groupId> <artifactId>duowan-common-redis</artifactId> <version>1.0.1</version> </dependency>
spring配置
<bean id="redisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxActive" value="20" /> <!-- 等待时长,单位:毫秒 --> <property name="maxWait" value="10000" /> <property name="testWhileIdle" value="true" /> </bean> <bean id="udbRedisParent" abstract="true"> <property name="jedisPool"> <bean class="com.duowan.common.redis.JedisPoolFactoryBean"> <property name="poolConfig" ref="redisPoolConfig" /> <!-- <property name="server" value="${udb_redis_server}" /> --> <property name="server" value="127.0.0.1:6379" /> </bean> </property> </bean> <!-- redis dao --> <bean id="udbRedisDao" class="com.duowan.usercenter.dao.redis.UdbDaoRedisImpl" parent="udbRedisParent"></bean>
reids Dao示例代码
import com.duowan.common.redis.RedisDaoSupport; public class UdbRedisDao extends RedisDaoSupport { // 单条操作 public void setUser(String username,String pwd) { getRedisTemplate().set("pwd_"+username, pwd); } // 通过RedisTransactionCallback 批量操作redis public void delUser(final List<String> userList) { getRedisTemplate().execute(new RedisTransactionCallback<Object>() { public Object doInTransaction(Transaction tran) { for(String key : userList) { tran.del(key); } tran.exec(); return null; } }); } }