原来是这样,在外面循环调用。 获取一次连接,操作一次,关闭连接,想想都慢。
/** * 增加元素到list头位置 * @param keyId * @param bean * @return */ public Long lPush(final String keyId,final Object bean){ Long result = redisTemplate.execute(new RedisCallback<Long>() { public Long doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = getRedisSerializer(); byte[] key = serializer.serialize(keyId); byte[] name ; if(StringUtil.isStr(bean)){ name= serializer.serialize(bean.toString()); }else{ name=serializer.serialize(JSONUtil.toJson(bean)); } connection.openPipeline(); connection.lPush(key, name); connection.lPush(key, name); connection.lPush(key, name); connection.lPush(key, name); connection.lPush(key, name); List<Object> xxList = connection.closePipeline(); return xxList; return connection.lPush(key, name); } }); return result; }
可以改成这样,随便写的,没试~~
/** * 增加元素到list头位置 * @param keyId * @param bean * @return */ public List<Object> lPush(final List<String> keyId,final List<Object> bean){ Long result = redisTemplate.execute(new RedisCallback<Long>() { public Long doInRedis(RedisConnection connection) throws DataAccessException {
connection.openPipeline(); for循环{ key=... value=.. connection.lPush(key, name); } return connection.closePipeline();
} }); return result; }
|